Class: Typeout
Constant Summary collapse
- VERSION =
'1.4.5'
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(sanitize = true) ⇒ Object
Class Method Details
.convert(text, sanitize = true) ⇒ Object
8 9 10 |
# File 'lib/typeout.rb', line 8 def self.convert(text, sanitize = true) self.new(text.to_s).to_html(sanitize) end |
Instance Method Details
#archive(text) ⇒ Object
75 76 77 78 |
# File 'lib/typeout.rb', line 75 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
68 69 70 71 72 73 |
# File 'lib/typeout.rb', line 68 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
63 64 65 66 |
# File 'lib/typeout.rb', line 63 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
59 60 61 |
# File 'lib/typeout.rb', line 59 def archive_html(text) text.gsub(/\[html\](.*?)\[\/html\]/mi) { archive(sanitize_html($1)) } end |
#clean_whitespace(text) ⇒ Object
49 50 51 52 53 |
# File 'lib/typeout.rb', line 49 def clean_whitespace(text) text.gsub(/\r\n/, "\n"). gsub(/\r/, "\n"). gsub(/^ +$/, '') end |
#make_breaks(text) ⇒ Object
121 122 123 |
# File 'lib/typeout.rb', line 121 def make_breaks(text) text.gsub(/([^\n])\n(?!\n)/m, "\\1<br />\n") end |
#make_headings(text) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/typeout.rb', line 87 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
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 170 171 172 173 174 175 |
# File 'lib/typeout.rb', line 125 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
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'lib/typeout.rb', line 94 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
117 118 119 |
# File 'lib/typeout.rb', line 117 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
55 56 57 |
# File 'lib/typeout.rb', line 55 def remove_excess_newlines(text) text.gsub(/\n{3,}/, "\n\n") end |
#retrieve_archive(text) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/typeout.rb', line 80 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
41 42 43 44 45 46 47 |
# File 'lib/typeout.rb', line 41 def sanitize_html(text) if @sanitize Sanitize.clean(text, Sanitize::Config::RELAXED) else text end end |
#to_html(sanitize = true) ⇒ 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 38 39 |
# File 'lib/typeout.rb', line 12 def to_html(sanitize = true) @sanitize = sanitize 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 |