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
-
#__clear_extensions__ ⇒ self
Clears all registered extensions.
-
#compute_backtrace_entry(entry, cache) ⇒ Object
Computes a backtrace entry with caching.
-
#default_kramdown_options ⇒ Hash
Returns the default Kramdown options used for rendering Markdown.
-
#default_kramdown_options=(opts) ⇒ Hash
Sets the default Kramdown options used for rendering Markdown.
-
#extension(spec) ⇒ self
Registers extensions to the Papercraft syntax.
-
#format_tag_attrs(attrs) ⇒ String
Formats the given hash as tag attributes.
- #make_argument_error(err, backtrace) ⇒ Object
-
#markdown(markdown, **opts) ⇒ String
Renders Markdown into HTML.
-
#markdown_doc(markdown, **opts) ⇒ Kramdown::Document
Returns a Kramdown doc for the given markdown.
-
#translate_backtrace(err) ⇒ Exception
Translates entries in exception’s backtrace to point to original source code.
-
#underscores_to_dashes(tag) ⇒ String
Formats the given string, converting underscores to dashes.
Instance Method Details
#__clear_extensions__ ⇒ self
Clears all registered extensions.
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.
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_options ⇒ Hash
Returns the default Kramdown options used for rendering Markdown.
140 141 142 143 144 145 146 147 |
# File 'lib/papercraft.rb', line 140 def @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.
153 154 155 |
# File 'lib/papercraft.rb', line 153 def (opts) @default_kramdown_options = opts end |
#extension(spec) ⇒ self
Registers extensions to the Papercraft syntax.
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.
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..match(/(given (\d+), expected (\d+))/) if m rectified = format('given %d, expected %d', m[2].to_i - 1, m[3].to_i - 1) = err..gsub(m[1], rectified) else = err. end ArgumentError.new().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.
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.
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 = .merge(opts) Kramdown::Document.new(markdown, **opts) end |
#translate_backtrace(err) ⇒ Exception
Translates entries in exception’s backtrace to point to original source code.
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.
43 44 45 |
# File 'lib/papercraft.rb', line 43 def underscores_to_dashes(tag) tag.to_s.gsub('_', '-') end |