File: commonmarker.rb

package info (click to toggle)
ruby-tilt 2.7.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 632 kB
  • sloc: ruby: 4,975; makefile: 7
file content (136 lines) | stat: -rw-r--r-- 3,481 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
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
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
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
# frozen_string_literal: true
#
# = Markdown (<tt>markdown</tt>, <tt>md</tt>, <tt>mkd</tt>)
#
# Markdown is a lightweight markup language, created by John Gruber
# and Aaron Swartz. For any markup that is not covered by Markdown’s syntax, HTML
# is used.  Marking up plain text with Markdown markup is easy and Markdown
# formatted texts are readable.
#
# === Example
#
#     Hello Markdown Templates
#     ========================
#
#     Hello World. This is a paragraph.
#
# === Usage
#
# To wrap a Markdown formatted document with a layout:
#
#     layout = Tilt['erb'].new do
#       "<!doctype html><title></title><%= yield %>"
#     end
#     data = Tilt['md'].new { "# hello tilt" }
#     layout.render { data.render }
#     # => "<!doctype html><title></title><h1>hello tilt</h1>\n"
#
# === Options
#
# ==== <tt>:smartypants => true|false</tt>
#
# Set <tt>true</tt> to enable [Smarty Pants][smartypants] style punctuation replacement.
#
# ==== <tt>:escape_html => true|false</tt>
#
# Set <tt>true</tt> disallow raw HTML in Markdown contents. HTML is converted to
# literal text by escaping <tt><</tt> characters.
#
# === See also
#
# * {Markdown Syntax Documentation}[http://daringfireball.net/projects/markdown/syntax]

require_relative 'template'
require 'commonmarker'

if defined?(::Commonmarker)
  aliases = {
    :smartypants => :smart
  }.freeze
  parse_opts = [
    :smart,
    :default_info_string,
  ].freeze
  render_opts = [
    :hardbreaks,
    :github_pre_lang,
    :width,
    :unsafe,
    :escape,
    :sourcepos,
  ].freeze
  exts = [
    :strikethrough,
    :tagfilter,
    :table,
    :autolink,
    :tasklist,
    :superscript,
    :header_ids,
    :footnotes,
    :description_lists,
    :front_matter_delimiter,
    :shortcodes,
  ].freeze

  Tilt::CommonMarkerTemplate = Tilt::StaticTemplate.subclass do
    parse_options = @options.select { |key, _| parse_opts.include?(key.downcase) }.transform_keys(&:downcase)
    parse_options.merge!(@options.select { |key, _| aliases.has_key?(key) }.transform_keys { |key| aliases[key] })
    render_options = @options.select { |key, _| render_opts.include?(key.downcase) }.transform_keys(&:downcase)
    extensions = @options.select { |key, _| exts.include?(key) }.transform_keys(&:downcase)

    Commonmarker.to_html(@data, options: { parse: parse_options, render: render_options, extension: extensions })
  end
# :nocov:
else
  aliases = {
    :smartypants => :SMART
  }.freeze
  parse_opts = [
    :FOOTNOTES,
    :LIBERAL_HTML_TAG,
    :SMART,
    :smartypants,
    :STRIKETHROUGH_DOUBLE_TILDE,
    :UNSAFE,
    :VALIDATE_UTF8,
  ].freeze
  render_opts = [
    :FOOTNOTES,
    :FULL_INFO_STRING,
    :GITHUB_PRE_LANG,
    :HARDBREAKS,
    :NOBREAKS,
    :SAFE, # Removed in v0.18.0 (2018-10-17)
    :SOURCEPOS,
    :TABLE_PREFER_STYLE_ATTRIBUTES,
    :UNSAFE,
  ].freeze
  exts = [
    :autolink,
    :strikethrough,
    :table,
    :tagfilter,
    :tasklist,
  ].freeze

  Tilt::CommonMarkerTemplate = Tilt::StaticTemplate.subclass do
    extensions = exts.select do |extension|
      @options[extension]
    end

    parse_options, render_options = [parse_opts, render_opts].map do |opts|
      opts = opts.select do |option|
        @options[option]
      end.map! do |option|
        aliases[option] || option
      end

      opts = :DEFAULT unless opts.any?
      opts
    end

    CommonMarker.render_doc(@data, parse_options, extensions).to_html(render_options, extensions)
  end
end
# :nocov: