Skip to content

Commit 025a644

Browse files
authored
Merge pull request #424 from DannyBen/refactor/render-model
Refactor render command with RenderSource model
2 parents f8d748c + 014cd86 commit 025a644

File tree

19 files changed

+299
-89
lines changed

19 files changed

+299
-89
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ jobs:
1414
LC_ALL: en_US.UTF-8
1515

1616
strategy:
17-
matrix: { ruby: ['3.0', '3.1', '3.2', head] }
17+
matrix: { ruby: ['3.0', '3.1', '3.2'] }
1818

1919
steps:
2020
- name: Checkout code

bashly.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Gem::Specification.new do |s|
2121
s.add_dependency 'gtx', '~> 0.1'
2222
s.add_dependency 'lp', '~> 0.2'
2323
s.add_dependency 'mister_bin', '~> 0.7'
24+
s.add_dependency 'tty-markdown', '~> 0.7'
2425

2526
# Ruby 3.0 comes with Psych 3.3.0, which does not have the `unsafe_load`
2627
# ref: https://github.com/ruby/psych/commit/cb50aa8d3fb8be01897becff77b4922b12a0ab4c

lib/bashly.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ module Bashly
1616
autoload :Library, 'bashly/library'
1717
autoload :LibrarySource, 'bashly/library_source'
1818
autoload :MessageStrings, 'bashly/message_strings'
19+
autoload :RenderContext, 'bashly/render_context'
20+
autoload :RenderSource, 'bashly/render_source'
1921
autoload :VERSION, 'bashly/version'
2022

2123
autoload :AssetHelper, 'bashly/concerns/asset_helper'

lib/bashly/commands/render.rb

Lines changed: 45 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,96 +1,93 @@
11
require 'filewatcher'
2-
require 'date' # for use by templates
2+
require 'tty-markdown'
33

44
module Bashly
55
module Commands
66
class Render < Base
77
help 'Render the bashly data structure using cutsom templates'
88

99
usage 'bashly render SOURCE TARGET [options]'
10+
usage 'bashly render SOURCE --about'
11+
usage 'bashly render --list'
1012
usage 'bashly render (-h|--help)'
1113

1214
param 'SOURCE', <<~HELP
1315
An ID to an internal templates source, or a path to a custom templates directory.
1416
15-
A leading colon (:) denotes an internal ID.
16-
17-
Available IDs:
18-
- :markdown - render markdown documents for each command.
19-
- :mandoc - render man pages for each command.
17+
A leading colon (:) denotes an internal ID (see `--list`).
2018
HELP
2119

2220
param 'TARGET', 'Output directory'
2321

2422
option '-w --watch', 'Watch bashly.yml and the templates source for changes and render on change'
23+
option '-l --list', 'Show list of built-in templates'
24+
option '-a --about', 'Show information about a given templates source'
2525

26+
example 'bashly render --list'
27+
example 'bashly render :markdown --about'
2628
example 'bashly render :markdown docs --watch'
2729
example 'bashly render /path/to/templates ./out_path'
2830

29-
attr_reader :watching, :source, :target
31+
attr_reader :watching, :target, :source
3032

3133
def run
32-
@source = source_path
33-
@target = args['TARGET']
34-
@watching = args['--watch']
35-
36-
render
37-
watch if watching
34+
if args['--list'] then show_list
35+
elsif args['--about'] then show_about
36+
else
37+
start_render
38+
end
3839
end
3940

4041
private
4142

42-
# This method is the single DSL method for the render script
43-
def save(filename, content)
44-
File.deep_write filename, content
45-
say "g`saved` #{filename}"
43+
def show_list
44+
RenderSource.internal.each_value do |source|
45+
say "g`:#{source.selector.to_s.ljust 10}` #{source.summary}"
46+
end
47+
end
48+
49+
def show_about
50+
puts TTY::Markdown.parse(render_source.readme)
4651
end
4752

48-
def watch
49-
say "g`watching`\n"
53+
def render_source
54+
@render_source ||= begin
55+
source = RenderSource.new selector
56+
raise "Invalid render source: #{args['SOURCE']}" unless source.exist?
5057

51-
Filewatcher.new(watchables).watch do
52-
reset
53-
render
54-
rescue Bashly::ConfigurationError => e
55-
say! "rib` #{e.class} `\n#{e.message}"
56-
ensure
57-
say "g`waiting`\n"
58+
source
5859
end
5960
end
6061

61-
def render
62-
with_valid_config { instance_eval render_script }
63-
end
62+
def selector
63+
return args['SOURCE'] unless args['SOURCE'].start_with? ':'
6464

65-
def reset
66-
@config = nil
67-
@config_validator = nil
68-
@command = nil
65+
args['SOURCE'][1..].to_sym
6966
end
7067

71-
def command
72-
@command ||= Script::Command.new config
68+
def start_render
69+
@target = args['TARGET']
70+
@watching = args['--watch']
71+
72+
render
73+
watch if watching
7374
end
7475

75-
def watchables
76-
@watchables ||= [Settings.config_path, source]
76+
def render
77+
render_source.render target
7778
end
7879

79-
def source_path
80-
result = args['SOURCE']
80+
def watch
81+
say "g`watching`\n"
8182

82-
if result.start_with? ':'
83-
id = result[1..]
84-
result = asset "libraries/render/#{id}"
83+
Filewatcher.new(watchables).watch do
84+
render
85+
say "g`waiting`\n"
8586
end
86-
87-
return result if Dir.exist? result
88-
89-
raise "Invalid source.\nDirectory not found: #{result}"
9087
end
9188

92-
def render_script
93-
@render_script ||= File.read "#{source}/render.rb"
89+
def watchables
90+
@watchables ||= [Settings.config_path, args['SOURCE']]
9491
end
9592
end
9693
end

lib/bashly/libraries/libraries.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ render_markdown:
6060
help: Copy the markdown templates to your project, allowing you to customize the markdown documentation output.
6161
skip_src_check: true
6262
files:
63+
- source: "render/markdown/README.md"
64+
target: "templates/markdown/README.md"
6365
- source: "render/markdown/markdown.gtx"
6466
target: "templates/markdown/markdown.gtx"
6567
- source: "render/markdown/render.rb"
@@ -73,6 +75,8 @@ render_mandoc:
7375
help: Copy the mandoc templates to your project, allowing you to customize the man documentation output.
7476
skip_src_check: true
7577
files:
78+
- source: "render/mandoc/README.md"
79+
target: "templates/mandoc/README.md"
7680
- source: "render/mandoc/mandoc.gtx"
7781
target: "templates/mandoc/mandoc.gtx"
7882
- source: "render/mandoc/render.rb"
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Render mandoc
2+
3+
Render man pages for your script.
4+
5+
Note that this renderer will render specially formatted markdown documents and
6+
will then use [pandoc](https://command-not-found.com/pandoc) to convert them.
7+
8+
Setting the environment variable `PREVIEW` to the full command you wish to
9+
preview, will prompt the renderer to show the output using the `man` command
10+
after rendering.
11+
12+
## Usage
13+
14+
```bash
15+
# Generate all man pages to the ./docs directory
16+
$ bashly render :mandoc docs
17+
18+
# .. and also preview the page for the "cli download" command
19+
$ PREVIEW="cli download" bashly render :mandoc docs
20+
21+
# .. and also watch for changes
22+
$ PREVIEW="cli download" bashly render :mandoc docs --watch
23+
```

lib/bashly/libraries/render/mandoc/render.rb

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
# render script - mandoc
2-
#
3-
# You may set the environment variable PREVIEW to the full command you wich
4-
# to preview while generating. For example:
5-
#
6-
# PREVIEW="cli download" bashly render :mandoc docs --watch
7-
#
8-
# Alternatively, view any of the man pages with man:
9-
#
10-
# man docs/cli.1
11-
#
2+
require 'gtx'
123

134
# Load the GTX template
145
template = "#{source}/mandoc.gtx"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Render man pages for your script
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Render markdown
2+
3+
Render markdown documents for your script.
4+
5+
## Usage
6+
7+
```bash
8+
# Generate all documents to the ./docs directory
9+
$ bashly render :markdown docs
10+
```
11+
12+
## Viewing your markdown documents
13+
14+
In order to preview your markdown files, you can use the
15+
[Madness markdown server](https://madness.dannyb.co/):
16+
17+
```bash
18+
$ gem install madness
19+
$ madness server docs
20+
```

lib/bashly/libraries/render/markdown/render.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
# render script - markdown
2-
#
3-
# In order to preview your markdown files, you can use the madness markdown
4-
# server <https://madness.dannyb.co/>:
5-
#
6-
# $ gem install madness
7-
# $ madness server docs
8-
#
2+
require 'gtx'
93

104
# Load the GTX template
115
template = "#{source}/markdown.gtx"

0 commit comments

Comments
 (0)