-
Notifications
You must be signed in to change notification settings - Fork 458
Move html characeter converting mechanism to regexp-handling #1570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,34 +29,6 @@ module RDoc::Text | |
|
|
||
| MARKUP_FORMAT.default = RDoc::Markup | ||
|
|
||
| ## | ||
| # Maps an encoding to a Hash of characters properly transcoded for that | ||
| # encoding. | ||
| # | ||
| # See also encode_fallback. | ||
|
|
||
| TO_HTML_CHARACTERS = Hash.new do |h, encoding| | ||
| h[encoding] = { | ||
| :close_dquote => encode_fallback('”', encoding, '"'), | ||
| :close_squote => encode_fallback('’', encoding, '\''), | ||
| :copyright => encode_fallback('©', encoding, '(c)'), | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think html character is |
||
| :ellipsis => encode_fallback('…', encoding, '...'), | ||
| :em_dash => encode_fallback('—', encoding, '---'), | ||
| :en_dash => encode_fallback('–', encoding, '--'), | ||
| :open_dquote => encode_fallback('“', encoding, '"'), | ||
| :open_squote => encode_fallback('‘', encoding, '\''), | ||
| :trademark => encode_fallback('®', encoding, '(r)'), | ||
| } | ||
| end | ||
|
|
||
| ## | ||
| # Transcodes +character+ to +encoding+ with a +fallback+ character. | ||
|
|
||
| def self.encode_fallback(character, encoding, fallback) | ||
| character.encode(encoding, :fallback => { character => fallback }, | ||
| :undef => :replace, :replace => fallback) | ||
| end | ||
|
|
||
| ## | ||
|
|
||
| # Expands tab characters in +text+ to eight spaces | ||
|
|
||
|
|
@@ -193,95 +165,6 @@ def strip_stars(text) | |
| text.gsub(/^\s+$/, empty) | ||
| end | ||
|
|
||
| def to_html(text) | ||
| to_html_characters(text) | ||
| end | ||
|
|
||
| ## | ||
| # Converts ampersand, dashes, ellipsis, quotes, copyright and registered | ||
| # trademark symbols in +text+ to properly encoded characters. | ||
|
|
||
| def to_html_characters(text) | ||
| html = (''.encode text.encoding).dup | ||
|
|
||
| encoded = RDoc::Text::TO_HTML_CHARACTERS[text.encoding] | ||
|
|
||
| s = StringScanner.new text | ||
| insquotes = false | ||
| indquotes = false | ||
| after_word = nil | ||
|
|
||
| until s.eos? do | ||
| case | ||
| when s.scan(/<(tt|code)>.*?<\/\1>/) then # skip contents of tt | ||
| html << s.matched | ||
| when s.scan(/<(tt|code)>.*?/) then | ||
| warn "mismatched <#{s[1]}> tag" # TODO signal file/line | ||
| html << s.matched | ||
| when s.scan(/<[^>]+\/?s*>/) then # skip HTML tags | ||
| html << s.matched | ||
| when s.scan(/\.\.\.(\.?)/) then | ||
| html << s[1] << encoded[:ellipsis] | ||
| after_word = nil | ||
| when s.scan(/\(c\)/i) then | ||
| html << encoded[:copyright] | ||
| after_word = nil | ||
| when s.scan(/\(r\)/i) then | ||
| html << encoded[:trademark] | ||
| after_word = nil | ||
| when s.scan(/---/) then | ||
| html << encoded[:em_dash] | ||
| after_word = nil | ||
| when s.scan(/--/) then | ||
| html << encoded[:en_dash] | ||
| after_word = nil | ||
| when s.scan(/"|"/) then | ||
| html << encoded[indquotes ? :close_dquote : :open_dquote] | ||
| indquotes = !indquotes | ||
| after_word = nil | ||
| when s.scan(/``/) then # backtick double quote | ||
| html << encoded[:open_dquote] | ||
| after_word = nil | ||
| when s.scan(/(?:'|'){2}/) then # tick double quote | ||
| html << encoded[:close_dquote] | ||
| after_word = nil | ||
| when s.scan(/`/) then # backtick | ||
| if insquotes or after_word | ||
| html << '`' | ||
| after_word = false | ||
| else | ||
| html << encoded[:open_squote] | ||
| insquotes = true | ||
| end | ||
| when s.scan(/'|'/) then # single quote | ||
| if insquotes | ||
| html << encoded[:close_squote] | ||
| insquotes = false | ||
| elsif after_word | ||
| # Mary's dog, my parents' house: do not start paired quotes | ||
| html << encoded[:close_squote] | ||
| else | ||
| html << encoded[:open_squote] | ||
| insquotes = true | ||
| end | ||
|
|
||
| after_word = nil | ||
| else # advance to the next potentially significant character | ||
| match = s.scan(/.+?(?=[<\\.("'`&-])/) #" | ||
|
|
||
| if match then | ||
| html << match | ||
| after_word = match =~ /\w$/ | ||
| else | ||
| html << s.rest | ||
| break | ||
| end | ||
| end | ||
| end | ||
|
|
||
| html | ||
| end | ||
|
|
||
| ## | ||
| # Wraps +txt+ to +line_len+ | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could
TO_HTML_CHARACTERS[quote.encoding]ever beniland causes NoMethodError?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TO_HTML_CHARACTERS = Hash.new do |h, encoding| ... endwill generate a new hash if there is no key.