Module: Papercraft

Extended by:
Compiler::AuxMethods
Defined in:
lib/papercraft.rb,
lib/papercraft/xml.rb,
lib/papercraft/html.rb,
lib/papercraft/json.rb,
lib/papercraft/tags.rb,
lib/papercraft/version.rb,
lib/papercraft/renderer.rb,
lib/papercraft/template.rb,
lib/papercraft/compiler_old.rb,
lib/papercraft/extension_proxy.rb,
lib/papercraft/extensions/soap.rb

Overview

Papercraft is a composable templating library

Defined Under Namespace

Modules: Extensions, HTML, JSON, Tags, XML Classes: Compiler, CompilerOld, Error, ExtensionProxy, HTMLRenderer, JSONRenderer, Renderer, Template, XMLRenderer

Constant Summary collapse

VERSION =
'1.3'

Class Method Summary collapse

Methods included from Compiler::AuxMethods

format_html_attr, format_html_attrs, render_emit_call

Class Method Details

.__cache_compiled_template__(value) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/papercraft/compiler_old.rb', line 7

def self.__cache_compiled_template__(value)
  @compiled_template_cache ||= {}
  
  if !(compiled = @compiled_template_cache[value])
    value = Papercraft.html(&value) if value.is_a?(Proc)
    compiled = value.compile.to_proc
    @compiled_template_cache[value] = compiled
  end
end

.__emit__(value, __buffer__, *args) ⇒ Object



17
18
19
20
21
22
23
24
25
# File 'lib/papercraft/compiler_old.rb', line 17

def self.__emit__(value, __buffer__, *args)
  case value
  when Proc, Papercraft::Template
    compiled = __cache_compiled_template__(value)
    compiled.(__buffer__, *args)
  else
    __buffer__ << CGI.escapeHTML(value.to_s)
  end
end

.default_kramdown_optionsHash

Returns the default Kramdown options used for rendering Markdown.

Returns:

  • (Hash)

    Kramdown options



92
93
94
95
96
97
98
99
# File 'lib/papercraft.rb', line 92

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

.default_kramdown_options=(opts) ⇒ void

This method returns an undefined value.

Sets the default Kramdown options used for rendering Markdown.

Parameters:

  • opts (Hash)

    Kramdown options



105
106
107
# File 'lib/papercraft.rb', line 105

def default_kramdown_options=(opts)
  @default_kramdown_options = opts
end

.extension(map) ⇒ void

This method returns an undefined value.

Installs one or more extensions. Extensions enhance templating capabilities by adding namespaced methods to emplates. An extension is implemented as a Ruby module containing one or more methods. Each method in the extension module can be used to render a specific HTML element or a set of elements.

This is a convenience method. For more information on using Papercraft extensions, see ‘Papercraft::Renderer::extension`

Parameters:

  • map (Hash)

    hash mapping methods to extension modules



29
30
31
# File 'lib/papercraft.rb', line 29

def extension(map)
  Renderer.extension(map)
end

.html(o = nil, mime_type: nil, &template) ⇒ Papercraft::Template

Creates a new papercraft template. ‘Papercraft.html` can take either a proc argument or a block. In both cases, the proc is converted to a `Papercraft::Template`.

Papercraft.html(proc { h1 ‘hi’ }).render #=> “<h1>hi</h1>” Papercraft.html { h1 ‘hi’ }.render #=> “<h1>hi</h1>”

Parameters:

  • template (Proc)

    template block

Returns:



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

def html(o = nil, mime_type: nil, &template)
  return o if o.is_a?(Papercraft::Template)
  template ||= o
  Papercraft::Template.new(mode: :html, mime_type: mime_type, &template)
end

.json(o = nil, mime_type: nil, &template) ⇒ Papercraft::Template

Creates a new Papercraft template in JSON mode. ‘Papercraft.json` can take either a proc argument or a block. In both cases, the proc is converted to a `Papercraft::Template`.

Papercraft.json(proc { item 42 }).render #=> “[42]” Papercraft.json { foo ‘bar’ }.render #=> “"bar"”

Parameters:

  • template (Proc)

    template block

Returns:



72
73
74
75
76
# File 'lib/papercraft.rb', line 72

def json(o = nil, mime_type: nil, &template)
  return o if o.is_a?(Papercraft::Template)
  template ||= o
  Papercraft::Template.new(mode: :json, mime_type: mime_type, &template)
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



84
85
86
87
# File 'lib/papercraft.rb', line 84

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

.xml(o = nil, mime_type: nil, &template) ⇒ Papercraft::Template

Creates a new Papercraft template in XML mode. ‘Papercraft.xml` can take either a proc argument or a block. In both cases, the proc is converted to a `Papercraft::Template`.

Papercraft.xml(proc { item ‘foo’ }).render #=> “<item>foo</item>” Papercraft.xml { item ‘foo’ }.render #=> “<item>foo</item>”

Parameters:

  • template (Proc)

    template block

Returns:



57
58
59
60
61
# File 'lib/papercraft.rb', line 57

def xml(o = nil, mime_type: nil, &template)
  return o if o.is_a?(Papercraft::Template)
  template ||= o
  Papercraft::Template.new(mode: :xml, mime_type: mime_type, &template)
end