Module: Markascend

Defined in:
lib/markascend.rb,
lib/markascend/env.rb,
lib/markascend/macro.rb,
lib/markascend/parser.rb,
lib/markascend/line_unit.rb,
lib/markascend/builtin_macros.rb,
lib/markascend/popular_company_macros.rb

Defined Under Namespace

Classes: Env, LineUnit, Macro, Parser

Constant Summary collapse

VERSION =
'0.1'
DEFAULT_MACROS =
Hash.[] %w[
  del underline sub sup
  img html slim
  csv headless_csv
  latex
  options hi
  dot

  twitter weibo
  wiki
  gist
  video
].map{|k| [k, "parse_#{k}"]}
SANDBOX_MACROS =
DEFAULT_MACROS.dup.delete_if do |k, v|
  %w[html slim options dot].include? k
end
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

%w[
  inline_code
  math
  auto_link
  macro
  link
  bold_italic
  char
].map{|k| "parse_#{k}"}

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.inline_parsersObject

Returns the value of attribute inline_parsers.



64
65
66
# File 'lib/markascend.rb', line 64

def inline_parsers
  @inline_parsers
end

.macrosObject

Returns the value of attribute macros.



64
65
66
# File 'lib/markascend.rb', line 64

def macros
  @macros
end

Class Method Details

.compile(src, opts = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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) ⇒ Object

escape string so that the result can be placed inside double-quoted value of a tag property



72
73
74
75
# File 'lib/markascend.rb', line 72

def escape_attr s
  # http://www.w3.org/TR/html5/syntax.html#attributes-0
  s ? (s.gsub /"/, '&quot;') : ''
end

.escape_html(s) ⇒ Object

escape html



67
68
69
# File 'lib/markascend.rb', line 67

def escape_html s
  CGI.escape_html s
end

.escape_pre(s) ⇒ Object

escape string so that the result can be placed inside a ‘<pre>` tag



78
79
80
# File 'lib/markascend.rb', line 78

def escape_pre s
  s.gsub(/(<)|(>)|&/){$1 ? '&lt;' : $2 ? '&gt;' : '&amp;'}
end

.hilite(s, lang, inline = false) ⇒ Object

syntax hilite s with lang



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# 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) ⇒ Object

detect mime type of the buffer



100
101
102
103
104
105
# File 'lib/markascend.rb', line 100

def mime buffer
  fm = FileMagic.new FileMagic::MAGIC_MIME_TYPE
  res = fm.buffer buffer
  fm.close
  res
end

.strip_tags(s) ⇒ Object

strip tags from s



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/markascend.rb', line 108

def strip_tags s
  # deal with html tags only
  s.gsub(/
    \<\s*script\b
      (?:
        (["']).*?\1|[^\>] # properties
      )*
    \>
    .*?
    \<\s*\/\s*script\s*\>
  /x, '').gsub(/
    \<\s*(?:\/\s*)?
      \w+\b               # tag name, no need to care xml namespace
      (?:
        (["']).*?\1|[^\>] # properties
      )*
    \>
  /x, '')
end