madman 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 47049987d632e62ea57b613c96170b95031acaa20b0390fdbf55d430d02c45ec
4
- data.tar.gz: 53856412c8eb24b3ee946f24b554f80fa66aad09ee93bfe0b7ff0bc30ae90bb1
3
+ metadata.gz: 8d2cbbb3df05900e21afcc52747a66d7e29966f89e16f479cf5fdf55dfcbf3d0
4
+ data.tar.gz: 48a7d30f17a8b31909d11ea296c424cbb5611d931d4b1ab101166bb192e43f57
5
5
  SHA512:
6
- metadata.gz: a2df596bb8c46b703e2e708199a945d95534bf5a294ce40580b3693bb7390087a46b1fce1e6dc31cc33adca2106fc36c64210e78a9d433597006463026af88a5
7
- data.tar.gz: daca0122ce6af5f38f9aeeed54c87fc818784c93a29e89da89eb6babe71ab0f6359c9686a5a9889cb5a77e295ca373fdd99903df4bb55b0a74416257d6e496a4
6
+ metadata.gz: 6e2fe3266fe89d3ba1c9cfa776e533d52919dbc0af47609ea5057f07bb6b8d420bd6cbcceb9914a9e53c94975a85ea245df4d91e0025ff0a50427e88051f9fa3
7
+ data.tar.gz: 59ae594fc5021740a4b980e914b8309a404f1399bfd99a322286307d99a84715f0452eb7131d5f0343715d6c5f221d3614768d10024a45f6bc6034b9e1280aa7
data/README.md CHANGED
@@ -35,10 +35,10 @@ Key Features
35
35
  - Convert markdown to a single HTML file (including CSS).
36
36
  - Ad-hoc server to serve a markdown file locally for preview.
37
37
  - Markdown generation with GitHub API (optional).
38
+ - Table of Contents generation for a folder of markdown files.
39
+ - Web server for an entire folder (like [Madness](https://github.com/DannyBen/madness))
38
40
  - [Planned] Table of Contents generation for a single file.
39
- - [Planned] Table of Contents generation for a folder of markdown files.
40
41
  - [Planned] Combine multiple markdown files to one.
41
- - [Considered] Web server for an entire folder (like [Madness][1])
42
42
 
43
43
 
44
44
 
@@ -46,21 +46,40 @@ Usage
46
46
  --------------------------------------------------
47
47
 
48
48
  <!-- usage -->
49
+
49
50
  ```
50
51
  $ madman
51
52
  Commands:
52
- render Render markdown to HTML
53
- serve Run a webserver and serve the markdown file as HTML
53
+ nav Add site-wide navigation links to README files
54
+ preview Serve a markdown file using a local server
55
+ render Render markdown to HTML
56
+ serve Serve a markdown folder using a local server
54
57
  ```
55
58
 
56
- ---
59
+ <!-- usage -->
60
+
61
+ ### Render Markdown to File
62
+
63
+ <!-- render -->
64
+
65
+ ```
66
+ $ madman render
67
+ Usage:
68
+ madman render FILE [--github --save OUTFILE]
69
+ madman render (-h|--help|--version)
70
+ ```
71
+
72
+ <!-- render -->
73
+
74
+ <details><summary>Show full usage</summary>
75
+ <!-- render-help -->
57
76
 
58
77
  ```
59
78
  $ madman render --help
60
79
  Render markdown to HTML
61
80
 
62
81
  Usage:
63
- madman render INFILE [OUTFILE] [--rtl --github]
82
+ madman render FILE [--github --save OUTFILE]
64
83
  madman render (-h|--help|--version)
65
84
 
66
85
  Options:
@@ -68,20 +87,16 @@ Options:
68
87
  Render using the GitHub API
69
88
  Requires setting the GITHUB_ACCESS_TOKEN environment variable
70
89
 
71
- --rtl
72
- Render text Right to Left
90
+ --save OUTFILE
91
+ Save the output to a file
73
92
 
74
93
  -h --help
75
94
  Show this help
76
95
 
77
96
  Parameters:
78
- INFILE
97
+ FILE
79
98
  The input markdown file
80
99
 
81
- OUTFILE
82
- The output HTML file
83
- If not provided, the input filename will be used with .html extension
84
-
85
100
  Environment Variables:
86
101
  GITHUB_ACCESS_TOKEN
87
102
  Your GitHub API access token
@@ -90,26 +105,93 @@ Environment Variables:
90
105
  Examples:
91
106
  madman render README.md
92
107
  madman render README.md --github
93
- madman render README.md out.html --rtl
108
+ madman render README.md --save out.html
94
109
  ```
95
110
 
96
- ---
111
+ <!-- render-help -->
112
+ </details>
113
+
114
+ ### Preview Markdown in Browser
115
+
116
+ <!-- preview -->
97
117
 
98
118
  ```
99
- $ madman serve --help
100
- Run a webserver and serve the markdown file as HTML
119
+ $ madman preview
120
+ Usage:
121
+ madman preview FILE [--port N --bind ADDRESS]
122
+ madman preview (-h|--help|--version)
123
+ ```
124
+
125
+ <!-- preview -->
126
+
127
+ <details><summary>Show full usage</summary><!-- preview-help -->
128
+
129
+ ```
130
+ $ madman preview --help
131
+ Serve a markdown file using a local server
101
132
 
102
133
  This command will start a local server with two endpoints:
103
134
  / will render the markdown with the default renderer
104
135
  /github will render with the GitHub API
105
136
 
106
137
  Usage:
107
- madman serve INFILE [--port N --bind ADDRESS --rtl]
138
+ madman preview FILE [--port N --bind ADDRESS]
139
+ madman preview (-h|--help|--version)
140
+
141
+ Options:
142
+ -p --port N
143
+ Set server port [default: 3000]
144
+
145
+ -b --bind ADDRESS
146
+ Set server listen address [default: 0.0.0.0]
147
+
148
+ -h --help
149
+ Show this help
150
+
151
+ Parameters:
152
+ FILE
153
+ The input markdown file
154
+
155
+ Environment Variables:
156
+ GITHUB_ACCESS_TOKEN
157
+ Your GitHub API access token
158
+ Required only if you wish to use the '/github' endpoint
159
+ Generate one here: https://github.com/settings/tokens
160
+
161
+ Examples:
162
+ madman preview README.md
163
+ madman preview README.md -p4000
164
+ ```
165
+
166
+ <!-- preview-help --></details>
167
+
168
+
169
+ ### Personal Wiki (Serve a complete Markdown folder)
170
+
171
+ <!-- serve -->
172
+
173
+ ```
174
+ $ madman serve
175
+ Usage:
176
+ madman serve FOLDER [--port N --bind ADDRESS --github]
177
+ madman serve (-h|--help|--version)
178
+ ```
179
+
180
+ <!-- serve -->
181
+
182
+ <details><summary>Show full usage</summary><!-- serve-help -->
183
+
184
+ ```
185
+ $ madman serve --help
186
+ Serve a markdown folder using a local server
187
+
188
+ Usage:
189
+ madman serve FOLDER [--port N --bind ADDRESS --github]
108
190
  madman serve (-h|--help|--version)
109
191
 
110
192
  Options:
111
- --rtl
112
- Render text Right to Left.
193
+ --github
194
+ Use the GitHub API renderer instead of the default one
113
195
 
114
196
  -p --port N
115
197
  Set server port [default: 3000]
@@ -121,8 +203,8 @@ Options:
121
203
  Show this help
122
204
 
123
205
  Parameters:
124
- INFILE
125
- The input markdown file
206
+ FOLDER
207
+ The folder containing markdown files
126
208
 
127
209
  Environment Variables:
128
210
  GITHUB_ACCESS_TOKEN
@@ -131,13 +213,67 @@ Environment Variables:
131
213
  Generate one here: https://github.com/settings/tokens
132
214
 
133
215
  Examples:
134
- madman serve README.md
135
- madman serve README.md -p4000 --rtl
216
+ madman serve
217
+ madman serve path/to/docs -p4000 --github
136
218
  ```
137
219
 
138
- ---
220
+ <!-- serve-help --></details>
139
221
 
140
- <!-- usage -->
141
222
 
223
+ ### Inject Site Navigation (Table of Contents)
224
+
225
+ <!-- nav -->
226
+
227
+ ```
228
+ $ madman nav
229
+ Usage:
230
+ madman nav FOLDER [--marker TEXT --force --dry --verbose --depth N]
231
+ madman nav (-h|--help|--version)
232
+ ```
233
+
234
+ <!-- nav -->
235
+
236
+ <details><summary>Show full usage</summary><!-- nav-help -->
237
+
238
+ ```
239
+ $ madman nav --help
240
+ Add site-wide navigation links to README files
241
+
242
+ This command will add a table of contents to all README files in the folder. The
243
+ table of contents will link to all the pages and folders that are in the same
244
+ folder as each README file.
245
+
246
+ Usage:
247
+ madman nav FOLDER [--marker TEXT --force --dry --verbose --depth N]
248
+ madman nav (-h|--help|--version)
249
+
250
+ Options:
251
+ -f --force
252
+ Add to all README files, even if they do not have a marker
253
+
254
+ -m --marker TEXT
255
+ Look for an HTML comment with <!-- TEXT --> [default: nav]
256
+
257
+ -d --depth N
258
+ The depth of the table of contents [default: 1]
259
+
260
+ --dry
261
+ Do not save the updated files, just show what will happen
262
+
263
+ -v --verbose
264
+ Show the updated README content
265
+
266
+ -h --help
267
+ Show this help
268
+
269
+ Parameters:
270
+ FOLDER
271
+ The folder containing markdown files
272
+
273
+ Examples:
274
+ madman nav
275
+ madman nav path/to/docs --force --marker toc
276
+ madman nav path/to/docs --dry -v -d2
277
+ ```
142
278
 
143
- [1]: https://github.com/DannyBen/madness
279
+ <!-- nav-help --></details>
data/bin/madman-nav.rb ADDED
@@ -0,0 +1,48 @@
1
+ require 'madman'
2
+
3
+ summary "Add site-wide navigation links to README files"
4
+
5
+ help "This command will add a table of contents to all README files in the folder. The table of contents will link to all the pages and folders that are in the same folder as each README file."
6
+
7
+ usage "madman nav FOLDER [--marker TEXT --force --dry --verbose --depth N]"
8
+ usage "madman nav (-h|--help|--version)"
9
+
10
+ option "-f --force", "Add to all README files, even if they do not have a marker"
11
+ option "-m --marker TEXT", "Look for an HTML comment with <!-- TEXT --> [default: nav]"
12
+ option "-d --depth N", "The depth of the table of contents [default: 1]"
13
+ option "--dry", "Do not save the updated files, just show what will happen"
14
+ option "-v --verbose", "Show the updated README content"
15
+
16
+ param "FOLDER", "The folder containing markdown files"
17
+
18
+ example "madman nav"
19
+ example "madman nav path/to/docs --force --marker toc"
20
+ example "madman nav path/to/docs --dry -v -d2"
21
+
22
+ action do |args|
23
+ dir = args['FOLDER'] || '.'
24
+ force = args['--force']
25
+ marker = args['--marker']
26
+ dry = args['--dry']
27
+ verbose = args['--verbose']
28
+ depth = args['--depth'].to_i
29
+
30
+ candidates = Dir["#{dir}/**/README.md"]
31
+ candidates.each do |candidate|
32
+ say "Updating !txtgrn!#{candidate}"
33
+ candidate_dir = File.dirname candidate
34
+ toc = Madman::Navigation.new candidate_dir, depth: depth
35
+ doc = Madman::Document.from_file candidate
36
+ doc.inject toc.markdown, marker: marker, force: force
37
+
38
+ if verbose
39
+ say word_wrap " !txtblu!#{doc.text}"
40
+ say ""
41
+ end
42
+
43
+ doc.save unless dry
44
+ end
45
+
46
+ say dry ? "Done (dry mode, nothing changed)" : "Done"
47
+ end
48
+
@@ -0,0 +1,34 @@
1
+ require 'madman'
2
+
3
+ summary "Serve a markdown file using a local server"
4
+
5
+ help "This command will start a local server with two endpoints:\n / will render the markdown with the default renderer\n /github will render with the GitHub API"
6
+
7
+ usage "madman preview FILE [--port N --bind ADDRESS]"
8
+ usage "madman preview (-h|--help|--version)"
9
+
10
+ option "-p --port N", "Set server port [default: 3000]"
11
+ option "-b --bind ADDRESS", "Set server listen address [default: 0.0.0.0]"
12
+
13
+ param "FILE", "The input markdown file"
14
+
15
+ environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nRequired only if you wish to use the '/github' endpoint\nGenerate one here: https://github.com/settings/tokens"
16
+
17
+ example "madman preview README.md"
18
+ example "madman preview README.md -p4000"
19
+
20
+ action do |args|
21
+ file = args['FILE']
22
+ port = args['--port']
23
+ bind = args['--bind']
24
+
25
+ say "Starting server at !undblu!localhost:#{port}!txtrst!"
26
+ if ENV['GITHUB_ACCESS_TOKEN']
27
+ say "To view the GitHub API version go to !undblu!localhost:#{port}/github!txtrst!"
28
+ end
29
+ say ''
30
+
31
+ Madman::PreviewServer.set bind: bind, port: port, file: file
32
+ Madman::PreviewServer.run!
33
+ end
34
+
data/bin/madman-render.rb CHANGED
@@ -2,33 +2,34 @@ require 'madman'
2
2
 
3
3
  help "Render markdown to HTML"
4
4
 
5
- usage "madman render INFILE [OUTFILE] [--rtl --github]"
5
+ usage "madman render FILE [--github --save OUTFILE]"
6
6
  usage "madman render (-h|--help|--version)"
7
7
 
8
8
  option "--github", "Render using the GitHub API\nRequires setting the GITHUB_ACCESS_TOKEN environment variable"
9
- option "--rtl", "Render text Right to Left"
9
+ option "--save OUTFILE", "Save the output to a file"
10
10
 
11
- param "INFILE", "The input markdown file"
12
- param "OUTFILE", "The output HTML file\nIf not provided, the input filename will be used with .html extension"
11
+ param "FILE", "The input markdown file"
13
12
 
14
13
  environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nGenerate one here: https://github.com/settings/tokens"
15
14
 
16
15
  example "madman render README.md"
17
16
  example "madman render README.md --github"
18
- example "madman render README.md out.html --rtl"
17
+ example "madman render README.md --save out.html"
19
18
 
20
19
  action do |args|
21
- infile = args['INFILE']
22
- outfile = args['OUTFILE'] || "#{infile}.html"
20
+ infile = args['FILE']
21
+ outfile = args['--save']
23
22
  renderer = args['--github'] ? :github : :default
24
23
 
25
24
  doc = Madman::Document.from_file infile
26
- template = Madman::Template.new :default
27
25
 
28
- say "Rendering using the #{renderer} renderer"
29
- output = template.render doc.render(renderer)
30
-
31
- File.write outfile, output
32
- say "Saved !txtgrn!#{outfile}"
26
+ output = doc.render renderer
27
+
28
+ if outfile
29
+ File.write outfile, output
30
+ say "Saved !txtgrn!#{outfile}"
31
+ else
32
+ puts output
33
+ end
33
34
  end
34
35
 
data/bin/madman-serve.rb CHANGED
@@ -1,38 +1,32 @@
1
1
  require 'madman'
2
2
 
3
- summary "Run a webserver and serve the markdown file as HTML"
3
+ summary "Serve a markdown folder using a local server"
4
4
 
5
- help "This command will start a local server with two endpoints:\n / will render the markdown with the default renderer\n /github will render with the GitHub API"
6
-
7
- usage "madman serve INFILE [--port N --bind ADDRESS --rtl]"
5
+ usage "madman serve FOLDER [--port N --bind ADDRESS --github]"
8
6
  usage "madman serve (-h|--help|--version)"
9
7
 
10
- option "--rtl", "Render text Right to Left."
8
+ option "--github", "Use the GitHub API renderer instead of the default one"
11
9
  option "-p --port N", "Set server port [default: 3000]"
12
10
  option "-b --bind ADDRESS", "Set server listen address [default: 0.0.0.0]"
13
11
 
14
- param "INFILE", "The input markdown file"
12
+ param "FOLDER", "The folder containing markdown files"
15
13
 
16
14
  environment "GITHUB_ACCESS_TOKEN", "Your GitHub API access token\nRequired only if you wish to use the '/github' endpoint\nGenerate one here: https://github.com/settings/tokens"
17
15
 
18
- example "madman serve README.md"
19
- example "madman serve README.md -p4000 --rtl"
16
+ example "madman serve"
17
+ example "madman serve path/to/docs -p4000 --github"
20
18
 
21
19
  action do |args|
22
- opts = {
23
- file: args['INFILE'],
24
- port: args['--port'],
25
- bind: args['--bind'],
26
- rtl: args['--rtl']
27
- }
28
-
29
- say "Starting server at !undblu!localhost:#{opts[:port]}!txtrst!"
30
- if ENV['GITHUB_ACCESS_TOKEN']
31
- say "To view the GitHub API version go to !undblu!localhost:#{opts[:port]}/github!txtrst!"
32
- end
33
- say ''
34
-
35
- Madman::Server.options opts
20
+ dir = args['FOLDER'] || '.'
21
+ port = args['--port']
22
+ bind = args['--bind']
23
+ renderer = args['--github'] ? :github : :default
24
+
25
+ say "Starting server at !undblu!localhost:#{port}!txtrst! using the !txtgrn!#{renderer}!txtrst! renderer\n"
26
+
27
+ Madman::Server.set bind: bind, port: port,
28
+ dir: dir, renderer: renderer
29
+
36
30
  Madman::Server.run!
37
31
  end
38
32
 
@@ -0,0 +1,32 @@
1
+ module Madman
2
+ # Represents a directory with markdown file sand subflders.
3
+ class Directory
4
+ attr_reader :dir, :basedir
5
+
6
+ def initialize(dir, basedir=nil)
7
+ @dir = dir
8
+ @basedir = basedir || dir
9
+ end
10
+
11
+ def list
12
+ @list ||= (dirs + files)
13
+ end
14
+
15
+ private
16
+
17
+ def files
18
+ result = Dir["#{dir}/*.md"]
19
+ result.reject! { |f| File.basename(f) == 'README.md' }
20
+ result.sort.map { |path| Item.new path, :file, basedir }
21
+ end
22
+
23
+ def dirs
24
+ result = Dir["#{dir}/*"].select { |f| File.directory? f }
25
+ result.reject! do |f|
26
+ basename = File.basename(f)
27
+ basename =~ /^[a-z_\-0-9]+$/
28
+ end
29
+ result.sort.map { |path| Item.new path, :dir, basedir }
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,9 @@
1
1
  module Madman
2
2
  class Document
3
- attr_reader :text, :filename
3
+ include Injector
4
+
5
+ attr_reader :filename
6
+ attr_accessor :text
4
7
 
5
8
  def self.from_file(file)
6
9
  new File.read(file), file
@@ -15,8 +18,22 @@ module Madman
15
18
  renderers[renderer].render text
16
19
  end
17
20
 
21
+ def rtl?
22
+ detector.direction(text[0..200]) == 'rtl'
23
+ end
24
+
25
+ def save(save_as=nil)
26
+ save_as ||= filename
27
+ raise ArgumentError, "No filename provided" unless save_as
28
+ File.write save_as, text
29
+ end
30
+
18
31
  private
19
32
 
33
+ def detector
34
+ @detector ||= StringDirection::Detector.new :dominant
35
+ end
36
+
20
37
  def renderers
21
38
  Renderers.available_renderers
22
39
  end
@@ -0,0 +1,18 @@
1
+ module Madman
2
+ module Injector
3
+ def inject(content, marker:, force: false)
4
+ marker = "<!-- #{marker} -->"
5
+ found_markers = text.scan(marker).size
6
+
7
+ content = [marker, content, marker].join "\n"
8
+
9
+ if found_markers == 0 and force
10
+ self.text += "\n\n#{content}"
11
+ elsif found_markers == 1
12
+ text.gsub! /#{marker}/im, content
13
+ elsif found_markers == 2
14
+ text.gsub! /#{marker}.*#{marker}/im, content
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,32 @@
1
+ module Madman
2
+ class Item
3
+ attr_reader :path, :type, :basedir
4
+
5
+ def initialize(path, type, basedir)
6
+ @path, @type, @basedir = path, type, basedir
7
+ end
8
+
9
+ def label
10
+ @label ||= File.basename(path_without_extension)
11
+ end
12
+
13
+ def href
14
+ URI.escape(path_without_extension.sub(/^#{basedir}\//, ''))
15
+ end
16
+
17
+ def dir?
18
+ type == :dir
19
+ end
20
+
21
+ def file?
22
+ type == :file
23
+ end
24
+
25
+ private
26
+
27
+ def path_without_extension
28
+ @path_without_extension ||= path.sub(/\.md$/, '')
29
+ end
30
+ end
31
+ end
32
+
@@ -0,0 +1,45 @@
1
+ module Madman
2
+ # Generate a markdown Table of Contents
3
+ class Navigation
4
+ attr_reader :dir, :depth
5
+
6
+ def initialize(dir='.', depth: 10)
7
+ @dir, @depth = dir, depth
8
+ end
9
+
10
+ def toc
11
+ @toc ||= toc!
12
+ end
13
+
14
+ def markdown
15
+ @markdown ||= markdown!
16
+ end
17
+
18
+ private
19
+
20
+ def markdown!
21
+ result = []
22
+ toc.each do |item|
23
+ indent = item[:level] * 4
24
+ result.push "#{' ' * indent}1. [#{item[:item].label}](#{item[:item].href})"
25
+ end
26
+ result.join "\n"
27
+ end
28
+
29
+ def toc!(path=dir, level=0)
30
+ return [] unless level < depth
31
+ list = Directory.new(path, dir).list
32
+
33
+ result = []
34
+ list.each do |item|
35
+ if item.type == :dir
36
+ result.push level: level, item: item
37
+ result += toc! item.path, level+1
38
+ elsif item.type == :file
39
+ result.push level: level, item: item
40
+ end
41
+ end
42
+ result
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ module Madman
2
+ class PreviewServer < ServerBase
3
+ set :public_folder, -> { File.expand_path(File.dirname(settings.file)) }
4
+
5
+ before do
6
+ @doc = Document.from_file settings.file
7
+ @renderer = :default
8
+ end
9
+
10
+ get '/' do
11
+ slim :template
12
+ end
13
+
14
+ get '/github' do
15
+ @renderer = :github
16
+ slim :template
17
+ end
18
+ end
19
+ end
@@ -2,7 +2,7 @@ module Madman
2
2
  module Renderers
3
3
  class GitHub
4
4
  def self.render(text, opts={})
5
- self.client.markdown text
5
+ client.markdown text
6
6
  end
7
7
 
8
8
  private