jekyll-haml-markup 0.1.0 → 0.1.1

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
  SHA1:
3
- metadata.gz: aa5a1c4f4f6de2b2eec2d73b30f7710c06a68267
4
- data.tar.gz: 73420ab7f89c64063b39c47269b367e2b5b3a5a7
3
+ metadata.gz: 9259c1159a8de7d6fca744c0315d319a05f373bd
4
+ data.tar.gz: 7fa0e7d351b574d01b63da17c876609afd0b1914
5
5
  SHA512:
6
- metadata.gz: 49f3975c43de103b7073005b5834b17c132a9080378359f58605161ae2eeef454e55115d8edb15b33ef6e3d45b88c8b8f399f2832ac13f69c0ad2a1d711da80b
7
- data.tar.gz: f77d17ed31f404a31160a548edb0f6f2b383b366971d653363df80b95b3cd795189b01b039ba76148c5a5aaf66308d8ca05fcf23514969ae96d44905c554e11f
6
+ metadata.gz: cd80d195acde91bb5f3b7be39801c8fcc8f6cb4324ff72960aea27d1112da2850a2a4c4cc0fa82dd1da73f76b38f67813812e07711e22c35e9103754fdd06f62
7
+ data.tar.gz: 8348ab98a42d8fecade22d853fa20088be0e2f2578b94955d7ea749dc389aba81ac10a1e7a08b6a7660b6ea17c3ccf1e49674eb6a6a31e2e19747f5bfa6e6953
data/.gitignore CHANGED
@@ -6,3 +6,4 @@
6
6
  /pkg/
7
7
  /spec/reports/
8
8
  /tmp/
9
+ *.gem
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- jekyll-haml-markup (0.1.0)
4
+ jekyll-haml-markup (0.1.1)
5
5
  haml (~> 5.0)
6
6
  jekyll (~> 3.7)
7
7
 
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
- # JEKYLL-HAML-MARKUP
1
+ # Jekyll Haml Markup
2
2
 
3
3
  [Jekyll](https://jekyllrb.com) plugin that enables [Haml](http://haml.info) as a markup option for layouts and partials.
4
4
 
5
+ **Note**: This gem overrides a few jekyll methods and it lacks of testing.
6
+
5
7
  ## Installation
6
8
 
7
9
  Add it to your Gemfile:
@@ -14,17 +16,24 @@ gem 'jekyll-haml-markup', group: :jekyll_plugins
14
16
 
15
17
  You just need to save your layout or template with extension `.haml`, replacing any other extension.
16
18
 
17
- The haml markup will be rendered before the [Liquid](http://shopify.github.io/liquid/) parsing, that means that you will need to escape with `\` any line that doesn't match haml syntax. For example:
19
+ The layout haml markup is rendered with hooks before the [Liquid](http://shopify.github.io/liquid/) render phase and modify version of the `include` tag is in charge of the partials.
18
20
 
19
21
  ```haml
20
22
  !!!
21
23
  %html
22
24
  %head
23
- \{% asset style.css %}
25
+ {% assets style.css %}
24
26
  %body
25
- \{{ content }}
27
+ %head
28
+ {% include head.haml %}
29
+ %main
30
+ {{ content }}
31
+ %footer
32
+ {% include footer.haml %}
26
33
  ```
27
34
 
35
+ Front-matter headers are not working on partials.
36
+
28
37
  ## Development
29
38
 
30
39
  I started this gem because I couldn't make work the gem [jekyll-haml](https://github.com/samvincent/jekyll-haml).
@@ -25,7 +25,7 @@ module Jekyll
25
25
  when "lib-haml" then return Jekyll::Haml::Parser.new(@config)
26
26
  end
27
27
  end
28
-
28
+
29
29
  def convert(content)
30
30
  get_processor.convert content
31
31
  end
@@ -1,7 +1,5 @@
1
1
  require 'jekyll/tags/include'
2
2
 
3
- # frozen_string_literal: true
4
-
5
3
  module Jekyll
6
4
  module Tags
7
5
  class IncludeTag < Liquid::Tag
@@ -1,7 +1,7 @@
1
1
  module Jekyll
2
2
  module Haml
3
3
  module Markup
4
- VERSION = "0.1.0"
4
+ VERSION = '0.1.1'
5
5
  end
6
6
  end
7
7
  end
@@ -1,13 +1,28 @@
1
1
  require 'jekyll-haml-markup/version'
2
2
 
3
3
  require 'jekyll'
4
+ require 'haml'
4
5
 
5
6
  require_relative 'jekyll/converters/haml'
6
7
  require_relative 'jekyll/haml/parser'
7
- require_relative 'jekyll/tags/include'
8
8
  require_relative 'jekyll/configuration'
9
- require_relative 'jekyll/convertible'
10
- require_relative 'jekyll/excerpt'
11
- require_relative 'jekyll/haml_renderer'
12
- require_relative 'jekyll/renderer'
13
- require_relative 'jekyll/site'
9
+ require_relative 'jekyll/tags/include'
10
+
11
+
12
+ Jekyll::Hooks.register :site, :post_read do |site|
13
+ site.layouts.each do |name, layout|
14
+ layout.content = ::Haml::Engine.new(layout.content).render if ['.haml'].include? layout.ext
15
+ end
16
+ end
17
+
18
+ Jekyll::Hooks.register :pages, :pre_render do |page|
19
+ page.content = ::Haml::Engine.new(page.content).render if ['.haml'].include? page.ext
20
+ end
21
+
22
+ Jekyll::Hooks.register :posts, :pre_render do |post|
23
+ post.content = ::Haml::Engine.new(post.content).render if ['.haml'].include? post.ext
24
+ end
25
+
26
+ Jekyll::Hooks.register :documents, :pre_render do |document|
27
+ document.content = ::Haml::Engine.new(document.content).render if ['.haml'].include? document.ext
28
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jekyll-haml-markup
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alvaro Faundez
@@ -102,16 +102,9 @@ files:
102
102
  - lib/jekyll-haml-markup/version.rb
103
103
  - lib/jekyll/configuration.rb
104
104
  - lib/jekyll/converters/haml.rb
105
- - lib/jekyll/convertible.rb
106
- - lib/jekyll/excerpt.rb
107
105
  - lib/jekyll/haml.rb
108
- - lib/jekyll/haml/include.rb
109
106
  - lib/jekyll/haml/parser.rb
110
107
  - lib/jekyll/haml_renderer.rb
111
- - lib/jekyll/haml_renderer/file.rb
112
- - lib/jekyll/haml_renderer/table.rb
113
- - lib/jekyll/renderer.rb
114
- - lib/jekyll/site.rb
115
108
  - lib/jekyll/tags/include.rb
116
109
  homepage: https://github.com/afaundez/jekyll-haml
117
110
  licenses:
@@ -1,15 +0,0 @@
1
- require 'jekyll/convertible'
2
-
3
- module Jekyll
4
- module Convertible
5
-
6
- def haml_file?
7
- %w(.haml).include?(ext)
8
- end
9
-
10
- def render_with_haml?
11
- haml_file?
12
- end
13
-
14
- end
15
- end
@@ -1,9 +0,0 @@
1
- require 'jekyll/excerpt'
2
-
3
- module Jekyll
4
- class Excerpt
5
-
6
- def_delegators :render_with_haml?
7
-
8
- end
9
- end
@@ -1,225 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Jekyll
4
- module Tags
5
- class IncludeTagError < StandardError
6
- attr_accessor :path
7
-
8
- def initialize(msg, path)
9
- super(msg)
10
- @path = path
11
- end
12
- end
13
-
14
- class IncludeTag < Liquid::Tag
15
- VALID_SYNTAX = %r!
16
- ([\w-]+)\s*=\s*
17
- (?:"([^"\\]*(?:\\.[^"\\]*)*)"|'([^'\\]*(?:\\.[^'\\]*)*)'|([\w\.-]+))
18
- !x
19
- VARIABLE_SYNTAX = %r!
20
- (?<variable>[^{]*(\{\{\s*[\w\-\.]+\s*(\|.*)?\}\}[^\s{}]*)+)
21
- (?<params>.*)
22
- !x
23
-
24
- def initialize(tag_name, markup, tokens)
25
- super
26
- matched = markup.strip.match(VARIABLE_SYNTAX)
27
- if matched
28
- @file = matched["variable"].strip
29
- @params = matched["params"].strip
30
- else
31
- @file, @params = markup.strip.split(%r!\s+!, 2)
32
- end
33
- validate_params if @params
34
- @tag_name = tag_name
35
- end
36
-
37
- def syntax_example
38
- "{% #{@tag_name} file.ext param='value' param2='value' %}"
39
- end
40
-
41
- def parse_params(context)
42
- params = {}
43
- markup = @params
44
-
45
- while (match = VALID_SYNTAX.match(markup))
46
- markup = markup[match.end(0)..-1]
47
-
48
- value = if match[2]
49
- match[2].gsub(%r!\\"!, '"')
50
- elsif match[3]
51
- match[3].gsub(%r!\\'!, "'")
52
- elsif match[4]
53
- context[match[4]]
54
- end
55
-
56
- params[match[1]] = value
57
- end
58
- params
59
- end
60
-
61
- def validate_file_name(file)
62
- if file !~ %r!^[a-zA-Z0-9_/\.-]+$! || file =~ %r!\./! || file =~ %r!/\.!
63
- raise ArgumentError, <<-MSG
64
- Invalid syntax for include tag. File contains invalid characters or sequences:
65
-
66
- #{file}
67
-
68
- Valid syntax:
69
-
70
- #{syntax_example}
71
-
72
- MSG
73
- end
74
- end
75
-
76
- def validate_params
77
- full_valid_syntax = %r!\A\s*(?:#{VALID_SYNTAX}(?=\s|\z)\s*)*\z!
78
- unless @params =~ full_valid_syntax
79
- raise ArgumentError, <<-MSG
80
- Invalid syntax for include tag:
81
-
82
- #{@params}
83
-
84
- Valid syntax:
85
-
86
- #{syntax_example}
87
-
88
- MSG
89
- end
90
- end
91
-
92
- # Grab file read opts in the context
93
- def file_read_opts(context)
94
- context.registers[:site].file_read_opts
95
- end
96
-
97
- # Render the variable if required
98
- def render_variable(context)
99
- if @file.match(VARIABLE_SYNTAX)
100
- partial = context.registers[:site]
101
- .liquid_renderer
102
- .file("(variable)")
103
- .parse(@file)
104
- partial.render!(context)
105
- end
106
- end
107
-
108
- def tag_includes_dirs(context)
109
- context.registers[:site].includes_load_paths.freeze
110
- end
111
-
112
- def locate_include_file(context, file, safe)
113
- includes_dirs = tag_includes_dirs(context)
114
- includes_dirs.each do |dir|
115
- path = File.join(dir.to_s, file.to_s)
116
- return path if valid_include_file?(path, dir.to_s, safe)
117
- end
118
- raise IOError, could_not_locate_message(file, includes_dirs, safe)
119
- end
120
-
121
- def render(context)
122
- site = context.registers[:site]
123
-
124
- file = render_variable(context) || @file
125
- validate_file_name(file)
126
-
127
- path = locate_include_file(context, file, site.safe)
128
- return unless path
129
-
130
- add_include_to_dependency(site, path, context)
131
-
132
- partial = load_cached_partial(path, context)
133
-
134
- context.stack do
135
- context["include"] = parse_params(context) if @params
136
- begin
137
- partial.render!(context)
138
- rescue Liquid::Error => e
139
- e.template_name = path
140
- e.markup_context = "included " if e.markup_context.nil?
141
- raise e
142
- end
143
- end
144
- end
145
-
146
- def add_include_to_dependency(site, path, context)
147
- if context.registers[:page] && context.registers[:page].key?("path")
148
- site.regenerator.add_dependency(
149
- site.in_source_dir(context.registers[:page]["path"]),
150
- path
151
- )
152
- end
153
- end
154
-
155
- def load_cached_partial(path, context)
156
- context.registers[:cached_partials] ||= {}
157
- cached_partial = context.registers[:cached_partials]
158
-
159
- if cached_partial.key?(path)
160
- cached_partial[path]
161
- else
162
- unparsed_file = context.registers[:site]
163
- .liquid_renderer
164
- .file(path)
165
- begin
166
- cached_partial[path] = unparsed_file.parse(read_file(path, context))
167
- rescue Liquid::Error => e
168
- e.template_name = path
169
- e.markup_context = "included " if e.markup_context.nil?
170
- raise e
171
- end
172
- end
173
- end
174
-
175
- def valid_include_file?(path, dir, safe)
176
- !outside_site_source?(path, dir, safe) && File.file?(path)
177
- end
178
-
179
- def outside_site_source?(path, dir, safe)
180
- safe && !realpath_prefixed_with?(path, dir)
181
- end
182
-
183
- def realpath_prefixed_with?(path, dir)
184
- File.exist?(path) && File.realpath(path).start_with?(dir)
185
- rescue StandardError
186
- false
187
- end
188
-
189
- # This method allows to modify the file content by inheriting from the class.
190
- def read_file(file, context)
191
- File.read(file, file_read_opts(context))
192
- end
193
-
194
- private
195
-
196
- def could_not_locate_message(file, includes_dirs, safe)
197
- message = "Could not locate the included file '#{file}' in any of "\
198
- "#{includes_dirs}. Ensure it exists in one of those directories and"
199
- message + if safe
200
- " is not a symlink as those are not allowed in safe mode."
201
- else
202
- ", if it is a symlink, does not point outside your site source."
203
- end
204
- end
205
- end
206
-
207
- class IncludeRelativeTag < IncludeTag
208
- def tag_includes_dirs(context)
209
- Array(page_path(context)).freeze
210
- end
211
-
212
- def page_path(context)
213
- if context.registers[:page].nil?
214
- context.registers[:site].source
215
- else
216
- current_doc_dir = File.dirname(context.registers[:page]["path"])
217
- context.registers[:site].in_source_dir current_doc_dir
218
- end
219
- end
220
- end
221
- end
222
- end
223
-
224
- Liquid::Template.register_tag("include", Jekyll::Tags::IncludeTag)
225
- Liquid::Template.register_tag("include_relative", Jekyll::Tags::IncludeRelativeTag)
@@ -1,54 +0,0 @@
1
- module Jekyll
2
- class HamlRenderer
3
- class File
4
- def initialize(renderer, filename)
5
- @renderer = renderer
6
- @filename = filename
7
- end
8
-
9
- def parse(content)
10
- measure_time do
11
- @template = ::Haml::Engine.new(content) #, :line_numbers => true)
12
- end
13
-
14
- self
15
- end
16
-
17
- def render(*args)
18
- measure_time do
19
- measure_bytes do
20
- @template.render(*args)
21
- end
22
- end
23
- end
24
-
25
- def render!(*args)
26
- measure_time do
27
- measure_bytes do
28
- @template = @template.render(*args)
29
- end
30
- end
31
- end
32
-
33
- def warnings
34
- [] # @template.warnings
35
- end
36
-
37
- private
38
-
39
- def measure_bytes
40
- yield.tap do |str|
41
- @renderer.increment_bytes(@filename, str.bytesize)
42
- end
43
- end
44
-
45
- def measure_time
46
- before = Time.now
47
- yield
48
- ensure
49
- after = Time.now
50
- @renderer.increment_time(@filename, after - before)
51
- end
52
- end
53
- end
54
- end
@@ -1,94 +0,0 @@
1
- module Jekyll
2
- class HamlRenderer::Table
3
- def initialize(stats)
4
- @stats = stats
5
- end
6
-
7
- def to_s(n = 50)
8
- data = data_for_table(n)
9
- widths = table_widths(data)
10
- generate_table(data, widths)
11
- end
12
-
13
- private
14
-
15
- def generate_table(data, widths)
16
- str = String.new("\n")
17
-
18
- table_head = data.shift
19
- str << generate_row(table_head, widths)
20
- str << generate_table_head_border(table_head, widths)
21
-
22
- data.each do |row_data|
23
- str << generate_row(row_data, widths)
24
- end
25
-
26
- str << "\n"
27
- str
28
- end
29
-
30
- def generate_table_head_border(row_data, widths)
31
- str = String.new("")
32
-
33
- row_data.each_index do |cell_index|
34
- str << "-" * widths[cell_index]
35
- str << "-+-" unless cell_index == row_data.length - 1
36
- end
37
-
38
- str << "\n"
39
- str
40
- end
41
-
42
- def generate_row(row_data, widths)
43
- str = String.new("")
44
-
45
- row_data.each_with_index do |cell_data, cell_index|
46
- str << if cell_index.zero?
47
- cell_data.ljust(widths[cell_index], " ")
48
- else
49
- cell_data.rjust(widths[cell_index], " ")
50
- end
51
-
52
- str << " | " unless cell_index == row_data.length - 1
53
- end
54
-
55
- str << "\n"
56
- str
57
- end
58
-
59
- def table_widths(data)
60
- widths = []
61
-
62
- data.each do |row|
63
- row.each_with_index do |cell, index|
64
- widths[index] = [ cell.length, widths[index] ].compact.max
65
- end
66
- end
67
-
68
- widths
69
- end
70
-
71
- def data_for_table(n)
72
- sorted = @stats.sort_by { |_, file_stats| -file_stats[:time] }
73
- sorted = sorted.slice(0, n)
74
-
75
- table = [%w(Filename Count Bytes Time)]
76
-
77
- sorted.each do |filename, file_stats|
78
- row = []
79
- row << filename
80
- row << file_stats[:count].to_s
81
- row << format_bytes(file_stats[:bytes])
82
- row << format("%.3f", file_stats[:time])
83
- table << row
84
- end
85
-
86
- table
87
- end
88
-
89
- def format_bytes(bytes)
90
- bytes /= 1024.0
91
- format("%.2fK", bytes)
92
- end
93
- end
94
- end
@@ -1,60 +0,0 @@
1
- require 'jekyll/renderer'
2
-
3
- module Jekyll
4
- class Renderer
5
- def render_document
6
- info = {
7
- :registers => { :site => site, :page => payload["page"] },
8
- }
9
- output = document.content
10
-
11
- if document.render_with_haml?
12
- Jekyll.logger.debug "Rendering Haml:", document.relative_path
13
- output = render_haml(output, payload, info, document.path)
14
- end
15
-
16
- if document.render_with_liquid?
17
- Jekyll.logger.debug "Rendering Liquid:", document.relative_path
18
- output = render_liquid(output, payload, info, document.path)
19
- end
20
-
21
- Jekyll.logger.debug "Rendering Markup:", document.relative_path
22
- output = convert(output)
23
- document.content = output
24
-
25
- if document.place_in_layout?
26
- Jekyll.logger.debug "Rendering Layout:", document.relative_path
27
- output = place_in_layouts(output, payload, info)
28
- end
29
-
30
- output
31
- end
32
-
33
- def render_haml(content, payload, info, path = nil)
34
- template = site.haml_renderer.file(path).parse(content)
35
- template.warnings.each do |e|
36
- Jekyll.logger.warn "Haml Warning:",
37
- HamlRenderer.format_error(e, path || document.relative_path)
38
- end
39
- template.render!(payload, info)
40
- end
41
-
42
- private
43
- def render_layout(output, layout, info)
44
- payload["content"] = output
45
- payload["layout"] = Utils.deep_merge_hashes(layout.data, payload["layout"] || {})
46
-
47
- if layout.render_with_haml?
48
- Jekyll.logger.debug "Rendering Haml:", layout.relative_path
49
- layout.content = ::Haml::Engine.new(layout.content).render
50
- end
51
-
52
- render_liquid(
53
- layout.content,
54
- payload,
55
- info,
56
- layout.relative_path
57
- )
58
- end
59
- end
60
- end
data/lib/jekyll/site.rb DELETED
@@ -1,14 +0,0 @@
1
- require 'jekyll/site'
2
-
3
- module Jekyll
4
- class Site
5
- attr_reader :haml_renderer
6
-
7
- alias_method :pre_haml_initialize, :initialize
8
-
9
- def initialize(config)
10
- pre_haml_initialize(config)
11
- @haml_renderer = HamlRenderer.new(self)
12
- end
13
- end
14
- end