Module: Papercraft

Extended by:
Papercraft
Included in:
Papercraft
Defined in:
lib/papercraft.rb,
lib/papercraft/version.rb,
lib/papercraft/compiler.rb,
lib/papercraft/proc_ext.rb,
lib/papercraft/template.rb,
lib/papercraft/compiler/tag_translator.rb,
lib/papercraft/compiler/nodes.rb

Overview

Papercraft is a functional templating library. In Papercraft, templates are expressed as plain Ruby procs.

Defined Under Namespace

Modules: ProcExtensions Classes: BlockInvocationNode, BuiltinNode, Compiler, ConstTagNode, DeferNode, Error, ExtensionTagNode, RawNode, RenderNode, TagNode, TagTranslator, Template, TextNode

Constant Summary collapse

Extensions =

Registry of Papercraft extensions

{
  link_stylesheet: ->(href, **atts) {
    link(rel: "stylesheet", href:, **atts)
  }
}
VERSION =
'2.22'

Instance Method Summary collapse

Instance Method Details

#__clear_extensions__self

Clears all registered extensions.

Returns:

  • (self)


34
35
36
37
# File 'lib/papercraft.rb', line 34

def __clear_extensions__
  Extensions.clear
  self
end

#compute_backtrace_entry(entry, cache) ⇒ Object

Computes a backtrace entry with caching.

Parameters:

  • entry (String)

    backtrace entry

  • cache (Hash)

    cache store mapping compiled filename to source_map



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/papercraft.rb', line 85

def compute_backtrace_entry(entry, cache)
  m = entry.match(/^((\:\:\(.+\:.+\))\:(\d+))/)
  return entry if !m

  fn = m[2]
  line = m[3].to_i
  source_map = cache[fn] ||= Compiler.source_map_store[fn]
  return entry if !source_map

  ref = source_map[line] || "?(#{line})"
  entry.sub(m[1], ref)
end

#default_kramdown_optionsHash

Returns the default Kramdown options used for rendering Markdown.

Returns:

  • (Hash)

    Kramdown options



140
141
142
143
144
145
146
147
# File 'lib/papercraft.rb', line 140

def default_kramdown_options
  @default_kramdown_options ||= {
    entity_output: :numeric,
    syntax_highlighter: :rouge,
    input: 'GFM',
    hard_wrap: false
  }
end

#default_kramdown_options=(opts) ⇒ Hash

Sets the default Kramdown options used for rendering Markdown.

Parameters:

  • opts (Hash)

    Kramdown options

Returns:

  • (Hash)

    Kramdown options



153
154
155
# File 'lib/papercraft.rb', line 153

def default_kramdown_options=(opts)
  @default_kramdown_options = opts
end

#extension(spec) ⇒ self

Registers extensions to the Papercraft syntax.

Parameters:

  • spec (Hash)

    hash mapping symbols to procs

Returns:

  • (self)


26
27
28
29
# File 'lib/papercraft.rb', line 26

def extension(spec)
  Extensions.merge!(spec)
  self
end

#format_tag_attrs(attrs) ⇒ String

Formats the given hash as tag attributes.

Parameters:

  • attrs (Hash)

    input hash

Returns:

  • (String)

    formatted attributes



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/papercraft.rb', line 51

def format_tag_attrs(attrs)
  attrs.each_with_object(+'') do |(k, v), html|
    case v
    when nil, false
    when true
      html << ' ' if !html.empty?
      html << underscores_to_dashes(k)
    else
      html << ' ' if !html.empty?
      v = v.join(' ') if v.is_a?(Array)
      html << "#{underscores_to_dashes(k)}=\"#{v}\""
    end
  end
end

#make_argument_error(err, backtrace) ⇒ Object



98
99
100
101
102
103
104
105
106
107
# File 'lib/papercraft.rb', line 98

def make_argument_error(err, backtrace)
  m = err.message.match(/(given (\d+), expected (\d+))/)
  if m
    rectified = format('given %d, expected %d', m[2].to_i - 1, m[3].to_i - 1)
    message = err.message.gsub(m[1], rectified)
  else
    message = err.message
  end
  ArgumentError.new(message).tap { it.set_backtrace(backtrace) }
end

#markdown(markdown, **opts) ⇒ String

Renders Markdown into HTML. The ‘opts` argument will be merged with the default Kramdown options in order to change the rendering behaviour.

Parameters:

  • markdown (String)

    Markdown

  • opts (Hash)

    Kramdown option overrides

Returns:

  • (String)

    HTML



133
134
135
# File 'lib/papercraft.rb', line 133

def markdown(markdown, **opts)
  markdown_doc(markdown, **opts).to_html
end

#markdown_doc(markdown, **opts) ⇒ Kramdown::Document

Returns a Kramdown doc for the given markdown. The ‘opts` argument will be merged with the default Kramdown options in order to change the rendering behaviour.

Parameters:

  • markdown (String)

    Markdown

  • opts (Hash)

    Kramdown option overrides

Returns:

  • (Kramdown::Document)

    Kramdown document



116
117
118
119
120
121
122
123
124
125
# File 'lib/papercraft.rb', line 116

def markdown_doc(markdown, **opts)
  @markdown_deps_loaded ||= true.tap do
    require 'kramdown'
    require 'rouge'
    require 'kramdown-parser-gfm'
  end

  opts = default_kramdown_options.merge(opts)
  Kramdown::Document.new(markdown, **opts)
end

#translate_backtrace(err) ⇒ Exception

Translates entries in exception’s backtrace to point to original source code.

Parameters:

  • err (Exception)

    raised exception

Returns:

  • (Exception)

    raised exception



70
71
72
73
74
75
76
77
78
79
# File 'lib/papercraft.rb', line 70

def translate_backtrace(err)
  cache = {}
  is_argument_error = err.is_a?(ArgumentError) && err.backtrace[0] =~ /^\:\:/
  backtrace = err.backtrace.map { |e| compute_backtrace_entry(e, cache) }

  return make_argument_error(err, backtrace) if is_argument_error

  err.set_backtrace(backtrace)
  err
end

#underscores_to_dashes(tag) ⇒ String

Formats the given string, converting underscores to dashes.

Parameters:

  • tag (String, Symbol)

    input string

Returns:

  • (String)

    output string



43
44
45
# File 'lib/papercraft.rb', line 43

def underscores_to_dashes(tag)
  tag.to_s.gsub('_', '-')
end