Class: Minidown::TextElement
- Defined in:
- lib/minidown/elements/text_element.rb
Constant Summary collapse
- EscapeChars =
%w{# > * + \- ` _ { } ( ) . ! \[ \] ~}
- EscapeRegexp =
/(?<!\\)\\([#{EscapeChars.join '|'}|\\])/
- Regexp =
{ tag: /(\\*)<(.+?)(\\*)>/, quot: /"/, link: /(?<!\!)\[(.+?)\]\((.+?)\)/, link_title: /((?<=").+?(?="))/, link_url: /(\S+)/, link_ref: /(?<!\!)\[(.+?)\]\s*\[(.*?)\]/, image: /\!\[(.+?)\]\((.+?)\)/, image_ref: /\!\[(.+?)\]\s*\[(.*?)\]/, star: /((?<!\\)\*{1,2})(.+?)\1/, underline: /(?<=\A|\s)((?<!\\)\_{1,2})(\S+)\1(?=\z|\s)/, delete_line: /(?<!\\)~~(?!\s)(.+?)(?<!\s)~~/, quotlink: /\<(.+?)\>/, link_scheme: /\A\S+?\:\/\//, email: /\A[A-Za-z0-9]+@[A-Za-z0-9]+\.[A-Za-z0-9]+/, auto_email: /(?<!\S)[A-Za-z0-9]+@[A-Za-z0-9]+\.[A-Za-z0-9]+(?!\S)/, auto_link: /(?<!\S)\w+?\:\/\/.+?(?!\S)/, inline_code: /(?<!\\)(`+)\s*(.+?)\s*(?<!\\)\1/ }.freeze
Instance Attribute Summary collapse
-
#convert ⇒ Object
Returns the value of attribute convert.
-
#escape ⇒ Object
Returns the value of attribute escape.
-
#sanitize ⇒ Object
Returns the value of attribute sanitize.
Attributes inherited from Element
Instance Method Summary collapse
- #content ⇒ Object
- #convert_str(str) ⇒ Object
- #escape_content!(str) ⇒ Object
- #escape_html(str) ⇒ Object
- #escape_str!(str) ⇒ Object
-
#initialize(*_) ⇒ TextElement
constructor
A new instance of TextElement.
- #paragraph ⇒ Object
- #parse ⇒ Object
- #to_html ⇒ Object
Methods inherited from Element
#blank?, #raw_content, #raw_content=, #unparsed_lines
Methods included from HtmlHelper
Constructor Details
#initialize(*_) ⇒ TextElement
Returns a new instance of TextElement.
28 29 30 31 32 33 |
# File 'lib/minidown/elements/text_element.rb', line 28 def initialize *_ super @escape = true @sanitize = false @convert = true end |
Instance Attribute Details
#convert ⇒ Object
Returns the value of attribute convert.
26 27 28 |
# File 'lib/minidown/elements/text_element.rb', line 26 def convert @convert end |
#escape ⇒ Object
Returns the value of attribute escape.
26 27 28 |
# File 'lib/minidown/elements/text_element.rb', line 26 def escape @escape end |
#sanitize ⇒ Object
Returns the value of attribute sanitize.
26 27 28 |
# File 'lib/minidown/elements/text_element.rb', line 26 def sanitize @sanitize end |
Instance Method Details
#content ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/minidown/elements/text_element.rb', line 39 def content str = super str = convert_str(str) if convert escape_content! str escape_str! str escape_html str if sanitize str end |
#convert_str(str) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/minidown/elements/text_element.rb', line 77 def convert_str str #auto link str.gsub! Regexp[:auto_link] do |origin_str| build_tag 'a'.freeze, href: origin_str do |a| a << origin_str end end #auto email str.gsub! Regexp[:auto_email] do |origin_str| build_tag 'a'.freeze, href: "mailto:#{origin_str}" do |a| a << origin_str end end #parse <link> str.gsub! Regexp[:quotlink] do |origin_str| link = $1 attr = case link when Regexp[:link_scheme] {href: link} when Regexp[:email] {href: "mailto:#{link}"} end attr ? build_tag('a'.freeze, attr){|a| a << link} : origin_str end #parse * _ Regexp.values_at(:star, :underline).each do |regex| str.gsub! regex do |origin_str| tag_name = $1.size > 1 ? 'strong'.freeze : 'em'.freeze build_tag tag_name do |tag| tag << $2 end end end #parse ~~del~~ str.gsub! Regexp[:delete_line] do |origin_str| build_tag 'del'.freeze do |tag| tag << $1 end end #convert image reference str.gsub! Regexp[:image_ref] do |origin_str| alt = $1 id = ($2 && !$2.empty?) ? $2 : $1 ref = doc.links_ref[id.downcase] if ref attr = {src: ref[:url], alt: alt} attr[:title] = ref[:title] if ref[:title] && !ref[:title].empty? build_tag 'img'.freeze, attr else origin_str end end #convert image syntax str.gsub! Regexp[:image] do alt, url = $1, $2 url =~ Regexp[:link_title] title = $1 url =~ Regexp[:link_url] url = $1 attr = {src: url, alt: alt} attr[:title] = title if title build_tag 'img'.freeze, attr end #convert link reference str.gsub! Regexp[:link_ref] do |origin_str| text = $1 id = ($2 && !$2.empty?) ? $2 : $1 ref = doc.links_ref[id.downcase] if ref attr = {href: ref[:url]} attr[:title] = ref[:title] if ref[:title] && !ref[:title].empty? build_tag 'a'.freeze, attr do |a| a << text end else origin_str end end #convert link syntax str.gsub! Regexp[:link] do text, url = $1, $2 url =~ Regexp[:link_title] title = $1 url =~ Regexp[:link_url] url = $1 attr = {href: url} attr[:title] = title if title build_tag 'a'.freeze, attr do |content| content << text end end escape_content! str #inline code str.gsub! Regexp[:inline_code] do |origin_str| build_tag 'code'.freeze do |code| code << escape_html($2) end end escape_str! str @escape = false str end |
#escape_content!(str) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/minidown/elements/text_element.rb', line 56 def escape_content! str return str unless @escape escape_html str str.gsub! Regexp[:tag] do left, tag, right = $1, $2, $3 tag.gsub! Regexp[:quot] do '"'.freeze end left = left.size.odd? ? "#{left[0..-2]}<" : "#{left}<" if left left ||= "<".freeze right = right.size.odd? ? "#{right[0..-2]}>" : "#{right}>" if right right ||= ">".freeze "#{left}#{tag}#{right}" end str end |
#escape_html(str) ⇒ Object
52 53 54 |
# File 'lib/minidown/elements/text_element.rb', line 52 def escape_html str str.replace Utils.escape_html(str) end |
#escape_str!(str) ⇒ Object
48 49 50 |
# File 'lib/minidown/elements/text_element.rb', line 48 def escape_str! str str.gsub!(EscapeRegexp, '\\1'.freeze) if escape end |
#paragraph ⇒ Object
190 191 192 |
# File 'lib/minidown/elements/text_element.rb', line 190 def paragraph ParagraphElement.new doc, raw_content end |
#parse ⇒ Object
35 36 37 |
# File 'lib/minidown/elements/text_element.rb', line 35 def parse nodes << self end |
#to_html ⇒ Object
194 195 196 |
# File 'lib/minidown/elements/text_element.rb', line 194 def to_html content end |