mr-edward 0.0.0 → 0.0.2
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 +4 -4
- data/lib/cli.rb +46 -0
- data/lib/context.rb +15 -0
- data/lib/edward.rb +1 -0
- data/lib/page.rb +76 -0
- data/lib/version.rb +3 -0
- metadata +6 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 550618153f13429874ed99478ecd6342971ae3bfb8129d7ef2a1398c0fb8ac81
|
|
4
|
+
data.tar.gz: ca24dacaa44c6d0141b24d0738e887bdeb5b1f25944c5f080fbfcf16160c2ef8
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 1806e0ff497429cc18556b2f5d7d959da925478d0ac843d4cacbf6878a05bea3206cf8db05b20d544531ff26a91468caf4b1ac9d598f1d29f24663d4d8026f4b
|
|
7
|
+
data.tar.gz: aaaffbc06816b7c55f6c69d2377f22186a8dd3c6cb3d0e3c4f74e4aa416c1f7b53bb3e6d82785a25a8b10c8cdea379818d89c0558b7c9815d5377c60ef06b58b
|
data/lib/cli.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
module Edward
|
|
2
|
+
class CLI
|
|
3
|
+
def initialize
|
|
4
|
+
@builder = Builder.new
|
|
5
|
+
end
|
|
6
|
+
|
|
7
|
+
def main
|
|
8
|
+
case ARGV.first
|
|
9
|
+
when "serve" then serve
|
|
10
|
+
when "init" then init
|
|
11
|
+
else build
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def build
|
|
16
|
+
@builder.start
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def serve
|
|
20
|
+
require "webrick"
|
|
21
|
+
port = ARGV[1] ? ARGV[1].to_i : 3000
|
|
22
|
+
@server = WEBrick::HTTPServer.new :Port => port, :DocumentRoot => @builder.target
|
|
23
|
+
trap("INT") { @server.shutdown }
|
|
24
|
+
listen
|
|
25
|
+
puts "visit edward at http://127.0.0.1:#{@server.config[:Port]}"
|
|
26
|
+
@server.start
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def listen
|
|
30
|
+
require "listen"
|
|
31
|
+
Listen.to('.', ignore: /_site/) { |modified, added, removed|
|
|
32
|
+
puts "rebuilding"
|
|
33
|
+
@builder.start
|
|
34
|
+
}.start
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def init
|
|
38
|
+
require "fileutils"
|
|
39
|
+
FileUtils.mkdir_p "_include"
|
|
40
|
+
FileUtils.mkdir_p "_layouts"
|
|
41
|
+
File.write ".gitignore", <<~END
|
|
42
|
+
_site
|
|
43
|
+
END
|
|
44
|
+
end
|
|
45
|
+
end
|
|
46
|
+
end
|
data/lib/context.rb
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
module Edward
|
|
2
|
+
# Context for a template to run in
|
|
3
|
+
class RenderContext
|
|
4
|
+
def initialize page
|
|
5
|
+
@page = page
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def include file, locals = {}, &block
|
|
9
|
+
include_path = "_include/" + file
|
|
10
|
+
# yaml in includes is currently ignored
|
|
11
|
+
_yaml, content = Page.extract_front_matter(File.read(include_path))
|
|
12
|
+
Tilt[include_path].new(@page[:options]&.merge(fixed_locals: "(locals:)")){ content }.render(self, { local: locals }, &block)
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
end
|
data/lib/edward.rb
CHANGED
data/lib/page.rb
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
require "deep_merge"
|
|
2
|
+
|
|
3
|
+
module Edward
|
|
4
|
+
# represents a file that may be converted
|
|
5
|
+
class Page
|
|
6
|
+
YAML_FRONT_MATTER_REGEXP = %r!\A(---\s*\n.*?\n?)^((---|\.\.\.)\s*$\n?)!m.freeze
|
|
7
|
+
|
|
8
|
+
attr_reader :path, :layout, :yaml
|
|
9
|
+
|
|
10
|
+
def initialize path, locals = {}
|
|
11
|
+
@path = path
|
|
12
|
+
@locals = locals || {}
|
|
13
|
+
content = File.read(path)
|
|
14
|
+
@yaml, @content = Page.extract_front_matter(content)
|
|
15
|
+
@block = nil
|
|
16
|
+
@template = Tilt[path].new(self[:options]) { @content }
|
|
17
|
+
add_layout(self[:layout]) if self[:layout]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# check if a file starts with yaml doc and can be mapped by tilt
|
|
21
|
+
def self.page? path
|
|
22
|
+
!Tilt.templates_for(path).empty? && YAML_FRONT_MATTER_REGEXP.match(File.read(path))
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def render
|
|
26
|
+
@template.render(Edward::RenderContext.new(self), nil, &@block)
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def self.extract_front_matter content
|
|
30
|
+
if content =~ YAML_FRONT_MATTER_REGEXP
|
|
31
|
+
yaml = YAML.safe_load(Regexp.last_match(1), symbolize_names: true, permitted_classes: [Symbol])
|
|
32
|
+
content = Regexp.last_match.post_match
|
|
33
|
+
[yaml, content]
|
|
34
|
+
else
|
|
35
|
+
[nil, content]
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# wrap this page in a layout
|
|
40
|
+
def add_layout name
|
|
41
|
+
layout_path = "_layouts/#{name}.slim"
|
|
42
|
+
yaml, content = Page.extract_front_matter(File.read(layout_path))
|
|
43
|
+
@yaml = yaml.deep_merge!(@yaml, knockout_prefix: "--") if yaml
|
|
44
|
+
inner_template = @template
|
|
45
|
+
inner_block = @block
|
|
46
|
+
@block = proc { inner_template.render(Edward::RenderContext.new(self), nil, &inner_block) }
|
|
47
|
+
@template = Tilt[layout_path].new(self[:options]) { content }
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
def name
|
|
51
|
+
File.basename(@path)
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# the name of the new file
|
|
55
|
+
def new_name
|
|
56
|
+
File.basename(@path, ".*")
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
def dirname
|
|
60
|
+
File.dirname(@path)
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def new_path
|
|
64
|
+
"#{dirname}/#{new_name}"
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def tag? tag
|
|
68
|
+
@yaml&.dig(:tags)&.include? tag
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def [](*keys)
|
|
72
|
+
@yaml&.dig(*keys)
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
end
|
|
76
|
+
end
|
data/lib/version.rb
ADDED
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.0.
|
|
4
|
+
version: 0.0.2
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Rein Fernhout
|
|
@@ -72,7 +72,12 @@ extra_rdoc_files: []
|
|
|
72
72
|
files:
|
|
73
73
|
- bin/edward
|
|
74
74
|
- lib/builder.rb
|
|
75
|
+
- lib/cli.rb
|
|
76
|
+
- lib/context.rb
|
|
75
77
|
- lib/edward.rb
|
|
78
|
+
- lib/page.rb
|
|
79
|
+
- lib/version.rb
|
|
80
|
+
homepage: https://github.com/LevitatingBusinessMan/edward
|
|
76
81
|
licenses:
|
|
77
82
|
- MIT
|
|
78
83
|
metadata: {}
|