Module: P2
- Extended by:
- P2
- Included in:
- P2
- Defined in:
- lib/p2.rb,
lib/p2/version.rb,
lib/p2/compiler.rb,
lib/p2/template.rb,
lib/p2/compiler/tag_translator.rb,
lib/p2/compiler/nodes.rb
Overview
P2 is a functional templating library. In P2, templates are expressed as plain Ruby procs.
Defined Under Namespace
Classes: BlockInvocationNode, BuiltinNode, Compiler, ConstTagNode, DeferNode, Error, ExtensionTagNode, RawNode, RenderNode, TagNode, TagTranslator, Template, TextNode
Constant Summary collapse
- Extensions =
Registry of P2 exgtensions
{}
- VERSION =
'2.13'
Instance Method Summary collapse
-
#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 P2 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.
-
#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
#compute_backtrace_entry(entry, cache) ⇒ Object
Computes a backtrace entry with caching.
73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/p2.rb', line 73 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.
117 118 119 120 121 122 123 124 |
# File 'lib/p2.rb', line 117 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.
130 131 132 |
# File 'lib/p2.rb', line 130 def (opts) @default_kramdown_options = opts end |
#extension(spec) ⇒ self
Registers extensions to the P2 syntax.
22 23 24 25 |
# File 'lib/p2.rb', line 22 def extension(spec) Extensions.merge!(spec) self end |
#format_tag_attrs(attrs) ⇒ String
Formats the given hash as tag attributes.
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/p2.rb', line 39 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
86 87 88 89 90 91 92 93 94 95 |
# File 'lib/p2.rb', line 86 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.
103 104 105 106 107 108 109 110 111 112 |
# File 'lib/p2.rb', line 103 def markdown(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).to_html end |
#translate_backtrace(err) ⇒ Exception
Translates entries in exception’s backtrace to point to original source code.
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/p2.rb', line 58 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.
31 32 33 |
# File 'lib/p2.rb', line 31 def underscores_to_dashes(tag) tag.to_s.gsub('_', '-') end |