papercraft 0.16 → 0.17

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 996d95088c85415ca883c27b6b1c98e65af5c046e3121ea08f092ac7bcdcc588
4
- data.tar.gz: 8fe294225c04dcfc82289fd2b9047760260b814712e83e9bf5ea7d361dad6950
3
+ metadata.gz: 31c80224c2a9dc3a6d594d923e9a16ccd8127284bce501f1d7f1dc25386bf25c
4
+ data.tar.gz: 45dc43d353bc1f3c06e62d1b439751570ce7d46a915b0a72f1ce3ea182b0c225
5
5
  SHA512:
6
- metadata.gz: 54fdbe164a4a2541153e57b90cdb35ba71b6887fbe2d199505da3389730f1bd937fe0254f8c019b39d81957058c5f51ad71bc900f324e3178b979f368642ee57
7
- data.tar.gz: fa05ea9ae79a57a3eace2e708b01fe5f44412591875776cf1a2fe95cc6d9d1586357756e702e8a17e5458b3fa4c3e3224290bc234b71fdcd426db0dedff31dc0
6
+ metadata.gz: 67529d1336456dded5d911548941c1accc695bd017b889b378ed008e177707b100e11e90b1adbe887d45530cfb94950808e21257955999d795b388dbeb13206e
7
+ data.tar.gz: 1caca448eb2a317b1616a529e82803f6571e26e8b0f36a19e771b2b4a9a96cabb8695ad5a01148acfa7481b016742e9c546c794fd2a9017ec54d299184d36c94
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 0.17 2022-01-23
2
+
3
+ - Refactor markdown code, add `Papercraft.markdown` method (#8)
4
+
1
5
  ## 0.16 2022-01-23
2
6
 
3
7
  - Implement JSON templating (#7)
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::HTML.kramdown_options`, e.g.:
381
+ `Papercraft.default_kramdown_options`, e.g.:
374
382
 
375
383
  ```ruby
376
- Papercraft::HTML.kramdown_options[:auto_ids] = false
384
+ Papercraft.default_kramdown_options[:auto_ids] = false
377
385
  ```
378
386
 
379
387
  ## Deferred evaluation
@@ -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 Kramdown::Document.new(markdown, **kramdown_options(opts)).to_html
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
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'escape_utils'
4
+
3
5
  require_relative './html'
4
6
  require_relative './json'
5
7
  require_relative './extension_proxy'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Papercraft
4
- VERSION = '0.16'
4
+ VERSION = '0.17'
5
5
  end
data/lib/papercraft.rb CHANGED
@@ -1,73 +1,110 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'escape_utils'
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
- # Installs one or more extensions. Extensions enhance templating capabilities
16
- # by adding namespaced methods to emplates. An extension is implemented as a
17
- # Ruby module containing one or more methods. Each method in the extension
18
- # module can be used to render a specific HTML element or a set of elements.
19
- #
20
- # This is a convenience method. For more information on using Papercraft
21
- # extensions, see `Papercraft::Renderer::extension`
22
- #
23
- # @param map [Hash] hash mapping methods to extension modules
24
- # @return [void]
25
- def self.extension(map)
26
- Renderer.extension(map)
27
- end
28
-
29
- # Creates a new papercraft component. `Papercraft.html` can take either a proc
30
- # argument or a block. In both cases, the proc is converted to a
31
- # `Papercraft::Component`.
32
- #
33
- # Papercraft.html(proc { h1 'hi' }).render #=> "<h1>hi</h1>"
34
- # Papercraft.html { h1 'hi' }.render #=> "<h1>hi</h1>"
35
- #
36
- # @param template [Proc] template block
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
- # Creates a new papercraft component in JSON mode. `Papercraft.json` can take
60
- # either a proc argument or a block. In both cases, the proc is converted to a
61
- # `Papercraft::Component`.
62
- #
63
- # Papercraft.json(proc { item 42 }).render #=> "[42]"
64
- # Papercraft.json { foo 'bar' }.render #=> "{\"foo\": \"bar\"}"
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: papercraft
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.16'
4
+ version: '0.17'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sharon Rosner