markdown_helper 0.2.2 → 0.2.3

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
  SHA1:
3
- metadata.gz: d13b25bca8211a7b125220f43823d929aa063f18
4
- data.tar.gz: 5abf171a884b8a945bd5a53b3c20db59eda73079
3
+ metadata.gz: f09669b50665a49012e58c114423d8cadbabdd78
4
+ data.tar.gz: 5e7efd7f10b45d9e55800be245016d8c221451ba
5
5
  SHA512:
6
- metadata.gz: d806efe553b5be7c0b029f1010f51a714d92399cd55e42ff54ccf8361cefa5f4daa7614ecfc4ec034aca729dd7248378de2288652947a30e6feda5dd762df865
7
- data.tar.gz: c27d30e13ac8eb24b5c95b857c103c3b1c0dfe9c7907c68ecbf47b442b6622173d6ae4d05786821d3a0fb0d182cae09db83b79683b3c69e6886b584ef63c26cd
6
+ metadata.gz: 97c756477a554d5b64800c58154a329512817134004a72ea8daa458190ca13332b93844b4810fcbf41b77f85ac5af3991a03d39b39b7053d6aa723aa6ff2217c
7
+ data.tar.gz: 5ddf37cb2ab123d4cc24d6aa60f1a45fea125774cb608e06cb2e8a8138b3e9a923d95c2f953586130ac8ecc971ca525de449c8a3088b0959d2c701d3791ab598
data/.gitignore CHANGED
@@ -7,3 +7,5 @@
7
7
  /pkg/
8
8
  /spec/reports/
9
9
  /tmp/
10
+ /test/tmp/
11
+
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- markdown_helper (0.2.2)
4
+ markdown_helper (0.2.3)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,3 +1,5 @@
1
+ <!-- >>>>>> BEGIN GENERATED FILE (include): SOURCE readme_files/temp_resolved.md -->
2
+ <!-- >>>>>> BEGIN GENERATED FILE (resolve): SOURCE readme_files/README.template.md -->
1
3
  # MarkdownHelper
2
4
 
3
5
  [![Gem Version](https://badge.fury.io/rb/markdown_helper.svg)](https://badge.fury.io/rb/markdown_helper)
@@ -12,9 +14,26 @@ Next feature:
12
14
 
13
15
  * "Slide deck": to chain markdown pages into a series with next/prev navigation links.
14
16
 
17
+ ## How It Works
18
+
19
+ The markdown helper reads a markdown document (template) and writes another markdown document.
20
+
21
+ The template can contain certain instructions that call for file inclusions and image resolutions.
22
+
23
+ By default, the output markdown has added comments that show:
24
+
25
+ * The path to the template file.
26
+ * The path to each included file.
27
+ * The image description (original) for each resolved image file path.
28
+
29
+ You can suppress those comments using the <code>pristine</code> option.
15
30
  ## File Inclusion
16
31
 
32
+ <!-- >>>>>> BEGIN RESOLVED IMAGE: DESCRIPTION '![include_icon](images/include.png | width=50)
33
+ ' -->
17
34
  <img src="https://raw.githubusercontent.com/BurdetteLamar/MarkdownHelper/master/images/include.png" alt="include_icon" width="50">
35
+ <!-- <<<<<< END RESOLVED IMAGE: DESCRIPTION '![include_icon](images/include.png | width=50)
36
+ ' -->
18
37
 
19
38
  This markdown helper enables file inclusion in GitHub markdown.
20
39
 
@@ -26,6 +45,7 @@ Use the markdown helper to merge external files into a markdown (</code>.md</cod
26
45
 
27
46
  #### Highlighted Code Block
28
47
 
48
+ <!-- >>>>>> BEGIN INCLUDED FILE (ruby): SOURCE readme_files/include.rb -->
29
49
  <code>include.rb</code>
30
50
  ```ruby
31
51
  class RubyCode
@@ -34,9 +54,11 @@ class RubyCode
34
54
  end
35
55
  end
36
56
  ```
57
+ <!-- <<<<<< END INCLUDED FILE (ruby): SOURCE readme_files/include.rb -->
37
58
 
38
59
  #### Plain Code Block
39
60
 
61
+ <!-- >>>>>> BEGIN INCLUDED FILE (code_block): SOURCE readme_files/include.rb -->
40
62
  <code>include.rb</code>
41
63
  ```
42
64
  class RubyCode
@@ -45,9 +67,14 @@ class RubyCode
45
67
  end
46
68
  end
47
69
  ```
70
+ <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE readme_files/include.rb -->
48
71
 
49
72
  [Note: In the gem documentation, RubyDoc.info chooses to highlight this code block regardless. Go figure.]
50
73
 
74
+ #### Comment
75
+
76
+ Comment text is written into the output between the comment delimiters <code>\<!--</code> and <code>--></code>
77
+
51
78
  #### Verbatim
52
79
 
53
80
  Verbatim text is included unadorned. Most often, verbatim text is markdown to be rendered as part of the markdown page.
@@ -56,12 +83,13 @@ Verbatim text is included unadorned. Most often, verbatim text is markdown to b
56
83
 
57
84
  #### CLI
58
85
 
86
+ <!-- >>>>>> BEGIN INCLUDED FILE (code_block): SOURCE readme_files/../bin/usage/include.txt -->
59
87
  <code>include.txt</code>
60
88
  ```
61
- Usage:
62
-
63
- include template_file_path markdown_file_page
64
-
89
+ Usage: bin/include [options] template_file_path markdown_file_page
90
+ --pristine No comments added
91
+ --help Display help
92
+
65
93
  where
66
94
 
67
95
  * template_file_path is the path to an existing file.
@@ -70,24 +98,33 @@ Usage:
70
98
  Typically:
71
99
 
72
100
  * Both file types are .md.
73
- * The template file contains file inclusion pragmas. See README.md.
101
+ * The template file contains file inclusion descriptions.
74
102
  ```
103
+ <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE readme_files/../bin/usage/include.txt -->
75
104
 
76
105
  #### API
77
106
 
107
+ <!-- >>>>>> BEGIN INCLUDED FILE (ruby): SOURCE readme_files/include_usage.rb -->
78
108
  <code>include_usage.rb</code>
79
109
  ```ruby
80
110
  require 'markdown_helper'
81
111
 
82
- markdown_helper = MarkdownHelper.new
83
112
  template_file_path = 'highlight_ruby_template.md'
84
113
  markdown_file_path = 'highlighted_ruby.md'
114
+ markdown_helper = MarkdownHelper.new
115
+ markdown_helper.include(template_file_path, markdown_file_path)
116
+ # Pristine.
117
+ markdown_helper.pristine = true
118
+ markdown_helper.include(template_file_path, markdown_file_path)
119
+ # Also pristine.
120
+ markdown_helper = MarkdownHelper.new(:pristine => true)
85
121
  markdown_helper.include(template_file_path, markdown_file_path)
86
122
  ```
123
+ <!-- <<<<<< END INCLUDED FILE (ruby): SOURCE readme_files/include_usage.rb -->
87
124
 
88
125
  #### Include Descriptions
89
126
 
90
- Specify each file inclusion via an *include description*, which has the form:
127
+ Specify each file inclusion at the beginning of a line via an *include description*, which has the form:
91
128
 
92
129
  <code>@[</code>*format*<code>]\(</code>*relative_file_path*<code>)</code>
93
130
 
@@ -101,6 +138,7 @@ where:
101
138
 
102
139
  ##### Example Include Descriptions
103
140
 
141
+ <!-- >>>>>> BEGIN INCLUDED FILE (code_block): SOURCE readme_files/include.md -->
104
142
  <code>include.md</code>
105
143
  ```code_block
106
144
  @[ruby](my_ruby.rb)
@@ -109,10 +147,15 @@ where:
109
147
 
110
148
  @[:verbatim](my_markdown.md)
111
149
  ```
150
+ <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE readme_files/include.md -->
112
151
 
113
152
  ## Image Path Resolution
114
153
 
154
+ <!-- >>>>>> BEGIN RESOLVED IMAGE: DESCRIPTION '![image_icon](images/image.png | width=50)
155
+ ' -->
115
156
  <img src="https://raw.githubusercontent.com/BurdetteLamar/MarkdownHelper/master/images/image.png" alt="image_icon" width="50">
157
+ <!-- <<<<<< END RESOLVED IMAGE: DESCRIPTION '![image_icon](images/image.png | width=50)
158
+ ' -->
116
159
 
117
160
  This markdown helper enables image path resolution in GitHub markdown.
118
161
 
@@ -126,12 +169,13 @@ This matters because when markdown becomes part of a Ruby gem, its images will h
126
169
 
127
170
  #### CLI
128
171
 
129
- <code>resolve_image_urls.txt</code>
172
+ <!-- >>>>>> BEGIN INCLUDED FILE (code_block): SOURCE readme_files/../bin/usage/resolve.txt -->
173
+ <code>resolve.txt</code>
130
174
  ```
131
- Usage:
132
-
133
- resolve_image_urls template_file_path markdown_file_page
134
-
175
+ Usage: bin/resolve [options] template_file_path markdown_file_page
176
+ --pristine No comments added
177
+ --help Display help
178
+
135
179
  where
136
180
 
137
181
  * template_file_path is the path to an existing file.
@@ -140,24 +184,33 @@ Usage:
140
184
  Typically:
141
185
 
142
186
  * Both file types are .md.
143
- * The template file contains image relative file paths. See README.md.
187
+ * The template file contains image descriptions.
144
188
  ```
189
+ <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE readme_files/../bin/usage/resolve.txt -->
145
190
 
146
191
  #### API
147
192
 
148
- <code>resolve_image_urls_usage.rb</code>
193
+ <!-- >>>>>> BEGIN INCLUDED FILE (ruby): SOURCE readme_files/resolve_usage.rb -->
194
+ <code>resolve_usage.rb</code>
149
195
  ```ruby
150
196
  require 'markdown_helper'
151
197
 
152
- markdown_helper = MarkdownHelper.new
153
198
  template_file_path = 'template.md'
154
199
  markdown_file_path = 'markdown.md'
155
- markdown_helper.resolve_image_urls(template_file_path, markdown_file_path)
200
+ markdown_helper = MarkdownHelper.new
201
+ markdown_helper.resolve(template_file_path, markdown_file_path)
202
+ # Pristine.
203
+ markdown_helper.pristine = true
204
+ markdown_helper.resolve(template_file_path, markdown_file_path)
205
+ # Also pristine.
206
+ markdown_helper = MarkdownHelper.new(:pristine => true)
207
+ markdown_helper.resolve(template_file_path, markdown_file_path)
156
208
  ```
209
+ <!-- <<<<<< END INCLUDED FILE (ruby): SOURCE readme_files/resolve_usage.rb -->
157
210
 
158
211
  #### Image Descriptions
159
212
 
160
- Specify each image via an *image description*, which has the form:
213
+ Specify each image at the beginning of a line via an *image description*, which has the form:
161
214
 
162
215
  <code>![*alt_text*]\(</code>*relative_file_path* <code>|</code> *attributes*<code>)</code>
163
216
 
@@ -169,18 +222,24 @@ where:
169
222
 
170
223
  ##### Example Image Descriptions
171
224
 
172
- <code>resolve_image_urls.md</code>
225
+ <!-- >>>>>> BEGIN INCLUDED FILE (code_block): SOURCE readme_files/resolve.md -->
226
+ <code>resolve.md</code>
173
227
  ```code_block
174
- ![my_alt](image/image.png)
228
+ ![my_alt](image/image.png)
175
229
 
176
- ![my_alt](image/image.png | width=50)
230
+ ![my_alt](image/image.png | width=50)
177
231
 
178
- ![my_alt](image/image.png| width=50 height=50)
232
+ ![my_alt](image/image.png| width=50 height=50)
179
233
  ```
234
+ <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE readme_files/resolve.md -->
180
235
 
181
236
  ## Image Attributes
182
237
 
238
+ <!-- >>>>>> BEGIN RESOLVED IMAGE: DESCRIPTION '![html_icon](images/html.png | width=50)
239
+ ' -->
183
240
  <img src="https://raw.githubusercontent.com/BurdetteLamar/MarkdownHelper/master/images/html.png" alt="html_icon" width="50">
241
+ <!-- <<<<<< END RESOLVED IMAGE: DESCRIPTION '![html_icon](images/html.png | width=50)
242
+ ' -->
184
243
 
185
244
  This markdown helper enables HTML image attributes in GitHub markdown [image descriptions](https://github.github.com/gfm/#image-description).
186
245
 
@@ -192,12 +251,13 @@ Use the markdown helper to add image attributes in a markdown (</code>.md</code>
192
251
 
193
252
  #### CLI
194
253
 
195
- <code>resolve_image_urls.txt</code>
254
+ <!-- >>>>>> BEGIN INCLUDED FILE (code_block): SOURCE readme_files/../bin/usage/resolve.txt -->
255
+ <code>resolve.txt</code>
196
256
  ```
197
- Usage:
198
-
199
- resolve_image_urls template_file_path markdown_file_page
200
-
257
+ Usage: bin/resolve [options] template_file_path markdown_file_page
258
+ --pristine No comments added
259
+ --help Display help
260
+
201
261
  where
202
262
 
203
263
  * template_file_path is the path to an existing file.
@@ -206,24 +266,33 @@ Usage:
206
266
  Typically:
207
267
 
208
268
  * Both file types are .md.
209
- * The template file contains image relative file paths. See README.md.
269
+ * The template file contains image descriptions.
210
270
  ```
271
+ <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE readme_files/../bin/usage/resolve.txt -->
211
272
 
212
273
  #### API
213
274
 
214
- <code>resolve_image_urls_usage.rb</code>
275
+ <!-- >>>>>> BEGIN INCLUDED FILE (ruby): SOURCE readme_files/resolve_usage.rb -->
276
+ <code>resolve_usage.rb</code>
215
277
  ```ruby
216
278
  require 'markdown_helper'
217
279
 
218
- markdown_helper = MarkdownHelper.new
219
280
  template_file_path = 'template.md'
220
281
  markdown_file_path = 'markdown.md'
221
- markdown_helper.resolve_image_urls(template_file_path, markdown_file_path)
282
+ markdown_helper = MarkdownHelper.new
283
+ markdown_helper.resolve(template_file_path, markdown_file_path)
284
+ # Pristine.
285
+ markdown_helper.pristine = true
286
+ markdown_helper.resolve(template_file_path, markdown_file_path)
287
+ # Also pristine.
288
+ markdown_helper = MarkdownHelper.new(:pristine => true)
289
+ markdown_helper.resolve(template_file_path, markdown_file_path)
222
290
  ```
291
+ <!-- <<<<<< END INCLUDED FILE (ruby): SOURCE readme_files/resolve_usage.rb -->
223
292
 
224
293
  #### Image Descriptions
225
294
 
226
- Specify each image via an *image description*, which has the form:
295
+ Specify each image at the beginning of a line via an *image description*, which has the form:
227
296
 
228
297
  <code>![*alt_text*]\(</code>*relative_file_path* <code>|</code> *attributes*<code>)</code>
229
298
 
@@ -235,12 +304,16 @@ where:
235
304
 
236
305
  ##### Example Image Descriptions
237
306
 
238
- <code>resolve_image_urls.md</code>
307
+ <!-- >>>>>> BEGIN INCLUDED FILE (code_block): SOURCE readme_files/resolve.md -->
308
+ <code>resolve.md</code>
239
309
  ```code_block
240
- ![my_alt](image/image.png)
310
+ ![my_alt](image/image.png)
241
311
 
242
- ![my_alt](image/image.png | width=50)
312
+ ![my_alt](image/image.png | width=50)
243
313
 
244
- ![my_alt](image/image.png| width=50 height=50)
314
+ ![my_alt](image/image.png| width=50 height=50)
245
315
  ```
316
+ <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE readme_files/resolve.md -->
246
317
 
318
+ <!-- <<<<<< END GENERATED FILE (resolve): SOURCE readme_files/README.template.md -->
319
+ <!-- <<<<<< END GENERATED FILE (include): SOURCE readme_files/temp_resolved.md -->
data/Rakefile CHANGED
@@ -11,11 +11,31 @@ namespace :build do
11
11
 
12
12
  desc 'Build README.md file from README.template.md'
13
13
  task :readme do
14
+ Rake::Task['build:usages'].invoke
14
15
  require_relative 'lib/markdown_helper'
15
16
  markdown_helper = MarkdownHelper.new
16
17
  markdown_helper.include('readme_files/highlight_ruby_template.md', 'readme_files/highlighted_ruby.md')
17
- markdown_helper.include('readme_files/README.template.md', 'README.md')
18
- markdown_helper.resolve_image_urls('README.md', 'README.md')
18
+ # Do the resolve before the include, so that the included text is not also resolved.
19
+ # Thie protects example code from being also resolved, thus damaging the example code.
20
+ # Temp file must be in the same directory as its source (it becomes the source).
21
+ temp_file_path = 'readme_files/temp_resolved.md'
22
+ markdown_helper.resolve('readme_files/README.template.md', temp_file_path)
23
+ markdown_helper.include(temp_file_path, 'README.md')
24
+ File.delete(temp_file_path)
25
+ end
26
+
27
+ desc 'Build usage for executables'
28
+ task :usages do
29
+ %w/
30
+ include
31
+ resolve
32
+ /.each do |executable_name|
33
+ usage_text = `ruby bin/#{executable_name} --help`
34
+ usage_file_path = "bin/usage/#{executable_name}.txt"
35
+ File.open(usage_file_path, 'w') do |file|
36
+ file.puts(usage_text)
37
+ end
38
+ end
19
39
  end
20
40
 
21
41
  end
@@ -1,23 +1,44 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
+ require 'optparse'
4
+
3
5
  require 'markdown_helper'
4
6
 
5
- def usage
6
- dir_path = File.dirname(File.absolute_path(__FILE__))
7
- file_path = File.join(
8
- dir_path,
9
- 'usage',
10
- 'include.txt'
11
- )
12
- puts File.read(file_path)
7
+ options = {:pristine => false}
8
+
9
+ parser = OptionParser.new do|opts|
10
+ opts.banner = "Usage: #{__FILE__} [options] template_file_path markdown_file_page"
11
+ opts.on('--pristine', 'No comments added') do |_|
12
+ options[:pristine] = true
13
+ end
14
+ opts.on('--help', 'Display help') do
15
+ usage(opts)
16
+ end
17
+ end
18
+
19
+ def usage(opts)
20
+ puts opts
21
+ puts <<-EOT
22
+
23
+ where
24
+
25
+ * template_file_path is the path to an existing file.
26
+ * markdown_file_path is the path to a file to be created.
27
+
28
+ Typically:
29
+
30
+ * Both file types are .md.
31
+ * The template file contains file inclusion descriptions.
32
+ EOT
13
33
  exit
14
34
  end
15
35
 
16
- template_file_path, markdown_file_path = ARGV
36
+ parser.parse!
17
37
 
18
- usage unless ARGV.size == 2
19
- usage unless File.readable?(template_file_path)
20
- usage unless File.writable?(File.dirname(markdown_file_path))
38
+ template_file_path, markdown_file_path = ARGV
21
39
 
22
- MarkdownHelper.new.include(template_file_path, markdown_file_path)
40
+ usage(options) unless ARGV.size == 2
41
+ usage(options) unless File.readable?(template_file_path)
42
+ usage(options) unless File.writable?(File.dirname(markdown_file_path))
23
43
 
44
+ MarkdownHelper.new(options).include(template_file_path, markdown_file_path)
@@ -0,0 +1,44 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ require 'markdown_helper'
6
+
7
+ options = {:pristine => false}
8
+
9
+ parser = OptionParser.new do|opts|
10
+ opts.banner = "Usage: #{__FILE__} [options] template_file_path markdown_file_page"
11
+ opts.on('--pristine', 'No comments added') do |_|
12
+ options[:pristine] = true
13
+ end
14
+ opts.on('--help', 'Display help') do
15
+ usage(opts)
16
+ end
17
+ end
18
+
19
+ def usage(opts)
20
+ puts opts
21
+ puts <<-EOT
22
+
23
+ where
24
+
25
+ * template_file_path is the path to an existing file.
26
+ * markdown_file_path is the path to a file to be created.
27
+
28
+ Typically:
29
+
30
+ * Both file types are .md.
31
+ * The template file contains image descriptions.
32
+ EOT
33
+ exit
34
+ end
35
+
36
+ parser.parse!
37
+
38
+ template_file_path, markdown_file_path = ARGV
39
+
40
+ usage(options) unless ARGV.size == 2
41
+ usage(options) unless File.readable?(template_file_path)
42
+ usage(options) unless File.writable?(File.dirname(markdown_file_path))
43
+
44
+ MarkdownHelper.new(options).resolve(template_file_path, markdown_file_path)
@@ -1,7 +1,7 @@
1
- Usage:
2
-
3
- include template_file_path markdown_file_page
4
-
1
+ Usage: bin/include [options] template_file_path markdown_file_page
2
+ --pristine No comments added
3
+ --help Display help
4
+
5
5
  where
6
6
 
7
7
  * template_file_path is the path to an existing file.
@@ -10,4 +10,4 @@ Usage:
10
10
  Typically:
11
11
 
12
12
  * Both file types are .md.
13
- * The template file contains file inclusion pragmas. See README.md.
13
+ * The template file contains file inclusion descriptions.
@@ -0,0 +1,13 @@
1
+ Usage: bin/resolve [options] template_file_path markdown_file_page
2
+ --pristine No comments added
3
+ --help Display help
4
+
5
+ where
6
+
7
+ * template_file_path is the path to an existing file.
8
+ * markdown_file_path is the path to a file to be created.
9
+
10
+ Typically:
11
+
12
+ * Both file types are .md.
13
+ * The template file contains image descriptions.
@@ -72,11 +72,11 @@ This matters because when markdown becomes part of a Ruby gem, its images will h
72
72
 
73
73
  #### CLI
74
74
 
75
- @[:code_block](../bin/usage/resolve_image_urls.txt)
75
+ @[:code_block](../bin/usage/resolve.txt)
76
76
 
77
77
  #### API
78
78
 
79
- @[ruby](resolve_image_urls_usage.rb)
79
+ @[ruby](resolve_usage.rb)
80
80
 
81
81
  #### Image Pragmas
82
82
 
@@ -92,5 +92,5 @@ where:
92
92
 
93
93
  ##### Example Image Pragmas
94
94
 
95
- @[code_block](resolve_image_urls.md)
95
+ @[code_block](resolve.md)
96
96
 
@@ -9,6 +9,23 @@ class MarkdownHelper
9
9
  IMAGE_REGEXP = /^!\[([^\[]+)\]\(([^)]+)\)/
10
10
  INCLUDE_REGEXP = /^@\[([^\[]+)\]\(([^)]+)\)/
11
11
 
12
+ attr_accessor :pristine
13
+
14
+ def initialize(options = {})
15
+ default_options = {
16
+ :pristine => false,
17
+ }
18
+ merged_options = default_options.merge(options)
19
+ merged_options.each_pair do |method, value|
20
+ unless self.respond_to?(method)
21
+ raise ArgumentError.new("Unknown option: #{method}")
22
+ end
23
+ setter_method = "#{method}="
24
+ send(setter_method, value)
25
+ merged_options.delete(method)
26
+ end
27
+ end
28
+
12
29
  # Get the user name and repository name from ENV.
13
30
  def repo_user_and_name
14
31
  repo_user = ENV['REPO_USER']
@@ -34,9 +51,8 @@ class MarkdownHelper
34
51
  # @example pragma to include text verbatim, to be rendered as markdown.
35
52
  # @[:verbatim](foo.md)
36
53
  def include(template_file_path, markdown_file_path)
37
- output_lines = []
38
- File.open(template_file_path, 'r') do |template_file|
39
- template_file.each_line do |input_line|
54
+ output = send(:generate_file, template_file_path, markdown_file_path, __method__) do |input_lines, output_lines|
55
+ input_lines.each do |input_line|
40
56
  match_data = input_line.match(INCLUDE_REGEXP)
41
57
  unless match_data
42
58
  output_lines.push(input_line)
@@ -47,6 +63,8 @@ class MarkdownHelper
47
63
  :code_block
48
64
  when ':verbatim'
49
65
  :verbatim
66
+ when ':comment'
67
+ :comment
50
68
  else
51
69
  match_data[1]
52
70
  end
@@ -55,30 +73,9 @@ class MarkdownHelper
55
73
  File.dirname(template_file_path),
56
74
  relative_file_path,
57
75
  )
58
- included_text = File.read(include_file_path)
59
- unless included_text.match("\n")
60
- message = "Warning: Included file has no trailing newline: #{include_file_path}"
61
- warn(message)
62
- end
63
- if treatment == :verbatim
64
- # Pass through unadorned.
65
- output_lines.push(included_text)
66
- else
67
- # Use the file name as a label.
68
- file_name_line = format("<code>%s</code>\n", File.basename(include_file_path))
69
- output_lines.push(file_name_line)
70
- # Put into code block.
71
- language = treatment == :code_block ? '' : treatment
72
- output_lines.push("```#{language}\n")
73
- output_lines.push(included_text)
74
- output_lines.push("```\n")
75
- end
76
+ send(:include_file, include_file_path, treatment, output_lines)
76
77
  end
77
78
  end
78
- output = output_lines.join('')
79
- File.open(markdown_file_path, 'w') do |md_file|
80
- md_file.write(output)
81
- end
82
79
  output
83
80
  end
84
81
 
@@ -103,34 +100,65 @@ class MarkdownHelper
103
100
  # "https://raw.githubusercontent.com/#{repo_user}/#{repo_name}/master",
104
101
  # relative_file_path,
105
102
  # )
106
- def resolve_image_urls(template_file_path, markdown_file_path)
107
- output_lines = []
108
- File.open(template_file_path, 'r') do |template_file|
109
- output_lines = []
110
- template_file.each_line do |input_line|
103
+ def resolve(template_file_path, markdown_file_path)
104
+ # Method :generate_file does the first things, yields the block, does the last things.
105
+ output = send(:generate_file, template_file_path, markdown_file_path, __method__) do |input_lines, output_lines|
106
+ input_lines.each do |input_line|
111
107
  match_data = input_line.match(IMAGE_REGEXP)
112
108
  unless match_data
113
109
  output_lines.push(input_line)
114
110
  next
115
111
  end
116
- alt_text = match_data[1]
117
- relative_file_path, attributes_s = match_data[2].split(/\s?\|\s?/, 2)
118
- attributes = attributes_s ? attributes_s.split(/\s+/) : []
119
- formatted_attributes = ['']
120
- attributes.each do |attribute|
121
- name, value = attribute.split('=', 2)
122
- formatted_attributes.push(format('%s="%s"', name, value))
112
+ send(:comment_resolve, input_line, output_lines) do
113
+ alt_text = match_data[1]
114
+ relative_file_path, attributes_s = match_data[2].split(/\s?\|\s?/, 2)
115
+ attributes = attributes_s ? attributes_s.split(/\s+/) : []
116
+ formatted_attributes = ['']
117
+ attributes.each do |attribute|
118
+ name, value = attribute.split('=', 2)
119
+ formatted_attributes.push(format('%s="%s"', name, value))
120
+ end
121
+ formatted_attributes_s = formatted_attributes.join(' ')
122
+ repo_user, repo_name = repo_user_and_name
123
+ absolute_file_path = nil
124
+ if relative_file_path.start_with?('http')
125
+ absolute_file_path = relative_file_path
126
+ else
127
+ absolute_file_path = File.join(
128
+ "https://raw.githubusercontent.com/#{repo_user}/#{repo_name}/master",
129
+ relative_file_path,
130
+ )
131
+ end
132
+ following_text = input_line.sub(IMAGE_REGEXP, '').chomp
133
+ line = format(
134
+ '<img src="%s" alt="%s"%s>%s',
135
+ absolute_file_path,
136
+ alt_text,
137
+ formatted_attributes_s,
138
+ following_text
139
+ ) + "\n"
140
+ output_lines.push(line)
123
141
  end
124
- formatted_attributes_s = formatted_attributes.join(' ')
125
- repo_user, repo_name = repo_user_and_name
126
- absolute_file_path = File.join(
127
- "https://raw.githubusercontent.com/#{repo_user}/#{repo_name}/master",
128
- relative_file_path,
129
- )
130
- line = format('<img src="%s" alt="%s"%s>', absolute_file_path, alt_text, formatted_attributes_s) + "\n"
131
- output_lines.push(line)
132
142
  end
133
143
  end
144
+ output
145
+ end
146
+ alias resolve_image_urls resolve
147
+
148
+ def comment(text)
149
+ "<!--#{text}-->\n"
150
+ end
151
+
152
+ private
153
+
154
+ def generate_file(template_file_path, markdown_file_path, method)
155
+ output_lines = []
156
+ File.open(template_file_path, 'r') do |template_file|
157
+ output_lines.push(comment(" >>>>>> BEGIN GENERATED FILE (#{method.to_s}): SOURCE #{template_file_path} ")) unless pristine
158
+ input_lines = template_file.readlines
159
+ yield input_lines, output_lines
160
+ output_lines.push(comment(" <<<<<< END GENERATED FILE (#{method.to_s}): SOURCE #{template_file_path} ")) unless pristine
161
+ end
134
162
  output = output_lines.join('')
135
163
  File.open(markdown_file_path, 'w') do |md_file|
136
164
  md_file.write(output)
@@ -138,4 +166,37 @@ class MarkdownHelper
138
166
  output
139
167
  end
140
168
 
169
+ def include_file(include_file_path, treatment, output_lines)
170
+ include_file = File.new(include_file_path, 'r')
171
+ output_lines.push(comment(" >>>>>> BEGIN INCLUDED FILE (#{treatment}): SOURCE #{include_file.path} ")) unless pristine
172
+ included_text = include_file.read
173
+ unless included_text.match("\n")
174
+ message = "Warning: Included file has no trailing newline: #{include_file_path}"
175
+ warn(message)
176
+ end
177
+ case treatment
178
+ when :verbatim
179
+ # Pass through unadorned.
180
+ output_lines.push(included_text)
181
+ when :comment
182
+ output_lines.push(comment(included_text))
183
+ else
184
+ # Use the file name as a label.
185
+ file_name_line = format("<code>%s</code>\n", File.basename(include_file_path))
186
+ output_lines.push(file_name_line)
187
+ # Put into code block.
188
+ language = treatment == :code_block ? '' : treatment
189
+ output_lines.push("```#{language}\n")
190
+ output_lines.push(included_text)
191
+ output_lines.push("```\n")
192
+ end
193
+ output_lines.push(comment(" <<<<<< END INCLUDED FILE (#{treatment}): SOURCE #{include_file.path} ")) unless pristine
194
+ end
195
+
196
+ def comment_resolve(description, output_lines)
197
+ output_lines.push(comment(" >>>>>> BEGIN RESOLVED IMAGE: DESCRIPTION '#{description}' ")) unless pristine
198
+ yield
199
+ output_lines.push(comment(" <<<<<< END RESOLVED IMAGE: DESCRIPTION '#{description}' ")) unless pristine
200
+ end
201
+
141
202
  end
@@ -1,3 +1,3 @@
1
1
  class MarkdownHelper
2
- VERSION = '0.2.2'
2
+ VERSION = '0.2.3'
3
3
  end
@@ -12,6 +12,19 @@ Next feature:
12
12
 
13
13
  * "Slide deck": to chain markdown pages into a series with next/prev navigation links.
14
14
 
15
+ ## How It Works
16
+
17
+ The markdown helper reads a markdown document (template) and writes another markdown document.
18
+
19
+ The template can contain certain instructions that call for file inclusions and image resolutions.
20
+
21
+ By default, the output markdown has added comments that show:
22
+
23
+ * The path to the template file.
24
+ * The path to each included file.
25
+ * The image description (original) for each resolved image file path.
26
+
27
+ You can suppress those comments using the <code>pristine</code> option.
15
28
  ## File Inclusion
16
29
 
17
30
  ![include_icon](images/include.png | width=50)
@@ -34,6 +47,10 @@ Use the markdown helper to merge external files into a markdown (</code>.md</cod
34
47
 
35
48
  [Note: In the gem documentation, RubyDoc.info chooses to highlight this code block regardless. Go figure.]
36
49
 
50
+ #### Comment
51
+
52
+ Comment text is written into the output between the comment delimiters <code>\<!--</code> and <code>--></code>
53
+
37
54
  #### Verbatim
38
55
 
39
56
  Verbatim text is included unadorned. Most often, verbatim text is markdown to be rendered as part of the markdown page.
@@ -50,7 +67,7 @@ Verbatim text is included unadorned. Most often, verbatim text is markdown to b
50
67
 
51
68
  #### Include Descriptions
52
69
 
53
- Specify each file inclusion via an *include description*, which has the form:
70
+ Specify each file inclusion at the beginning of a line via an *include description*, which has the form:
54
71
 
55
72
  <code>@[</code>*format*<code>]\(</code>*relative_file_path*<code>)</code>
56
73
 
@@ -82,15 +99,15 @@ This matters because when markdown becomes part of a Ruby gem, its images will h
82
99
 
83
100
  #### CLI
84
101
 
85
- @[:code_block](../bin/usage/resolve_image_urls.txt)
102
+ @[:code_block](../bin/usage/resolve.txt)
86
103
 
87
104
  #### API
88
105
 
89
- @[ruby](resolve_image_urls_usage.rb)
106
+ @[ruby](resolve_usage.rb)
90
107
 
91
108
  #### Image Descriptions
92
109
 
93
- Specify each image via an *image description*, which has the form:
110
+ Specify each image at the beginning of a line via an *image description*, which has the form:
94
111
 
95
112
  <code>![*alt_text*]\(</code>*relative_file_path* <code>|</code> *attributes*<code>)</code>
96
113
 
@@ -102,7 +119,7 @@ where:
102
119
 
103
120
  ##### Example Image Descriptions
104
121
 
105
- @[code_block](resolve_image_urls.md)
122
+ @[code_block](resolve.md)
106
123
 
107
124
  ## Image Attributes
108
125
 
@@ -118,15 +135,15 @@ Use the markdown helper to add image attributes in a markdown (</code>.md</code>
118
135
 
119
136
  #### CLI
120
137
 
121
- @[:code_block](../bin/usage/resolve_image_urls.txt)
138
+ @[:code_block](../bin/usage/resolve.txt)
122
139
 
123
140
  #### API
124
141
 
125
- @[ruby](resolve_image_urls_usage.rb)
142
+ @[ruby](resolve_usage.rb)
126
143
 
127
144
  #### Image Descriptions
128
145
 
129
- Specify each image via an *image description*, which has the form:
146
+ Specify each image at the beginning of a line via an *image description*, which has the form:
130
147
 
131
148
  <code>![*alt_text*]\(</code>*relative_file_path* <code>|</code> *attributes*<code>)</code>
132
149
 
@@ -138,5 +155,5 @@ where:
138
155
 
139
156
  ##### Example Image Descriptions
140
157
 
141
- @[code_block](resolve_image_urls.md)
158
+ @[code_block](resolve.md)
142
159
 
@@ -1,3 +1,5 @@
1
+ <!-- >>>>>> BEGIN GENERATED FILE (include): SOURCE readme_files/highlight_ruby_template.md -->
2
+ <!-- >>>>>> BEGIN INCLUDED FILE (ruby): SOURCE readme_files/include.rb -->
1
3
  <code>include.rb</code>
2
4
  ```ruby
3
5
  class RubyCode
@@ -6,3 +8,5 @@ class RubyCode
6
8
  end
7
9
  end
8
10
  ```
11
+ <!-- <<<<<< END INCLUDED FILE (ruby): SOURCE readme_files/include.rb -->
12
+ <!-- <<<<<< END GENERATED FILE (include): SOURCE readme_files/highlight_ruby_template.md -->
@@ -1,6 +1,12 @@
1
1
  require 'markdown_helper'
2
2
 
3
- markdown_helper = MarkdownHelper.new
4
3
  template_file_path = 'highlight_ruby_template.md'
5
4
  markdown_file_path = 'highlighted_ruby.md'
5
+ markdown_helper = MarkdownHelper.new
6
+ markdown_helper.include(template_file_path, markdown_file_path)
7
+ # Pristine.
8
+ markdown_helper.pristine = true
9
+ markdown_helper.include(template_file_path, markdown_file_path)
10
+ # Also pristine.
11
+ markdown_helper = MarkdownHelper.new(:pristine => true)
6
12
  markdown_helper.include(template_file_path, markdown_file_path)
@@ -0,0 +1,5 @@
1
+ ![my_alt](image/image.png)
2
+
3
+ ![my_alt](image/image.png | width=50)
4
+
5
+ ![my_alt](image/image.png| width=50 height=50)
@@ -0,0 +1,12 @@
1
+ require 'markdown_helper'
2
+
3
+ template_file_path = 'template.md'
4
+ markdown_file_path = 'markdown.md'
5
+ markdown_helper = MarkdownHelper.new
6
+ markdown_helper.resolve(template_file_path, markdown_file_path)
7
+ # Pristine.
8
+ markdown_helper.pristine = true
9
+ markdown_helper.resolve(template_file_path, markdown_file_path)
10
+ # Also pristine.
11
+ markdown_helper = MarkdownHelper.new(:pristine => true)
12
+ markdown_helper.resolve(template_file_path, markdown_file_path)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: markdown_helper
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - burdettelamar
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-03-10 00:00:00.000000000 Z
11
+ date: 2018-03-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -89,10 +89,10 @@ files:
89
89
  - Rakefile
90
90
  - bin/console
91
91
  - bin/include
92
- - bin/resolve_image_urls
92
+ - bin/resolve
93
93
  - bin/setup
94
94
  - bin/usage/include.txt
95
- - bin/usage/resolve_image_urls.txt
95
+ - bin/usage/resolve.txt
96
96
  - images/html.png
97
97
  - images/image.png
98
98
  - images/include.png
@@ -107,8 +107,8 @@ files:
107
107
  - readme_files/include.md
108
108
  - readme_files/include.rb
109
109
  - readme_files/include_usage.rb
110
- - readme_files/resolve_image_urls.md
111
- - readme_files/resolve_image_urls_usage.rb
110
+ - readme_files/resolve.md
111
+ - readme_files/resolve_usage.rb
112
112
  - readme_files/verbatim_ruby_template.md
113
113
  homepage: https://github.com/BurdetteLamar/MarkdownHelper
114
114
  licenses:
@@ -1,22 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require 'markdown_helper'
4
-
5
- def usage
6
- dir_path = File.dirname(File.absolute_path(__FILE__))
7
- file_path = File.join(
8
- dir_path,
9
- 'usage',
10
- 'resolve_image_urls.txt'
11
- )
12
- puts File.read(file_path)
13
- exit
14
- end
15
-
16
- template_file_path, markdown_file_path = ARGV
17
-
18
- usage unless ARGV.size == 2
19
- usage unless File.readable?(template_file_path)
20
- usage unless File.writable?(File.dirname(markdown_file_path))
21
-
22
- MarkdownHelper.new.resolve_image_urls(template_file_path, markdown_file_path)
@@ -1,13 +0,0 @@
1
- Usage:
2
-
3
- resolve_image_urls template_file_path markdown_file_page
4
-
5
- where
6
-
7
- * template_file_path is the path to an existing file.
8
- * markdown_file_path is the path to a file to be created.
9
-
10
- Typically:
11
-
12
- * Both file types are .md.
13
- * The template file contains image relative file paths. See README.md.
@@ -1,5 +0,0 @@
1
- ![my_alt](image/image.png)
2
-
3
- ![my_alt](image/image.png | width=50)
4
-
5
- ![my_alt](image/image.png| width=50 height=50)
@@ -1,6 +0,0 @@
1
- require 'markdown_helper'
2
-
3
- markdown_helper = MarkdownHelper.new
4
- template_file_path = 'template.md'
5
- markdown_file_path = 'markdown.md'
6
- markdown_helper.resolve_image_urls(template_file_path, markdown_file_path)