markdown_helper 1.9.0 → 1.9.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile.lock +1 -1
  3. data/README.md +16 -14
  4. data/Rakefile +1 -0
  5. data/bin/_create_page_toc +46 -0
  6. data/bin/usage/create_page_toc.txt +14 -0
  7. data/lib/markdown_helper.rb +81 -49
  8. data/lib/markdown_helper/version.rb +1 -1
  9. data/lib/string_helper.rb +14 -0
  10. data/markdown/readme/README.template.md +16 -14
  11. data/markdown/use_cases/Rakefile +10 -4
  12. data/markdown/use_cases/include_files/diagnose_circular_includes/diagnose_circular_includes.err +26 -0
  13. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/diagnose_circular_includes.rb +0 -0
  14. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/include.rb +0 -0
  15. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/includer.md +0 -0
  16. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/includer_0.md +0 -0
  17. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/includer_1.md +0 -0
  18. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/includer_2.md +0 -0
  19. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/use_case.md +7 -7
  20. data/markdown/use_cases/{include → include_files}/diagnose_circular_includes/use_case_template.md +0 -0
  21. data/markdown/use_cases/include_files/diagnose_missing_includee/diagnose_missing_includee.err +26 -0
  22. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/diagnose_missing_includee.rb +0 -0
  23. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/include.rb +0 -0
  24. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/included.md +0 -0
  25. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/includer.md +0 -0
  26. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/includer_0.md +0 -0
  27. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/includer_1.md +0 -0
  28. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/includer_2.md +0 -0
  29. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/use_case.md +8 -8
  30. data/markdown/use_cases/{include → include_files}/diagnose_missing_includee/use_case_template.md +0 -0
  31. data/markdown/use_cases/{include → include_files}/include.rb +0 -0
  32. data/markdown/use_cases/{include → include_files}/include_code_block/hello.rb +0 -0
  33. data/markdown/use_cases/{include → include_files}/include_code_block/include_code_block.rb +1 -1
  34. data/markdown/use_cases/{include → include_files}/include_code_block/included.md +0 -0
  35. data/markdown/use_cases/{include → include_files}/include_code_block/includer.md +0 -0
  36. data/markdown/use_cases/{include → include_files}/include_code_block/use_case.md +1 -1
  37. data/markdown/use_cases/{include → include_files}/include_code_block/use_case_template.md +1 -1
  38. data/markdown/use_cases/{include → include_files}/include_generated_text/include_generated_text.rb +0 -0
  39. data/markdown/use_cases/{include → include_files}/include_generated_text/use_case.md +0 -0
  40. data/markdown/use_cases/{include → include_files}/include_generated_text/use_case_template.md +0 -0
  41. data/markdown/use_cases/{include → include_files}/include_highlighted_code/hello.rb +0 -0
  42. data/markdown/use_cases/{include → include_files}/include_highlighted_code/include_highlighted_code.rb +0 -0
  43. data/markdown/use_cases/{include → include_files}/include_highlighted_code/included.md +0 -0
  44. data/markdown/use_cases/{include → include_files}/include_highlighted_code/includer.md +0 -0
  45. data/markdown/use_cases/{include → include_files}/include_highlighted_code/use_case.md +0 -0
  46. data/markdown/use_cases/{include → include_files}/include_highlighted_code/use_case_template.md +0 -0
  47. data/markdown/use_cases/{include → include_files}/include_markdown/include_markdown.rb +0 -0
  48. data/markdown/use_cases/{include → include_files}/include_markdown/included.md +0 -0
  49. data/markdown/use_cases/{include → include_files}/include_markdown/includer.md +0 -0
  50. data/markdown/use_cases/{include → include_files}/include_markdown/markdown.md +0 -0
  51. data/markdown/use_cases/{include → include_files}/include_markdown/use_case.md +0 -0
  52. data/markdown/use_cases/{include → include_files}/include_markdown/use_case_template.md +0 -0
  53. data/markdown/use_cases/{include → include_files}/include_use_case.rb +1 -1
  54. data/markdown/use_cases/{include → include_files}/include_with_added_comments/include_with_added_comments.rb +0 -0
  55. data/markdown/use_cases/{include → include_files}/include_with_added_comments/included.md +2 -2
  56. data/markdown/use_cases/{include → include_files}/include_with_added_comments/includee.md +0 -0
  57. data/markdown/use_cases/{include → include_files}/include_with_added_comments/includer.md +0 -0
  58. data/markdown/use_cases/{include → include_files}/include_with_added_comments/use_case.md +2 -2
  59. data/markdown/use_cases/{include → include_files}/include_with_added_comments/use_case_template.md +0 -0
  60. data/markdown/use_cases/{include → include_files}/interface.md +0 -0
  61. data/markdown/use_cases/{include → include_files}/nest_inclusions/included.md +0 -0
  62. data/markdown/use_cases/{include → include_files}/nest_inclusions/includee.md +0 -0
  63. data/markdown/use_cases/{include → include_files}/nest_inclusions/includer.md +0 -0
  64. data/markdown/use_cases/{include → include_files}/nest_inclusions/nest_inclusions.rb +0 -0
  65. data/markdown/use_cases/{include → include_files}/nest_inclusions/nested_includee.md +0 -0
  66. data/markdown/use_cases/{include → include_files}/nest_inclusions/use_case.md +0 -0
  67. data/markdown/use_cases/{include → include_files}/nest_inclusions/use_case_template.md +0 -0
  68. data/markdown/use_cases/{include → include_files}/reuse_text/include.rb +0 -0
  69. data/markdown/use_cases/{include → include_files}/reuse_text/included.md +0 -0
  70. data/markdown/use_cases/{include → include_files}/reuse_text/includee.md +0 -0
  71. data/markdown/use_cases/{include → include_files}/reuse_text/includer.md +0 -0
  72. data/markdown/use_cases/{include → include_files}/reuse_text/reuse_text.rb +0 -0
  73. data/markdown/use_cases/{include → include_files}/reuse_text/use_case.md +0 -0
  74. data/markdown/use_cases/{include → include_files}/reuse_text/use_case_template.md +0 -0
  75. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/create_and_include.rb +6 -0
  76. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/create_and_include.sh +3 -0
  77. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/create_and_include_page_toc.rb +95 -0
  78. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/includer.md +5 -0
  79. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/page.md +38 -0
  80. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/text.md +28 -0
  81. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/toc.md +7 -0
  82. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/use_case.md +62 -0
  83. data/markdown/use_cases/tables_of_contents/create_and_include_page_toc/use_case_template.md +42 -0
  84. data/markdown/use_cases/tables_of_contents/create_page_toc_use_case.rb +81 -0
  85. data/markdown/use_cases/use_cases.md +13 -10
  86. data/markdown_helper.gemspec +1 -0
  87. metadata +79 -65
  88. data/markdown/use_cases/include/diagnose_circular_includes/diagnose_circular_includes.err +0 -26
  89. data/markdown/use_cases/include/diagnose_missing_includee/diagnose_missing_includee.err +0 -26
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c8845145b2e85dd7e7420a4fd9c9c96d5795f01e
4
- data.tar.gz: 088ef4f4cdb02275f43f2c99441f27f34e67e24d
3
+ metadata.gz: f37bc35a877f0cbad86a8441204b6c1d8a6af34c
4
+ data.tar.gz: d56f7ef795b3e7d382ba2a2cd32e5d1461252999
5
5
  SHA512:
6
- metadata.gz: aa95f50a350189ee1caf1987dabc3b6ebd28f65ee6e5e1d53db52ab57102dfa0543212c8d81d6e5dce78c641cc6b50dc34193ab84cdc1b54468af411906fa0db
7
- data.tar.gz: 72ccd0a952302199225dd553c1cd071f70790bd574a0ecfec5e887f771536225e418c93630483b6df92a98cb03db54a289ee1c1ad315c8511fad2346ecf12bac
6
+ metadata.gz: bcbbe35a306f7689ae6dbc65884f9f4b43e3085fbe1c4181e4ccac405ab43f84a962e8290baf961be1fa633403eadf66e8c07eb5c1eb90d13d9cce2d221cb022
7
+ data.tar.gz: dd5a5f2b96c308f817999412ef138e205d3418032f4162fb1b318828c8b3fa02e18f44e10a6b2f02b5cd33e55d06839ead8fafa60d40361fc5eec44db9ad9927
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- markdown_helper (1.9.0)
4
+ markdown_helper (1.9.5)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -10,25 +10,18 @@
10
10
 
11
11
  ## What's New?
12
12
 
13
- * The helper now checks whether the working directory is in a ```git``` project, and raises an exception if not.
14
-
15
- * There is now a backtrace of inclusions when:
16
- * An includee file cannot be read.
17
- * A circular inclusion (direct or indirect) is found.
18
- * The backtrace shows:
19
- * Each includer file path.
20
- * Its include description.
21
- * The includee file path.
22
- * File paths are relative to the git directory.
23
- * Use cases are added:
24
- * [Diagnose Missing Includee](markdown/use_cases/include/diagnose_missing_includee/use_case.md#diagnose-missing-includee).
25
- * [Diagnose Circular Includes](markdown/use_cases/include/diagnose_circular_includes/use_case.md#diagnose-circular-includes).
13
+ Page TOC:
14
+
15
+ - Support is added for creating the table of contents for a markdown page.
16
+ - The TOC is a tree of links to the headers on the page, suitable for inclusion with the page itself.
17
+ - See the [use case](markdown/use_cases/tables_of_contents/create_and_include_page_toc/use_case.md#create-and-include-page-toc).
26
18
 
27
19
  ## What's a Markdown Helper?
28
20
 
29
21
  Class <code>MarkdownHelper</code> supports:
30
22
 
31
23
  * [File inclusion](#file-inclusion): to include text from other files, as code-block or markdown.
24
+ * [Page TOC](#page-toc): to create the table of contents for a markdown page.
32
25
  * [Image path resolution](#image-path-resolution): to resolve relative image paths to absolute URL paths (so they work even in gem documentation).
33
26
  * [Image attributes](#image-attributes): image attributes are passed through to an HTML <code>img</code> tag.
34
27
 
@@ -66,6 +59,8 @@ This markdown helper enables file inclusion in GitHub markdown.
66
59
 
67
60
  Use the markdown helper to merge external files into a markdown (</code>.md</code>) file.
68
61
 
62
+ See the [use cases](markdown/use_cases/use_cases.md#use-cases).
63
+
69
64
  ### Merged Text Formats
70
65
 
71
66
  #### Highlighted Code Block
@@ -177,6 +172,14 @@ where:
177
172
  ```
178
173
  <!-- <<<<<< END INCLUDED FILE (code_block): SOURCE markdown/readme/include.md -->
179
174
 
175
+ ## Page TOC
176
+
177
+ The markdown helper can create the table of contents for a markdown page.
178
+ - The TOC is a tree of links to the headers on the page, suitable for inclusion with the page itself.
179
+ - See the [use case](markdown/use_cases/tables_of_contents/create_and_include_page_toc/use_case.md#create-and-include-page-toc).
180
+
181
+
182
+
180
183
  ## Image Path Resolution
181
184
 
182
185
  <!-- >>>>>> BEGIN RESOLVED IMAGES: INPUT-LINE '![image_icon](../../images/image.png | width=50)
@@ -349,7 +352,6 @@ where:
349
352
 
350
353
  I have opened some enhancement Issues in the GitHub [markdown_helper](https://github.com/BurdetteLamar/markdown_helper) project:
351
354
 
352
- * [File TOC](https://github.com/BurdetteLamar/markdown_helper/issues/36): table of contents of all headers in a markdown page.
353
355
  * [Project TOC](https://github.com/BurdetteLamar/markdown_helper/issues/37): table of contents of all markdown pages in project.
354
356
  * [Partial file inclusion](https://github.com/BurdetteLamar/markdown_helper/issues/38): including only specified lines from a file (instead of the whole file).
355
357
  * [Ruby-entity inclusion](https://github.com/BurdetteLamar/markdown_helper/issues/39): like file inclusion, but including a Ruby class, module, or method.
data/Rakefile CHANGED
@@ -33,6 +33,7 @@ namespace :build do
33
33
  desc 'Build usage for executables'
34
34
  task :usages do
35
35
  %w/
36
+ create_page_toc
36
37
  include
37
38
  resolve
38
39
  /.each do |executable_name|
@@ -0,0 +1,46 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'optparse'
4
+
5
+ require 'markdown_helper'
6
+
7
+ options = {:pristine => false}
8
+
9
+ # Save opts for use below.
10
+ opts = nil
11
+ parser = OptionParser.new do |_opts|
12
+ opts = _opts
13
+ _opts.banner = "Usage: markdown_helper create_page_toc [options] markdown_file_path toc_file_path"
14
+ _opts.on('--pristine', 'No comments added') do |_|
15
+ options[:pristine] = true
16
+ end
17
+ _opts.on('--help', 'Display help') do
18
+ usage(_opts)
19
+ end
20
+ end
21
+
22
+ def usage(opts)
23
+ puts ''
24
+ puts opts
25
+ puts <<-EOT
26
+
27
+ where
28
+
29
+ * markdown_file_path is the path to an existing markdown file.
30
+ * toc_file_path is the path to the table-of-contents file to be created.
31
+
32
+ Typically:
33
+
34
+ * Both file types are .md.
35
+ * The markdown file contains various headings.
36
+ EOT
37
+ exit
38
+ end
39
+
40
+ parser.parse!
41
+
42
+ _, markdown_file_path, toc_file_path = ARGV
43
+
44
+ usage(opts) unless ARGV.size == 3
45
+
46
+ MarkdownHelper.new(options).create_page_toc(markdown_file_path, toc_file_path)
@@ -0,0 +1,14 @@
1
+
2
+ Usage: markdown_helper create_page_toc [options] markdown_file_path toc_file_path
3
+ --pristine No comments added
4
+ --help Display help
5
+
6
+ where
7
+
8
+ * markdown_file_path is the path to an existing markdown file.
9
+ * toc_file_path is the path to the table-of-contents file to be created.
10
+
11
+ Typically:
12
+
13
+ * Both file types are .md.
14
+ * The markdown file contains various headings.
@@ -9,7 +9,8 @@ class MarkdownHelper
9
9
 
10
10
  class MarkdownHelperError < RuntimeError; end
11
11
  class CircularIncludeError < MarkdownHelperError; end
12
- class MissingIncludeeError < MarkdownHelperError; end
12
+ class UnreadableInputError < MarkdownHelperError; end
13
+ class TocHeadingsError < MarkdownHelperError; end
13
14
  class OptionError < MarkdownHelperError; end
14
15
  class EnvironmentError < MarkdownHelperError; end
15
16
 
@@ -54,6 +55,12 @@ class MarkdownHelper
54
55
  end
55
56
  end
56
57
 
58
+ def create_page_toc(markdown_file_path, toc_file_path)
59
+ send(:generate_file, markdown_file_path, toc_file_path, __method__) do |input_lines, output_lines|
60
+ send(:_create_page_toc, input_lines, output_lines)
61
+ end
62
+ end
63
+
57
64
  # Resolves relative image paths to absolute urls in markdown text.
58
65
  # @param template_file_path [String] the path to the input template markdown file, usually containing image pragmas.
59
66
  # @param markdown_file_path [String] the path to the output resolved markdown file.
@@ -85,6 +92,35 @@ class MarkdownHelper
85
92
 
86
93
  private
87
94
 
95
+ class Heading
96
+
97
+ attr_accessor :level, :title
98
+
99
+ def initialize(level, title)
100
+ self.level = level
101
+ self.title = title
102
+ end
103
+
104
+ def self.parse(line)
105
+ # Four leading spaces not allowed (but three are allowed).
106
+ return nil if line.start_with?(' ' * 4)
107
+ stripped_line = line.sub(/^ */, '')
108
+ # Now must begin with hash marks and space.
109
+ return nil unless stripped_line.match(/^#+ /)
110
+ hash_marks, title = stripped_line.split(' ', 2)
111
+ level = hash_marks.size
112
+ # Seventh level heading not allowed.
113
+ return nil if level > 6
114
+ self.new(level, title)
115
+ end
116
+
117
+ def link
118
+ anchor = title.gsub(/\W+/, '-').downcase
119
+ "[#{title}](##{anchor})"
120
+ end
121
+
122
+ end
123
+
88
124
  def self.comment(text)
89
125
  "<!--#{text}-->\n"
90
126
  end
@@ -100,39 +136,41 @@ class MarkdownHelper
100
136
  end
101
137
 
102
138
  def generate_file(template_file_path, markdown_file_path, method)
139
+ unless File.readable?(template_file_path)
140
+ message = [
141
+ Inclusions::UNREADABLE_INPUT_EXCEPTION_LABEL,
142
+ template_file_path.inspect,
143
+ ].join("\n")
144
+ raise UnreadableInputError.new(message)
145
+ end
103
146
  output_lines = []
104
- begin
105
- File.open(template_file_path, 'r') do |template_file|
106
- template_path_in_project = MarkdownHelper.path_in_project(template_file_path)
107
- output_lines.push(MarkdownHelper.comment(" >>>>>> BEGIN GENERATED FILE (#{method.to_s}): SOURCE #{template_path_in_project} ")) unless pristine
108
- input_lines = template_file.readlines
109
- yield input_lines, output_lines
110
- output_lines.push(MarkdownHelper.comment(" <<<<<< END GENERATED FILE (#{method.to_s}): SOURCE #{template_path_in_project} ")) unless pristine
111
- end
112
- output = output_lines.join('')
113
- rescue => e
114
- unless e.kind_of?(MarkdownHelperError)
115
- message = [
116
- e.message,
117
- Inclusions::UNREADABLE_TEMPLATE_EXCEPTION_LABEL,
118
- ].join("\n")
119
- e = e.exception(message)
120
- end
121
- raise e
147
+ File.open(template_file_path, 'r') do |template_file|
148
+ template_path_in_project = MarkdownHelper.path_in_project(template_file_path)
149
+ output_lines.push(MarkdownHelper.comment(" >>>>>> BEGIN GENERATED FILE (#{method.to_s}): SOURCE #{template_path_in_project} ")) unless pristine
150
+ input_lines = template_file.readlines
151
+ yield input_lines, output_lines
152
+ output_lines.push(MarkdownHelper.comment(" <<<<<< END GENERATED FILE (#{method.to_s}): SOURCE #{template_path_in_project} ")) unless pristine
122
153
  end
123
- begin
124
- File.write(markdown_file_path, output)
125
- rescue => e
126
- unless e.kind_of?(MarkdownHelperError)
127
- message = [
128
- e.message,
129
- Inclusions::UNWRITABLE_OUTPUT_EXCEPTION_LABEL,
130
- ].join("\n")
131
- e = e.exception(message)
154
+ output = output_lines.join('')
155
+ File.write(markdown_file_path, output)
156
+ output
157
+ end
158
+
159
+ def _create_page_toc(input_lines, output_lines)
160
+ level_one_seen = false
161
+ input_lines.each do |input_line|
162
+ input_line.chomp!
163
+ heading = Heading.parse(input_line)
164
+ next unless heading
165
+ unless level_one_seen || heading.level == 1
166
+ message = "First heading must be level 1, not '#{input_line}'"
167
+ raise TocHeadingsError.new(message)
132
168
  end
133
- raise e
169
+ level_one_seen = true
170
+ indentation = ' ' * heading.level
171
+ output_line = "#{indentation}- #{heading.link}"
172
+ output_lines.push("#{output_line}\n")
134
173
  end
135
- output
136
174
  end
137
175
 
138
176
  def include_files(includer_file_path, input_lines, output_lines, inclusions)
@@ -284,11 +322,12 @@ EOT
284
322
  MISSING_INCLUDEE_EXCEPTION_LABEL,
285
323
  backtrace_inclusions,
286
324
  ].join("\n")
287
- e = MissingIncludeeError.new(message)
325
+ e = UnreadableInputError.new(message)
288
326
  e.set_backtrace([])
289
327
  raise e
290
328
  end
291
- unless include_lines.last.match("\n")
329
+ last_line = include_lines.last
330
+ unless last_line && last_line.match("\n")
292
331
  message = "Warning: Included file has no trailing newline: #{cited_includee_file_path}"
293
332
  warn(message)
294
333
  end
@@ -318,8 +357,8 @@ EOT
318
357
  end
319
358
 
320
359
  CIRCULAR_EXCEPTION_LABEL = 'Includes are circular:'
321
- UNREADABLE_TEMPLATE_EXCEPTION_LABEL = 'Could not read template file.'
322
- UNWRITABLE_OUTPUT_EXCEPTION_LABEL = 'Could not write markdown file.'
360
+ UNREADABLE_INPUT_EXCEPTION_LABEL = 'Could not read input file.'
361
+ UNWRITABLE_OUTPUT_EXCEPTION_LABEL = 'Could not write output file.'
323
362
  MISSING_INCLUDEE_EXCEPTION_LABEL = 'Could not read include file,'
324
363
  LEVEL_LABEL = ' Level'
325
364
  BACKTRACE_LABEL = ' Backtrace (innermost include first):'
@@ -349,12 +388,13 @@ EOT
349
388
  lines.join("\n")
350
389
  end
351
390
 
352
- def self.assert_io_exception(test, expected_exception_class, exception_label, e)
391
+ def self.assert_io_exception(test, expected_exception_class, expected_label, expected_file_path, e)
353
392
  test.assert_kind_of(expected_exception_class, e)
354
393
  lines = e.message.split("\n")
355
- _ = lines.shift # Message from original exception.
356
- label_line = lines.shift
357
- test.assert_equal(exception_label, label_line)
394
+ actual_label = lines.shift
395
+ test.assert_equal(expected_label, actual_label)
396
+ actual_file_path = lines.shift
397
+ test.assert_equal(expected_file_path.inspect, actual_file_path)
358
398
  end
359
399
 
360
400
  def self.assert_inclusion_exception(test, expected_exception_class, exception_label, expected_inclusions, e)
@@ -397,20 +437,12 @@ EOT
397
437
  )
398
438
  end
399
439
 
400
- def self.assert_template_exception(test, e)
401
- self.assert_io_exception(
402
- test,
403
- Exception,
404
- UNREADABLE_TEMPLATE_EXCEPTION_LABEL,
405
- e
406
- )
407
- end
408
-
409
- def self.assert_output_exception(test, e)
440
+ def self.assert_template_exception(test, expected_file_path, e)
410
441
  self.assert_io_exception(
411
442
  test,
412
443
  Exception,
413
- UNWRITABLE_OUTPUT_EXCEPTION_LABEL,
444
+ UNREADABLE_INPUT_EXCEPTION_LABEL,
445
+ expected_file_path,
414
446
  e
415
447
  )
416
448
  end
@@ -1,3 +1,3 @@
1
1
  class MarkdownHelper
2
- VERSION = '1.9.0'
2
+ VERSION = '1.9.5'
3
3
  end
@@ -0,0 +1,14 @@
1
+ class StringHelper
2
+
3
+ def self.to_title(s)
4
+ small_words = %w{a an and the or for of nor}
5
+ title_words = []
6
+ s.split('_').each_with_index do |word, i|
7
+ word.capitalize! unless small_words.include?(word)
8
+ word.capitalize! if i == 0
9
+ title_words.push(word)
10
+ end
11
+ title_words.join(' ')
12
+ end
13
+
14
+ end
@@ -4,25 +4,18 @@
4
4
 
5
5
  ## What's New?
6
6
 
7
- * The helper now checks whether the working directory is in a ```git``` project, and raises an exception if not.
8
-
9
- * There is now a backtrace of inclusions when:
10
- * An includee file cannot be read.
11
- * A circular inclusion (direct or indirect) is found.
12
- * The backtrace shows:
13
- * Each includer file path.
14
- * Its include description.
15
- * The includee file path.
16
- * File paths are relative to the git directory.
17
- * Use cases are added:
18
- * [Diagnose Missing Includee](markdown/use_cases/include/diagnose_missing_includee/use_case.md#diagnose-missing-includee).
19
- * [Diagnose Circular Includes](markdown/use_cases/include/diagnose_circular_includes/use_case.md#diagnose-circular-includes).
7
+ Page TOC:
8
+
9
+ - Support is added for creating the table of contents for a markdown page.
10
+ - The TOC is a tree of links to the headers on the page, suitable for inclusion with the page itself.
11
+ - See the [use case](markdown/use_cases/tables_of_contents/create_and_include_page_toc/use_case.md#create-and-include-page-toc).
20
12
 
21
13
  ## What's a Markdown Helper?
22
14
 
23
15
  Class <code>MarkdownHelper</code> supports:
24
16
 
25
17
  * [File inclusion](#file-inclusion): to include text from other files, as code-block or markdown.
18
+ * [Page TOC](#page-toc): to create the table of contents for a markdown page.
26
19
  * [Image path resolution](#image-path-resolution): to resolve relative image paths to absolute URL paths (so they work even in gem documentation).
27
20
  * [Image attributes](#image-attributes): image attributes are passed through to an HTML <code>img</code> tag.
28
21
 
@@ -56,6 +49,8 @@ This markdown helper enables file inclusion in GitHub markdown.
56
49
 
57
50
  Use the markdown helper to merge external files into a markdown (</code>.md</code>) file.
58
51
 
52
+ See the [use cases](markdown/use_cases/use_cases.md#use-cases).
53
+
59
54
  ### Merged Text Formats
60
55
 
61
56
  #### Highlighted Code Block
@@ -106,6 +101,14 @@ where:
106
101
 
107
102
  @[code_block](include.md)
108
103
 
104
+ ## Page TOC
105
+
106
+ The markdown helper can create the table of contents for a markdown page.
107
+ - The TOC is a tree of links to the headers on the page, suitable for inclusion with the page itself.
108
+ - See the [use case](markdown/use_cases/tables_of_contents/create_and_include_page_toc/use_case.md#create-and-include-page-toc).
109
+
110
+
111
+
109
112
  ## Image Path Resolution
110
113
 
111
114
  ![image_icon](../../images/image.png | width=50)
@@ -184,7 +187,6 @@ where:
184
187
 
185
188
  I have opened some enhancement Issues in the GitHub [markdown_helper](https://github.com/BurdetteLamar/markdown_helper) project:
186
189
 
187
- * [File TOC](https://github.com/BurdetteLamar/markdown_helper/issues/36): table of contents of all headers in a markdown page.
188
190
  * [Project TOC](https://github.com/BurdetteLamar/markdown_helper/issues/37): table of contents of all markdown pages in project.
189
191
  * [Partial file inclusion](https://github.com/BurdetteLamar/markdown_helper/issues/38): including only specified lines from a file (instead of the whole file).
190
192
  * [Ruby-entity inclusion](https://github.com/BurdetteLamar/markdown_helper/issues/39): like file inclusion, but including a Ruby class, module, or method.
@@ -1,4 +1,5 @@
1
- require_relative 'include/include_use_case'
1
+ require_relative 'include_files/include_use_case'
2
+ require_relative '../../lib/string_helper'
2
3
 
3
4
  namespace :build do
4
5
 
@@ -19,7 +20,7 @@ EOT
19
20
  Dir.chdir(dir_path) do
20
21
  IncludeUseCase.write_interface_file
21
22
  use_case_dirs = {
22
- :include => %w/
23
+ :include_files => %w/
23
24
  reuse_text
24
25
  include_with_added_comments
25
26
  nest_inclusions
@@ -30,17 +31,21 @@ EOT
30
31
  diagnose_missing_includee
31
32
  diagnose_circular_includes
32
33
  /,
34
+ :tables_of_contents => %w/
35
+ create_and_include_page_toc
36
+ /,
33
37
  # :resolve => %w/
34
38
  # resize_images
35
39
  # resolve_with_added_comments
36
40
  # gemify_images
37
- # /
41
+ # /,
38
42
  }
39
43
  use_case_dirs.each_pair do |section, dir_names|
40
44
 
41
45
  # Header for section, if any dirs therein.
46
+ title = StringHelper.to_title(section.to_s).sub(/ toc$/i, ' TOC')
42
47
  use_case_file.puts(<<EOT
43
- ## #{section.to_s.capitalize}
48
+ ## #{title}
44
49
 
45
50
  EOT
46
51
  ) unless dir_names.empty?
@@ -65,6 +70,7 @@ EOT
65
70
  ensure
66
71
  end
67
72
  else
73
+ p command
68
74
  system(command)
69
75
  end
70
76
  end