module Markascend
Constants
- DEFAULT_LINE_UNITS
NOTE on the order:
-
link/bold/italic can contain char but link need not interpolate with bold or italic, seems too rare cased
-
bold/italic can interpolate with each other
-
escapes are processed in the char parser, link/bold/italic can use escape chars
-
- DEFAULT_MACROS
- LineUnit
- Macro
inline contains the src for fallback
- SANDBOX_MACROS
- VERSION
Attributes
inline_parsers[RW]
macros[RW]
Public Class Methods
compile(src, opts={})
click to toggle source
# File lib/markascend.rb, line 48 def compile src, opts={} src = src.gsub "\t", ' ' env = Env.new opts res = Parser.new(env, src).parse if env.toc and !env.toc.empty? res = (generate_toc(env.toc) << res) end if env.footnotes and !env.footnotes.empty? res << generate_footnotes(env.footnotes) end res end
escape_attr(s)
click to toggle source
escape string so that the result can be placed inside double-quoted value of a tag property
# File lib/markascend.rb, line 72 def escape_attr s # http://www.w3.org/TR/html5/syntax.html#attributes-0 s ? (s.gsub /"/, '"') : '' end
escape_html(s)
click to toggle source
escape html
# File lib/markascend.rb, line 67 def escape_html s CGI.escape_html s end
escape_pre(s)
click to toggle source
escape string so that the result can be placed inside a ‘<pre>` tag
# File lib/markascend.rb, line 78 def escape_pre s s.gsub(/(<)|(>)|&/){$1 ? '<' : $2 ? '>' : '&'} end
hilite(s, lang, inline=false)
click to toggle source
syntax hilite s with lang
# File lib/markascend.rb, line 83 def hilite s, lang, inline=false if !lang or lang =~ /\A(ma(rkascend)?)?\z/i or !(::Pygments::Lexer.find lang) # TODO ma lexer s = inline ? (escape_html s) : (escape_pre s) else s = Pygments.highlight s, lexer: lang, options: {nowrap: true} end # TODO config class if inline %Q|<code class="highlight">#{s}</code>| else %Q|<pre><code class="highlight">#{s}</code></pre>| end end
mime(buffer)
click to toggle source
detect mime type of the buffer
# File lib/markascend.rb, line 100 def mime buffer fm = FileMagic.new FileMagic::MAGIC_MIME_TYPE res = fm.buffer buffer fm.close res end
Private Class Methods
generate_footnotes(footnotes)
click to toggle source
# File lib/markascend.rb, line 155 def generate_footnotes footnotes res = '<div id="footnotes"><ol>' i = 0 footnotes.each do |abbrev, detail| res << %Q|<li id="footnote-#{i}">#{escape_html detail}</li>| i += 1 end res << "</ol></div>" end
generate_toc(toc)
click to toggle source
# File lib/markascend.rb, line 130 def generate_toc toc res = '<div id="toc"><ol>' levels = toc.values.map(&:first).uniq.sort prev_level = 0 toc.each do |id, (x, header_content)| # x as in "hx" level = levels.index(x) if level >= prev_level (level - prev_level).times do res << "<ol>" end elsif (prev_level - level).times do res << "</ol>" end end prev_level = level title = strip_tags header_content res << %Q{<li><a href="\##{id}">#{title}</a></li>} end prev_level.times do res << "</ol>" end res << "</ol></div>" end