Class: Markascend::Macro
- Inherits:
-
Struct
- Object
- Struct
- Markascend::Macro
- Defined in:
- lib/markascend/macro.rb,
lib/markascend/macro.rb,
lib/markascend/builtin_macros.rb,
lib/markascend/popular_company_macros.rb
Overview
video
Instance Attribute Summary collapse
-
#content ⇒ Object
Returns the value of attribute content.
-
#env ⇒ Object
Returns the value of attribute env.
-
#inline ⇒ Object
Returns the value of attribute inline.
Class Method Summary collapse
Instance Method Summary collapse
- #parse(name) ⇒ Object
- #parse_csv ⇒ Object
- #parse_dot ⇒ Object
-
#parse_gist ⇒ Object
embed gist, accepts: | luikore/737238 gist.github.com/luikore/737238 gist.github.com/luikore/737238.
- #parse_headless_csv ⇒ Object
- #parse_hi ⇒ Object
- #parse_html ⇒ Object
- #parse_img ⇒ Object
- #parse_latex ⇒ Object
- #parse_options ⇒ Object
- #parse_slim ⇒ Object
-
#parse_twitter ⇒ Object
accepts: | @somebody.
-
#parse_video ⇒ Object
embed video, calculates embed iframe by urls from various simple formats, but not accept iframe code.
-
#parse_weibo ⇒ Object
accepts: | @somebody.
- #parse_wiki ⇒ Object
Instance Attribute Details
#content ⇒ Object
Returns the value of attribute content
3 4 5 |
# File 'lib/markascend/macro.rb', line 3 def content @content end |
#env ⇒ Object
Returns the value of attribute env
3 4 5 |
# File 'lib/markascend/macro.rb', line 3 def env @env end |
#inline ⇒ Object
Returns the value of attribute inline
3 4 5 |
# File 'lib/markascend/macro.rb', line 3 def inline @inline end |
Class Method Details
.generate_csv(content, headless) ⇒ Object
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 |
# File 'lib/markascend/builtin_macros.rb', line 112 def Macro.generate_csv content, headless rows = ::CSV.parse(content) return if rows.empty? table = "<table>" unless headless table << "<thead>" head = rows.shift head.map!{|e| "<th>#{::Markascend.escape_html e}</th>" } table << "<tr>#{head.join}</tr>" table << "</thead>" end table << "<tbody>" rows.each do |row| row.map!{|e| "<td>#{::Markascend.escape_html e}</td>" } table << "<tr>#{row.join}</tr>" end table << "</tbody></table>" end |
.scan_attr(strscan, attr_name) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/markascend/builtin_macros.rb', line 133 def Macro.scan_attr strscan, attr_name pos = strscan.pos strscan.skip /\s+/ unless strscan.peek(attr_name.size) == attr_name strscan.pos = pos return end strscan.pos += attr_name.size if strscan.scan(/\s*=\s*/) # http://www.w3.org/TR/html5/syntax.html#attributes-0 if v = strscan.scan(/(["']).*?\1/) v[1...-1] else strscan.scan(/\w+/) end else attr_name end end |
Instance Method Details
#parse(name) ⇒ Object
5 6 7 8 9 10 11 |
# File 'lib/markascend/macro.rb', line 5 def parse name self.content ||= '' if meth = env.macros[name] res = send meth end res or (inline ? ::Markascend.escape_html(inline) : "\\#{name}") end |
#parse_csv ⇒ Object
64 65 66 |
# File 'lib/markascend/builtin_macros.rb', line 64 def parse_csv Macro.generate_csv content, false end |
#parse_dot ⇒ Object
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/markascend/builtin_macros.rb', line 92 def parse_dot err, out, code = nil ::Open3.popen3 'dot', '-Tpng' do |i, o, e, t| i.puts content i.close err = e.read out = o.read code = t.value.to_i e.close o.close end if code != 0 env.warn err return end data = ::Base64.strict_encode64 out %Q|<img src="data:image/png;base64,#{data}" alt="#{Markascend.escape_attr content}"/>| end |
#parse_gist ⇒ Object
embed gist, accepts: |
luikore/737238
gist.github.com/luikore/737238
https://gist.github.com/luikore/737238
42 43 44 45 46 47 48 49 50 51 |
# File 'lib/markascend/popular_company_macros.rb', line 42 def parse_gist src = content.strip if src =~ /\A\w+(\-\w+)*\/\d+/ src = "https://gist.github.com/#{src}" else src.sub! /\A(?=gist\.github\.com)/, 'https://' end src.sub!(/((?<!\.js)|\.git)$/, '.js') %Q|<script src="#{src}"></script>| end |
#parse_headless_csv ⇒ Object
68 69 70 |
# File 'lib/markascend/builtin_macros.rb', line 68 def parse_headless_csv Macro.generate_csv content, true end |
#parse_hi ⇒ Object
86 87 88 89 90 |
# File 'lib/markascend/builtin_macros.rb', line 86 def parse_hi # TODO validate syntax name env.hi = content == 'none' ? nil : content '' end |
#parse_html ⇒ Object
55 56 57 58 |
# File 'lib/markascend/builtin_macros.rb', line 55 def parse_html # TODO sanitize in strict mode content end |
#parse_img ⇒ Object
11 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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/markascend/builtin_macros.rb', line 11 def parse_img s = ::StringScanner.new content unless src = s.scan(/(\\\ |\S)+/) env.warn "require src for \\img" return end alt = Macro.scan_attr s, 'alt' s.skip /\s+/ href = Macro.scan_attr s, 'href' s.skip /\s+/ if alt2 = Macro.scan_attr(s, 'alt') alt = alt2 end unless s.eos? env.warn "parse error for content of \\img" return end alt = ::Markascend.escape_attr alt if env.inline_img begin if env.pwd Dir.chdir env.pwd do data = open src, 'rb', &:read end else data = open src, 'rb', &:read end mime = ::Markascend.mime data src = "data:#{mime};base64,#{::Base64.strict_encode64 data}" rescue env.warn $!. end end img = %Q|<img src="#{src}" alt="#{alt}"/>| if href href = ::Markascend.escape_attr href %Q|<a href="#{href}">#{img}</a>| else img end end |
#parse_latex ⇒ Object
72 73 74 |
# File 'lib/markascend/builtin_macros.rb', line 72 def parse_latex %Q|<code class="latex">#{content}</code>| end |
#parse_options ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/markascend/builtin_macros.rb', line 76 def yaml = ::YAML.load(content) rescue nil if yaml.is_a?(Hash) env..merge! yaml else env.warn '\options content should be a yaml hash' end '' end |
#parse_slim ⇒ Object
60 61 62 |
# File 'lib/markascend/builtin_macros.rb', line 60 def parse_slim ::Slim::Template.new(){content}.render env.scope end |
#parse_twitter ⇒ Object
accepts: |
@somebody
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/markascend/popular_company_macros.rb', line 8 def parse_twitter if content.start_with?('@') text = ::Markascend.escape_html content link = "https://twitter.com/#{::Markascend.escape_attr content[1..-1]}" else # TODO embed tweet raise 'not implemented yet' end %Q{<a href="#{link}">#{text}</a>} end |
#parse_video ⇒ Object
embed video, calculates embed iframe by urls from various simple formats, but not accept iframe code
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/markascend/popular_company_macros.rb', line 54 def parse_video # standard unless /\A\s*(?<width>\d+)x(?<height>\d+)\s+(?<url>.+)\z/ =~ content env.warn 'can not parse \video content, should be "#{WIDTH}x#{HEIGHT} #{URL}"' return end case url when /youtu\.?be/ # NOTE merging them into one regexp fails (because longest match?) unless id = url[/(?<=watch\?v=)\w+/] || url[/(?<=embed\/)\w+/] || url[/(?<=youtu\.be\/)\w+/] env.warn 'can not parse youtube id' return end %Q|<iframe width="#{width}" height="#{height}" src="https://www.youtube-nocookie.com/embed/#{id}?rel=0" frameborder="0" allowfullscreen></iframe>| when /vimeo/ unless id = url[/(?<=vimeo\.com\/)\w+/] env.warn 'can not parse vimeo id, should use link like this "http://vimeo.com/#{DIGITS}"' return end %Q|<iframe width="#{width}" height="#{height}" src="https://player.vimeo.com/video/#{id}" frameborder="0" allowFullScreen></iframe>| when /sm/ unless id = url[/\bsm\d+/] env.warn 'can not find "sm#{DIGITS}" from link' return end %Q|<script src="https://ext.nicovideo.jp/thumb_watch/#{id}?w=#{width}&h=#{height}"></script>"| else env.warn 'failed to parse video link, currently only youtube, vimeo and niconico are supported' return end end |
#parse_weibo ⇒ Object
accepts: |
@somebody
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/markascend/popular_company_macros.rb', line 22 def parse_weibo if content.start_with?('@') text = ::Markascend.escape_html content link = "https://weibo.com/#{::Markascend.escape_attr content[1..-1]}" else # TODO embed tweet raise 'not implemented yet' end %Q{<a href="#{link}">#{text}</a>} end |
#parse_wiki ⇒ Object
33 34 35 |
# File 'lib/markascend/popular_company_macros.rb', line 33 def parse_wiki %Q|<a href="http://en.wikipedia.org/wiki/#{content}">#{content}</a>| end |