Skip to content

Commit ac9fe10

Browse files
authored
Merge pull request #432 from DannyBen/add/render-show
Document rendering improvements
2 parents f352922 + d04ab0b commit ac9fe10

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+740
-191
lines changed

examples/render-mandoc/test.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
rm -f ./src/*.sh
44

5-
set -x
5+
# set -x # can't use this here, since the piped command appears in random order
66

77
bashly render :mandoc docs
88

lib/bashly/commands/render.rb

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module Commands
66
class Render < Base
77
help 'Render the bashly data structure using cutsom templates'
88

9-
usage 'bashly render SOURCE TARGET [options]'
9+
usage 'bashly render SOURCE TARGET [--watch --show PATH]'
1010
usage 'bashly render SOURCE --about'
1111
usage 'bashly render --list'
1212
usage 'bashly render (-h|--help)'
@@ -20,12 +20,21 @@ class Render < Base
2020
param 'TARGET', 'Output directory'
2121

2222
option '-w --watch', 'Watch bashly.yml and the templates source for changes and render on change'
23+
option '-s --show PATH', <<~USAGE
24+
After rendering, show the result generated in PATH.
25+
26+
The provided PATH is treated as relative TARGET.
27+
28+
Note that this works only if the template source supports it.
29+
USAGE
30+
2331
option '-l --list', 'Show list of built-in templates'
2432
option '-a --about', 'Show information about a given templates source'
2533

2634
example 'bashly render --list'
2735
example 'bashly render :markdown --about'
2836
example 'bashly render :markdown docs --watch'
37+
example 'bashly render :markdown docs --show "cli-download.1"'
2938
example 'bashly render /path/to/templates ./out_path'
3039

3140
attr_reader :watching, :target, :source
@@ -50,21 +59,6 @@ def show_about
5059
puts TTY::Markdown.parse(render_source.readme)
5160
end
5261

53-
def render_source
54-
@render_source ||= begin
55-
source = RenderSource.new selector
56-
raise "Invalid render source: #{args['SOURCE']}" unless source.exist?
57-
58-
source
59-
end
60-
end
61-
62-
def selector
63-
return args['SOURCE'] unless args['SOURCE'].start_with? ':'
64-
65-
args['SOURCE'][1..].to_sym
66-
end
67-
6862
def start_render
6963
@target = args['TARGET']
7064
@watching = args['--watch']
@@ -74,7 +68,7 @@ def start_render
7468
end
7569

7670
def render
77-
render_source.render target
71+
render_source.render target, show: args['--show']
7872
end
7973

8074
def watch
@@ -86,6 +80,21 @@ def watch
8680
end
8781
end
8882

83+
def render_source
84+
@render_source ||= begin
85+
source = RenderSource.new selector
86+
raise "Invalid render source: #{args['SOURCE']}" unless source.exist?
87+
88+
source
89+
end
90+
end
91+
92+
def selector
93+
return args['SOURCE'] unless args['SOURCE'].start_with? ':'
94+
95+
args['SOURCE'][1..].to_sym
96+
end
97+
8998
def watchables
9099
@watchables ||= [Settings.config_path, render_source.path]
91100
end

lib/bashly/libraries/render/mandoc/README.md

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,30 +10,9 @@ will then use [pandoc](https://command-not-found.com/pandoc) to convert them.
1010
```bash
1111
# Generate all man pages to the ./docs directory
1212
$ bashly render :mandoc docs
13-
```
14-
15-
## Viewing the output
16-
17-
Setting the environment variable `PREVIEW` to the full command you wish
18-
to preview, will prompt the renderer to show the output using the `man`
19-
command after rendering.
20-
21-
```bash
22-
# Preview the page for the "cli download" command
23-
$ PREVIEW="cli download" bashly render :mandoc docs
24-
25-
# .. and also watch for changes (after existing the man preview)
26-
$ PREVIEW="cli download" bashly render :mandoc docs --watch
27-
```
28-
29-
In addition, you can use one of the following commands:
30-
31-
```bash
32-
# View the man page interactively
33-
$ man docs/cli-download.1
3413

35-
# Print the man page to stdout
36-
$ man docs/cli-download.1 | col -bx
14+
# Generate on change, and show one of the files
15+
$ bashly render :mandoc docs --watch --show cli-download.1
3716
```
3817

3918
## Supported custom definitions

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
raise "Failed running pandoc\nMake sure the following command succeeds and try again:\n\n #{cmd}" unless success
1818

1919
say "g`saved` #{manfile}"
20-
21-
system %[man "#{manfile}"] if ENV['PREVIEW'] == command.full_name
2220
}
2321

2422
# Render the main command
@@ -28,3 +26,9 @@
2826
command.deep_commands.reject(&:private).each do |subcommand|
2927
save_manpage.call subcommand
3028
end
29+
30+
# Show one of the files if requested
31+
if show
32+
file = "#{target}/#{show}"
33+
system "man #{file}" if File.exist?(file)
34+
end

lib/bashly/libraries/render/markdown/README.md

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,9 @@ Render markdown documents for your script.
77
```bash
88
# Generate all documents to the ./docs directory
99
$ bashly render :markdown docs
10-
```
11-
12-
## Viewing the output
13-
14-
In order to view your markdown files, you can use
15-
[Madness markdown server](https://madness.dannyb.co/):
1610

17-
```bash
18-
$ gem install madness
19-
$ madness server docs
11+
# Generate on change, and show one of the files
12+
$ bashly render :markdown docs --watch --show index.md
2013
```
2114

2215
## Supported custom definitions
@@ -37,3 +30,13 @@ x_markdown_footer: |-
3730
3831
Report issues at <https://github.com/lanalang/smallville>
3932
```
33+
34+
## Markdown server
35+
36+
In order to view your markdown files in a browser, you can use the
37+
[Madness markdown server](https://madness.dannyb.co/):
38+
39+
```bash
40+
$ gem install madness
41+
$ madness server docs
42+
```

lib/bashly/libraries/render/markdown/markdown.gtx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ if commands.any?
9494
> ## {{ group.gsub(/:$/, '') }}
9595
>
9696
commands.each do |subcommand|
97-
> - [{{ subcommand.name }}]({{ subcommand.full_name }}) - {{ subcommand.summary.for_markdown }}
97+
> - [{{ subcommand.name }}]({{ subcommand.full_name.gsub(' ', '%20') }}) - {{ subcommand.summary.for_markdown }}
9898
end
9999
>
100100
end

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# render script - markdown
22
require 'gtx'
33

4+
# for previewing only (not needed for rendering)
5+
require 'tty-markdown'
6+
47
# Load the GTX template
58
template = "#{source}/markdown.gtx"
69
gtx = GTX.load_file template
@@ -12,3 +15,9 @@
1215
command.deep_commands.reject(&:private).each do |subcommand|
1316
save "#{target}/#{subcommand.full_name}.md", gtx.parse(subcommand)
1417
end
18+
19+
# Show one of the files if requested
20+
if show
21+
file = "#{target}/#{show}"
22+
puts TTY::Markdown.parse_file(file) if File.exist?(file)
23+
end

lib/bashly/render_context.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ module Bashly
55
class RenderContext
66
include Colsole
77

8-
attr_reader :source, :target
8+
attr_reader :source, :target, :show
9+
attr_writer :config
910

10-
def initialize(source, target)
11+
def initialize(source:, target:, show: nil)
1112
@source = source
1213
@target = target
14+
@show = show
1315
end
1416

1517
def config

lib/bashly/render_source.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def initialize(selector)
2525
@selector = selector
2626
end
2727

28-
def render(target)
29-
RenderContext.new(path, target).instance_eval render_script
28+
def render(target, show: nil)
29+
context = RenderContext.new source: path, target: target, show: show
30+
context.instance_eval render_script
3031
end
3132

3233
def internal?
Lines changed: 1 addition & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1 @@
1-
Render markdown
2-
3-
Render markdown documents for your script.
4-
5-
Usage
6-
7-
# Generate all documents to the ./docs directory
8-
$ bashly render :markdown docs
9-
10-
Viewing the output
11-
12-
In order to view your markdown files, you can use
13-
Madness markdown server » https://madness.dannyb.co/:
14-
15-
$ gem install madness
16-
$ madness server docs
17-
18-
Supported custom definitions
19-
20-
Add these definitions to your bashly.yml to render them in your
21-
markdown:
22-
23-
Footer: x_markdown_footer
24-
25-
Add additional sections to your man pages. This field is expected
26-
to be in markdown format.
27-
28-
Example
29-
30-
x_markdown_footer: |-
31-
# ISSUE TRACKER
32-
33-
Report issues at <https://github.com/lanalang/smallville>
34-
1+
Heading

0 commit comments

Comments
 (0)