mr-edward 0.4.0 → 0.6.0

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: 3a9220176669d7089cdf418e72f2694c9155ac9f4f1e7221396cbc313b1b311b
4
- data.tar.gz: 3062475cca120f2246d75ae857c229bbd417d00cd106faaee1ed0c82c52f8535
3
+ metadata.gz: cba75b99f925e933379311f179ad9bfd42de5837b8f620adf73dd9a3a3790bbc
4
+ data.tar.gz: 527c2cc38460e315b16338b82e8bc7ffaeddf15afa0ab2b8e7f92a29903937b7
5
5
  SHA512:
6
- metadata.gz: 60827cee11a1dff53cc072b97d23354ec1df9770fd91df71898caa2824b4cdfbcbe5ada7c5ec5631284fe89c0d21f2f9625c8cfeecf129bc12df96b52c1564bb
7
- data.tar.gz: 39aeb57d0c7c60dd8486f698d02baccddda67ace4f0dacb1b1378ac7fc530d7f307b2b9c585d054376267ee236b94656a2ec2a3603dcfa0a4372784da487ca96
6
+ metadata.gz: 6e50f960d5ac335485ac800ede80e1835927e446bd389261857a2a5288408699957d2f137328d85ba94b2b18c47aee59528f28d1ee2786e8377ba0a1eed0770b
7
+ data.tar.gz: ff50d23ecaf57f3ad070924845f831dc848732133c3e1830c60a1a057a80864ddb4b29ea44debb0e61b43980259b93bba6291f0814a744a57ee9a50f53728ba3
data/src/builder.rb CHANGED
@@ -6,7 +6,7 @@ module Edward
6
6
  # Builds a website
7
7
  class Builder
8
8
 
9
- attr_reader :target
9
+ attr_reader :target, :pages
10
10
 
11
11
  def initialize
12
12
  @target = "_site"
@@ -21,9 +21,14 @@ module Edward
21
21
  FileUtils.rm_r @target if File.exist? @target
22
22
  FileUtils.mkdir_p @target
23
23
 
24
+ @pages = []
25
+
24
26
  Dir.glob("**/*") do |path|
25
27
  visit_file path if visit_file?(path)
26
28
  end
29
+
30
+ write_pages
31
+
27
32
  end
28
33
 
29
34
  def visit_file? path
@@ -37,14 +42,20 @@ module Edward
37
42
  def visit_file path
38
43
  if Page.page?(path)
39
44
  page = Edward::Page.new(path)
40
- puts "converting #{page.path} => #{page.dirname}/#{page.new_name}"
45
+ @pages << page
41
46
  FileUtils.mkdir_p "#{@target}/#{page.dirname}"
42
- File.write("#{@target}/#{page.new_path}", page.render)
43
47
  else
44
48
  copy_plain path
45
49
  end
46
50
  end
47
51
 
52
+ def write_pages
53
+ for page in @pages
54
+ puts "converting #{page.path} => #{page.dirname}/#{page.new_name}"
55
+ File.write("#{@target}/#{page.new_path}", page.render(self))
56
+ end
57
+ end
58
+
48
59
  # simply copy the file
49
60
  def copy_plain path
50
61
  FileUtils.mkdir_p "#{@target}/#{File.dirname path}"
data/src/context.rb CHANGED
@@ -1,10 +1,16 @@
1
1
  module Edward
2
- # Context for a template to run in
2
+ # Context for a template to run in.
3
+ # It has a reference to its page and the builder.
3
4
  class RenderContext
4
- def initialize page
5
+ def initialize page, builder
5
6
  @page = page
7
+ @pages = builder.pages
6
8
  end
7
9
 
10
+ def pages_with_tag tag
11
+ @pages.filter { it.tag? tag }
12
+ end
13
+
8
14
  # Include a partial file.
9
15
  # The file is searched for in _include,
10
16
  # rendered and returned.
@@ -14,7 +20,7 @@ module Edward
14
20
  def include file, locals = {}, &block
15
21
  include_path = "_include/" + file
16
22
  yaml, content = Page.extract_front_matter(File.read(include_path))
17
- options = (yaml&.dig(:options) || {}).merge(fixed_locals: "(locals:)")
23
+ options = (yaml&.dig(:options) || {}).merge(fixed_locals: "(local:)")
18
24
  Tilt[include_path].new(options) { content }.render(self, { local: locals }, &block)
19
25
  end
20
26
  end
data/src/page.rb CHANGED
@@ -17,21 +17,22 @@ module Edward
17
17
  @template = Tilt[path].new(self[:options]) { @content }
18
18
  add_layout(self[:layout]) if self[:layout]
19
19
  end
20
-
20
+
21
21
  # transform extensions from options to strings (for use in pipeline)
22
22
  def transform_option_keys options
23
23
  options&.transform_keys! { |k| Tilt.default_mapping.registered?(k.to_s) ? k.to_s : k }
24
24
  end
25
-
25
+
26
26
  # check if a file starts with yaml doc and can be mapped by tilt
27
27
  def self.page? path
28
28
  !Tilt.templates_for(path).empty? && YAML_FRONT_MATTER_REGEXP.match(File.read(path))
29
29
  end
30
30
 
31
- def render
32
- @template.render(Edward::RenderContext.new(self), nil, &@block)
31
+ def render builder
32
+ ctx = Edward::RenderContext.new(self, builder)
33
+ @template.render(ctx, nil, &proc { @block.call(ctx) })
33
34
  end
34
-
35
+
35
36
  def self.extract_front_matter content
36
37
  if content =~ YAML_FRONT_MATTER_REGEXP
37
38
  yaml = YAML.safe_load(Regexp.last_match(1), symbolize_names: true, permitted_classes: [Symbol])
@@ -41,36 +42,48 @@ module Edward
41
42
  [nil, content]
42
43
  end
43
44
  end
44
-
45
+
45
46
  # wrap this page in a layout
46
47
  def add_layout name
47
48
  layout_path = "_layouts/#{name}"
48
49
  yaml, content = Page.extract_front_matter(File.read(layout_path))
50
+ next_layout = yaml[:layout]
49
51
  @yaml = yaml.deep_merge!(@yaml, knockout_prefix: "--") if yaml
50
52
  transform_option_keys(self[:options])
51
53
  inner_template = @template
52
54
  inner_block = @block
53
- @block = proc { inner_template.render(Edward::RenderContext.new(self), nil, &inner_block) }
55
+ @block = proc { |ctx| inner_template.render(ctx, nil, &inner_block) }
54
56
  @template = Tilt[layout_path].new(self[:options]) { content }
57
+ add_layout(next_layout) if next_layout
55
58
  end
56
-
59
+
57
60
  def name
58
61
  File.basename(@path)
59
62
  end
60
-
63
+
61
64
  # the name of the new file
62
65
  def new_name
63
66
  File.basename(@path, ".*")
64
67
  end
65
-
68
+
66
69
  def dirname
67
70
  File.dirname(@path)
68
71
  end
69
-
72
+
70
73
  def new_path
71
74
  "#{dirname}/#{new_name}"
72
75
  end
73
-
76
+
77
+ # The directory name if new_name is index.html.
78
+ # Otherwise the full new path.
79
+ def href
80
+ if new_name == "index.html"
81
+ "/" + dirname
82
+ else
83
+ "/#{dirname}/#{new_name}"
84
+ end
85
+ end
86
+
74
87
  def tag? tag
75
88
  @yaml&.dig(:tags)&.include? tag
76
89
  end
@@ -78,6 +91,6 @@ module Edward
78
91
  def [](*keys)
79
92
  @yaml&.dig(*keys)
80
93
  end
81
-
94
+
82
95
  end
83
96
  end
data/src/version.rb CHANGED
@@ -1,4 +1,4 @@
1
- # generated with rake release[0.4.0]
1
+ # generated with rake release[0.6.0]
2
2
  module Edward
3
- VERSION = "0.4.0"
3
+ VERSION = "0.6.0"
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mr-edward
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rein Fernhout
@@ -95,7 +95,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubygems_version: 4.0.3
98
+ rubygems_version: 4.0.6
99
99
  specification_version: 4
100
100
  summary: Statis site generator
101
101
  test_files: []