Class: Typeout
Constant Summary collapse
- VERSION =
'1.4.2'
Class Method Summary collapse
Instance Method Summary collapse
- #archive(text) ⇒ Object
- #archive_blockquotes(text) ⇒ Object
- #archive_code(text) ⇒ Object
- #archive_html(text) ⇒ Object
- #clean_whitespace(text) ⇒ Object
- #make_breaks(text) ⇒ Object
- #make_headings(text) ⇒ Object
- #make_inlines(text) ⇒ Object
- #make_lists(text, base_level = nil) ⇒ Object
- #make_paragraphs(text) ⇒ Object
- #remove_excess_newlines(text) ⇒ Object
- #retrieve_archive(text) ⇒ Object
- #sanitize_html(text) ⇒ Object
- #to_html ⇒ Object
Class Method Details
.convert(text) ⇒ Object
8 9 10 |
# File 'lib/typeout.rb', line 8 def self.convert(text) self.new(text.to_s).to_html end |
Instance Method Details
#archive(text) ⇒ Object
69 70 71 72 |
# File 'lib/typeout.rb', line 69 def archive(text) @archive << text "!a!r!c!h!i!v!e!#{@archive.length-1}!a!r!c!h!i!v!e!" # nobody's going to type that! end |
#archive_blockquotes(text) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/typeout.rb', line 62 def archive_blockquotes(text) text.gsub(/^~{3,}\n(.+?)\n~{3,}$/m) do content = remove_excess_newlines(make_paragraphs(make_lists("\n\n#{$1}\n\n"))) # blockquotes support paragraphs and lists archive("<blockquote>#{content}</blockquote>") end end |
#archive_code(text) ⇒ Object
57 58 59 60 |
# File 'lib/typeout.rb', line 57 def archive_code(text) text = text.gsub(/^-{3,}\n(.+?)\n-{3,}$/m) { archive("<pre><code>#{$1}</code></pre>") } text.gsub(/`(?!\s)((?:\s*\S)+?)`/) { archive("<code>#{$1}</code>") } end |
#archive_html(text) ⇒ Object
53 54 55 |
# File 'lib/typeout.rb', line 53 def archive_html(text) text.gsub(/\[html\](.*?)\[\/html\]/mi) { archive(sanitize_html($1)) } end |
#clean_whitespace(text) ⇒ Object
43 44 45 46 47 |
# File 'lib/typeout.rb', line 43 def clean_whitespace(text) text.gsub(/\r\n/, "\n"). gsub(/\r/, "\n"). gsub(/^ +$/, '') end |
#make_breaks(text) ⇒ Object
115 116 117 |
# File 'lib/typeout.rb', line 115 def make_breaks(text) text.gsub(/([^\n])\n(?!\n)/m, "\\1<br />\n") end |
#make_headings(text) ⇒ Object
81 82 83 84 85 86 |
# File 'lib/typeout.rb', line 81 def make_headings(text) text.gsub(/^(={1,6})(.+?)=*?$/) do depth = $1.length "\n\n<h#{depth}>#{$2.strip}</h#{depth}>\n\n" end end |
#make_inlines(text) ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/typeout.rb', line 119 def make_inlines(text) text = text. gsub(/^((?: ?[\w\(\)']){3,}:) (?!\s*$)/, '<b>\1</b> '). # spaces are not allowed directly in front of the colon, there must be a space after it, and the rest of the line can't be blank gsub(/!\((.+?)\)(?:\:([a-zA-Z0-9\_]+))?/) do if $2 "<img src=\"#{archive($1)}\" class=\"#{$2}\" />" else "<img src=\"#{archive($1)}\" />" end end. gsub(/\[(.+?)\]\((.+?)\)(?:\:([a-zA-Z0-9\_]+))?/) do if $3 "<a href=\"#{archive($2)}\" class=\"#{$3}\">#{archive($1)}</a>" else "<a href=\"#{archive($2)}\">#{archive($1)}</a>" end end. gsub(/([a-z0-9\.\-\_\+]+)@((?:[a-z0-9\-]{2,}\.)*)([a-z0-9\-]+\.)(com|org|net|biz|edu|info|gov|co\.uk|co\.us)/i) do "<a href=\"mailto:#{archive($1 + '@' + $2 + $3 + $4)}\">#{archive($1 + '@' + $2 + $3 + $4)}</a>" end. gsub(/(https?:\/\/)?((?:[a-z0-9\-]{2,}\.)*)([a-z0-9\-]+\.)(com|org|net|biz|edu|info|gov|co\.uk|co\.us)((?:\/[a-z0-9\-\._=\?&;\%#]+)*\/?)/i) do if $1.nil? protocol = 'http://' else protocol = $1 end "<a href=\"#{archive(protocol + $2 + $3 + $4 + $5)}\">#{archive(protocol + $2 + $3 + $4 + $5)}</a>" end inlines = [ ['*', 'strong'], ['_', 'em'], ['`', 'code'], ['^', 'sup'], ['~', 'sub'] ] inlines.each do |char, tag| char = Regexp.escape(char) re = /#{char} # the opening character (?!\s) # no spaces directly after the char ( # main capture group (?:\s*\S)+? # because of no lookbehinds, every space must be followed by a non-space ) # with lookbehinds, the whole regex is: \*(?! )(.+?)(?<! )\* #{char}/x # closing character, no spaces directly before it #re = /#{char}(.+?)#{char}/ text.gsub!(re, "<#{tag}>\\1</#{tag}>") end text end |
#make_lists(text, base_level = nil) ⇒ Object
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/typeout.rb', line 88 def make_lists(text, base_level = nil) text.gsub(/^(#{Regexp.escape(base_level.to_s)}[\*\#])[\*\#]* [^\n]+?\n(?:\1[\*\#]* (?:[^\n])+?\n)*/m) do |content| level = $1 content = make_lists(content, level) content = content.gsub(/^#{Regexp.escape(level)} (.+)$/, '<li>\1</li>') content = content.gsub(/<\/li>\n<([uo])l>/m, '<\1l>') if level.last == "*" # its an unordered list content = "<ul>\n#{content}</ul>" else content = "<ol>\n#{content}</ol>" end if base_level content += "</li>\n" else content += "\n" end content end end |
#make_paragraphs(text) ⇒ Object
111 112 113 |
# File 'lib/typeout.rb', line 111 def make_paragraphs(text) text.gsub(/^(?!<|!a!r!c!h!i!v!e!)([^\n]+\n)+\n/m) { |content| "\n\n<p>\n#{make_breaks(content.strip)}</p>\n\n" } end |
#remove_excess_newlines(text) ⇒ Object
49 50 51 |
# File 'lib/typeout.rb', line 49 def remove_excess_newlines(text) text.gsub(/\n{3,}/, "\n\n") end |
#retrieve_archive(text) ⇒ Object
74 75 76 77 78 79 |
# File 'lib/typeout.rb', line 74 def retrieve_archive(text) @archive.each_with_index do |content, index| text = text.sub("!a!r!c!h!i!v!e!#{index}!a!r!c!h!i!v!e!") { content } # the block is so gsub won't substitute \& end text end |
#sanitize_html(text) ⇒ Object
39 40 41 |
# File 'lib/typeout.rb', line 39 def sanitize_html(text) Sanitize.clean(text, Sanitize::Config::RELAXED) end |
#to_html ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/typeout.rb', line 12 def to_html text = self.dup text = clean_whitespace text text = "\n\n#{text}\n\n" # a few guaranteed newlines @archive = [] text = archive_html text text = html_escape text text = archive_code text text = archive_blockquotes text text = make_headings text text = make_lists text text = make_paragraphs text text = make_inlines text text = remove_excess_newlines text text = retrieve_archive text text end |