papercraft 0.16 → 0.17
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/README.md +10 -2
- data/lib/papercraft/html.rb +1 -39
- data/lib/papercraft/renderer.rb +2 -0
- data/lib/papercraft/version.rb +1 -1
- data/lib/papercraft.rb +94 -57
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 31c80224c2a9dc3a6d594d923e9a16ccd8127284bce501f1d7f1dc25386bf25c
|
4
|
+
data.tar.gz: 45dc43d353bc1f3c06e62d1b439751570ce7d46a915b0a72f1ce3ea182b0c225
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67529d1336456dded5d911548941c1accc695bd017b889b378ed008e177707b100e11e90b1adbe887d45530cfb94950808e21257955999d795b388dbeb13206e
|
7
|
+
data.tar.gz: 1caca448eb2a317b1616a529e82803f6571e26e8b0f36a19e771b2b4a9a96cabb8695ad5a01148acfa7481b016742e9c546c794fd2a9017ec54d299184d36c94
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -358,6 +358,14 @@ template = Papercraft.html { |md| div { emit_markdown md, auto_ids: false } }
|
|
358
358
|
template.render("# title") #=> "<div><h1>title</h1></div>"
|
359
359
|
```
|
360
360
|
|
361
|
+
The `#emit_markdown` method is available only to HTML templates. If you need to
|
362
|
+
render markdown in XML or JSON templates (usually for implementing RSS or JSON
|
363
|
+
feeds), you can use `Papercraft.markdown` directly:
|
364
|
+
|
365
|
+
```ruby
|
366
|
+
Papercraft.markdown('# title') #=> "<h1>title</h1>"
|
367
|
+
```
|
368
|
+
|
361
369
|
The default Kramdown options are:
|
362
370
|
|
363
371
|
```ruby
|
@@ -370,10 +378,10 @@ The default Kramdown options are:
|
|
370
378
|
```
|
371
379
|
|
372
380
|
The deafult options can be configured by accessing
|
373
|
-
`Papercraft
|
381
|
+
`Papercraft.default_kramdown_options`, e.g.:
|
374
382
|
|
375
383
|
```ruby
|
376
|
-
Papercraft
|
384
|
+
Papercraft.default_kramdown_options[:auto_ids] = false
|
377
385
|
```
|
378
386
|
|
379
387
|
## Deferred evaluation
|
data/lib/papercraft/html.rb
CHANGED
@@ -1,9 +1,5 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require 'kramdown'
|
4
|
-
require 'rouge'
|
5
|
-
require 'kramdown-parser-gfm'
|
6
|
-
|
7
3
|
module Papercraft
|
8
4
|
# HTML Markup extensions
|
9
5
|
module HTML
|
@@ -83,41 +79,7 @@ module Papercraft
|
|
83
79
|
# @param **opts [Hash] Kramdown options
|
84
80
|
# @return [void]
|
85
81
|
def emit_markdown(markdown, **opts)
|
86
|
-
emit
|
87
|
-
end
|
88
|
-
|
89
|
-
class << self
|
90
|
-
# Returns the default Kramdown options used for converting Markdown to
|
91
|
-
# HTML.
|
92
|
-
#
|
93
|
-
# @return [Hash] Default Kramdown options
|
94
|
-
def kramdown_options
|
95
|
-
@kramdown_options ||= {
|
96
|
-
entity_output: :numeric,
|
97
|
-
syntax_highlighter: :rouge,
|
98
|
-
input: 'GFM',
|
99
|
-
hard_wrap: false
|
100
|
-
}
|
101
|
-
end
|
102
|
-
|
103
|
-
# Sets the default Kramdown options used for converting Markdown to
|
104
|
-
# HTML.
|
105
|
-
#
|
106
|
-
# @param opts [Hash] New deafult Kramdown options
|
107
|
-
# @return [Hash] New default Kramdown options
|
108
|
-
def kramdown_options=(opts)
|
109
|
-
@kramdown_options = opts
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
private
|
114
|
-
|
115
|
-
# Returns the default Kramdown options, merged with the given overrides.
|
116
|
-
#
|
117
|
-
# @param opts [Hash] Kramdown option overrides
|
118
|
-
# @return [Hash] Merged Kramdown options
|
119
|
-
def kramdown_options(opts)
|
120
|
-
HTML.kramdown_options.merge(**opts)
|
82
|
+
emit Papercraft.markdown(markdown, **opts)
|
121
83
|
end
|
122
84
|
end
|
123
85
|
end
|
data/lib/papercraft/renderer.rb
CHANGED
data/lib/papercraft/version.rb
CHANGED
data/lib/papercraft.rb
CHANGED
@@ -1,73 +1,110 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'kramdown'
|
4
|
+
require 'rouge'
|
5
|
+
require 'kramdown-parser-gfm'
|
4
6
|
|
5
7
|
require_relative 'papercraft/component'
|
6
8
|
require_relative 'papercraft/renderer'
|
7
9
|
require_relative 'papercraft/encoding'
|
8
10
|
# require_relative 'papercraft/compiler'
|
9
11
|
|
12
|
+
|
10
13
|
# Papercraft is a component-based HTML templating library
|
11
14
|
module Papercraft
|
12
15
|
# Exception class used to signal templating-related errors
|
13
16
|
class Error < RuntimeError; end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
|
20
|
+
# Installs one or more extensions. Extensions enhance templating capabilities
|
21
|
+
# by adding namespaced methods to emplates. An extension is implemented as a
|
22
|
+
# Ruby module containing one or more methods. Each method in the extension
|
23
|
+
# module can be used to render a specific HTML element or a set of elements.
|
24
|
+
#
|
25
|
+
# This is a convenience method. For more information on using Papercraft
|
26
|
+
# extensions, see `Papercraft::Renderer::extension`
|
27
|
+
#
|
28
|
+
# @param map [Hash] hash mapping methods to extension modules
|
29
|
+
# @return [void]
|
30
|
+
def extension(map)
|
31
|
+
Renderer.extension(map)
|
32
|
+
end
|
33
|
+
|
34
|
+
# Creates a new papercraft component. `Papercraft.html` can take either a proc
|
35
|
+
# argument or a block. In both cases, the proc is converted to a
|
36
|
+
# `Papercraft::Component`.
|
37
|
+
#
|
38
|
+
# Papercraft.html(proc { h1 'hi' }).render #=> "<h1>hi</h1>"
|
39
|
+
# Papercraft.html { h1 'hi' }.render #=> "<h1>hi</h1>"
|
40
|
+
#
|
41
|
+
# @param template [Proc] template block
|
42
|
+
# @return [Papercraft::Component] Papercraft component
|
43
|
+
def html(o = nil, mime_type: nil, &template)
|
44
|
+
return o if o.is_a?(Papercraft::Component)
|
45
|
+
template ||= o
|
46
|
+
Papercraft::Component.new(mode: :html, mime_type: mime_type, &template)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Creates a new papercraft component in XML mode. `Papercraft.xml` can take
|
50
|
+
# either a proc argument or a block. In both cases, the proc is converted to a
|
51
|
+
# `Papercraft::Component`.
|
52
|
+
#
|
53
|
+
# Papercraft.xml(proc { item 'foo' }).render #=> "<item>foo</item>"
|
54
|
+
# Papercraft.xml { item 'foo' }.render #=> "<item>foo</item>"
|
55
|
+
#
|
56
|
+
# @param template [Proc] template block
|
57
|
+
# @return [Papercraft::Component] Papercraft component
|
58
|
+
def xml(o = nil, mime_type: nil, &template)
|
59
|
+
return o if o.is_a?(Papercraft::Component)
|
60
|
+
template ||= o
|
61
|
+
Papercraft::Component.new(mode: :xml, mime_type: mime_type, &template)
|
62
|
+
end
|
63
|
+
|
64
|
+
# Creates a new papercraft component in JSON mode. `Papercraft.json` can take
|
65
|
+
# either a proc argument or a block. In both cases, the proc is converted to a
|
66
|
+
# `Papercraft::Component`.
|
67
|
+
#
|
68
|
+
# Papercraft.json(proc { item 42 }).render #=> "[42]"
|
69
|
+
# Papercraft.json { foo 'bar' }.render #=> "{\"foo\": \"bar\"}"
|
70
|
+
#
|
71
|
+
# @param template [Proc] template block
|
72
|
+
# @return [Papercraft::Component] Papercraft component
|
73
|
+
def json(o = nil, mime_type: nil, &template)
|
74
|
+
return o if o.is_a?(Papercraft::Component)
|
75
|
+
template ||= o
|
76
|
+
Papercraft::Component.new(mode: :json, mime_type: mime_type, &template)
|
77
|
+
end
|
14
78
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
# @return [Papercraft::Component] Papercraft component
|
38
|
-
def self.html(o = nil, mime_type: nil, &template)
|
39
|
-
return o if o.is_a?(Papercraft::Component)
|
40
|
-
template ||= o
|
41
|
-
Papercraft::Component.new(mode: :html, mime_type: mime_type, &template)
|
42
|
-
end
|
43
|
-
|
44
|
-
# Creates a new papercraft component in XML mode. `Papercraft.xml` can take
|
45
|
-
# either a proc argument or a block. In both cases, the proc is converted to a
|
46
|
-
# `Papercraft::Component`.
|
47
|
-
#
|
48
|
-
# Papercraft.xml(proc { item 'foo' }).render #=> "<item>foo</item>"
|
49
|
-
# Papercraft.xml { item 'foo' }.render #=> "<item>foo</item>"
|
50
|
-
#
|
51
|
-
# @param template [Proc] template block
|
52
|
-
# @return [Papercraft::Component] Papercraft component
|
53
|
-
def self.xml(o = nil, mime_type: nil, &template)
|
54
|
-
return o if o.is_a?(Papercraft::Component)
|
55
|
-
template ||= o
|
56
|
-
Papercraft::Component.new(mode: :xml, mime_type: mime_type, &template)
|
57
|
-
end
|
79
|
+
# Renders Markdown into HTML. The `opts` argument will be merged with the
|
80
|
+
# default Kramdown options in order to change the rendering behaviour.
|
81
|
+
#
|
82
|
+
# @param markdown [String] Markdown
|
83
|
+
# @param **opts [Hash] Kramdown option overrides
|
84
|
+
# @return [String] HTML
|
85
|
+
def markdown(markdown, **opts)
|
86
|
+
opts = default_kramdown_options.merge(opts)
|
87
|
+
Kramdown::Document.new(markdown, **opts).to_html
|
88
|
+
end
|
89
|
+
|
90
|
+
# Returns the default Kramdown options used for rendering Markdown.
|
91
|
+
#
|
92
|
+
# @return [Hash] Kramdown options
|
93
|
+
def default_kramdown_options
|
94
|
+
@default_kramdown_options ||= {
|
95
|
+
entity_output: :numeric,
|
96
|
+
syntax_highlighter: :rouge,
|
97
|
+
input: 'GFM',
|
98
|
+
hard_wrap: false
|
99
|
+
}
|
100
|
+
end
|
58
101
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
# @param template [Proc] template block
|
67
|
-
# @return [Papercraft::Component] Papercraft component
|
68
|
-
def self.json(o = nil, mime_type: nil, &template)
|
69
|
-
return o if o.is_a?(Papercraft::Component)
|
70
|
-
template ||= o
|
71
|
-
Papercraft::Component.new(mode: :json, mime_type: mime_type, &template)
|
102
|
+
# Sets the default Kramdown options used for rendering Markdown.
|
103
|
+
#
|
104
|
+
# @param opts [Hash] Kramdown options
|
105
|
+
# @return [void]
|
106
|
+
def default_kramdown_options=(opts)
|
107
|
+
@default_kramdown_options = opts
|
108
|
+
end
|
72
109
|
end
|
73
110
|
end
|