class Markascend::Macro

video

Public Class Methods

generate_csv(content, headless) click to toggle source
# 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) click to toggle source
# 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

Public Instance Methods

parse(name) click to toggle source
# 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() click to toggle source
# File lib/markascend/builtin_macros.rb, line 64
def parse_csv
  Macro.generate_csv content, false
end
parse_dot() click to toggle source
# 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() click to toggle source

embed gist, accepts: |

luikore/737238
gist.github.com/luikore/737238
https://gist.github.com/luikore/737238
# 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() click to toggle source
# File lib/markascend/builtin_macros.rb, line 68
def parse_headless_csv
  Macro.generate_csv content, true
end
parse_hi() click to toggle source
# File lib/markascend/builtin_macros.rb, line 86
def parse_hi
  # TODO validate syntax name
  env.hi = content == 'none' ? nil : content
  ''
end
parse_html() click to toggle source
# File lib/markascend/builtin_macros.rb, line 55
def parse_html
  # TODO sanitize in strict mode
  content
end
parse_img() click to toggle source
# 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 $!.message
    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() click to toggle source
# File lib/markascend/builtin_macros.rb, line 72
def parse_latex
  %Q|<code class="latex">#{content}</code>|
end
parse_options() click to toggle source
# File lib/markascend/builtin_macros.rb, line 76
def parse_options
  yaml = ::YAML.load(content) rescue nil
  if yaml.is_a?(Hash)
    env.options.merge! yaml
  else
    env.warn '\options content should be a yaml hash'
  end
  ''
end
parse_slim() click to toggle source
# File lib/markascend/builtin_macros.rb, line 60
def parse_slim
  ::Slim::Template.new(){content}.render env.scope
end
parse_twitter() click to toggle source

accepts: |

@somebody
# 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() click to toggle source

embed video, calculates embed iframe by urls from various simple formats, but not accept iframe code

# 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() click to toggle source

accepts: |

@somebody
# 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() click to toggle source
# File lib/markascend/popular_company_macros.rb, line 33
def parse_wiki
  %Q|<a href="http://en.wikipedia.org/wiki/#{content}">#{content}</a>|
end