markdown_helper 1.8.0 → 1.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (97) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +5 -5
  3. data/CODE_OF_CONDUCT.md +74 -74
  4. data/Gemfile.lock +2 -1
  5. data/LICENSE.txt +21 -21
  6. data/README.md +60 -47
  7. data/Rakefile +16 -13
  8. data/bin/_include +0 -1
  9. data/bin/_resolve +0 -1
  10. data/bin/markdown_helper +3 -0
  11. data/bin/setup +8 -8
  12. data/lib/markdown_helper.rb +329 -97
  13. data/lib/markdown_helper/version.rb +1 -1
  14. data/markdown/{README.template.md → readme/README.template.md} +25 -12
  15. data/markdown/{code_block_ruby_template.md → readme/code_block_ruby_template.md} +0 -0
  16. data/markdown/{highlight_ruby_template.md → readme/highlight_ruby_template.md} +0 -0
  17. data/markdown/readme/highlighted_ruby.md +12 -0
  18. data/markdown/{include.md → readme/include.md} +0 -0
  19. data/markdown/{include.rb → readme/include.rb} +0 -0
  20. data/markdown/{include_usage.rb → readme/include_usage.rb} +0 -0
  21. data/markdown/{resolve.md → readme/resolve.md} +0 -0
  22. data/markdown/{resolve_usage.rb → readme/resolve_usage.rb} +0 -0
  23. data/markdown/{verbatim_ruby_template.md → readme/verbatim_ruby_template.md} +0 -0
  24. data/markdown/use_cases/Rakefile +53 -23
  25. data/markdown/use_cases/include/diagnose_circular_includes/diagnose_circular_includes.err +26 -0
  26. data/markdown/use_cases/include/diagnose_circular_includes/diagnose_circular_includes.rb +65 -0
  27. data/markdown/use_cases/include/diagnose_circular_includes/include.rb +5 -0
  28. data/markdown/use_cases/include/diagnose_circular_includes/includer.md +1 -0
  29. data/markdown/use_cases/include/diagnose_circular_includes/includer_0.md +1 -0
  30. data/markdown/use_cases/include/diagnose_circular_includes/includer_1.md +1 -0
  31. data/markdown/use_cases/include/diagnose_circular_includes/includer_2.md +1 -0
  32. data/markdown/use_cases/include/diagnose_circular_includes/use_case.md +98 -0
  33. data/markdown/use_cases/include/diagnose_circular_includes/use_case_template.md +27 -0
  34. data/markdown/use_cases/include/diagnose_missing_includee/diagnose_missing_includee.err +26 -0
  35. data/markdown/use_cases/include/diagnose_missing_includee/diagnose_missing_includee.rb +67 -0
  36. data/markdown/use_cases/include/diagnose_missing_includee/include.rb +5 -0
  37. data/markdown/use_cases/include/diagnose_missing_includee/included.md +1 -0
  38. data/markdown/use_cases/include/diagnose_missing_includee/includer.md +1 -0
  39. data/markdown/use_cases/include/diagnose_missing_includee/includer_0.md +1 -0
  40. data/markdown/use_cases/include/diagnose_missing_includee/includer_1.md +1 -0
  41. data/markdown/use_cases/include/diagnose_missing_includee/includer_2.md +1 -0
  42. data/markdown/use_cases/include/diagnose_missing_includee/use_case.md +100 -0
  43. data/markdown/use_cases/include/diagnose_missing_includee/use_case_template.md +29 -0
  44. data/markdown/use_cases/include/include.rb +5 -0
  45. data/markdown/use_cases/include/include_code_block/hello.rb +8 -0
  46. data/markdown/use_cases/include/include_code_block/include_code_block.rb +79 -0
  47. data/markdown/use_cases/include/include_code_block/included.md +14 -0
  48. data/markdown/use_cases/include/include_code_block/includer.md +4 -0
  49. data/markdown/use_cases/include/include_code_block/use_case.md +108 -0
  50. data/markdown/use_cases/include/include_code_block/use_case_template.md +33 -0
  51. data/markdown/use_cases/include/include_generated_text/include_generated_text.rb +38 -0
  52. data/markdown/use_cases/include/include_generated_text/use_case.md +18 -0
  53. data/markdown/use_cases/include/include_generated_text/use_case_template.md +18 -0
  54. data/markdown/use_cases/include/include_highlighted_code/hello.rb +8 -0
  55. data/markdown/use_cases/include/include_highlighted_code/include_highlighted_code.rb +83 -0
  56. data/markdown/use_cases/include/include_highlighted_code/included.md +14 -0
  57. data/markdown/use_cases/include/include_highlighted_code/includer.md +4 -0
  58. data/markdown/use_cases/include/include_highlighted_code/use_case.md +110 -0
  59. data/markdown/use_cases/include/include_highlighted_code/use_case_template.md +35 -0
  60. data/markdown/use_cases/include/include_markdown/include_markdown.rb +81 -0
  61. data/markdown/use_cases/include/include_markdown/included.md +13 -0
  62. data/markdown/use_cases/include/include_markdown/includer.md +4 -0
  63. data/markdown/use_cases/include/include_markdown/markdown.md +10 -0
  64. data/markdown/use_cases/include/include_markdown/use_case.md +106 -0
  65. data/markdown/use_cases/include/include_markdown/use_case_template.md +33 -0
  66. data/markdown/use_cases/include/include_use_case.rb +110 -0
  67. data/markdown/use_cases/include/include_with_added_comments/include_with_added_comments.rb +43 -60
  68. data/markdown/use_cases/include/include_with_added_comments/included.md +2 -2
  69. data/markdown/use_cases/include/include_with_added_comments/use_case.md +56 -0
  70. data/markdown/use_cases/include/include_with_added_comments/{template.md → use_case_template.md} +20 -1
  71. data/markdown/use_cases/include/interface.md +25 -0
  72. data/markdown/use_cases/include/nest_inclusions/included.md +5 -0
  73. data/markdown/use_cases/include/nest_inclusions/includee.md +3 -0
  74. data/markdown/use_cases/include/nest_inclusions/includer.md +3 -0
  75. data/markdown/use_cases/include/nest_inclusions/nest_inclusions.rb +61 -0
  76. data/markdown/use_cases/include/nest_inclusions/nested_includee.md +1 -0
  77. data/markdown/use_cases/include/nest_inclusions/use_case.md +74 -0
  78. data/markdown/use_cases/include/nest_inclusions/use_case_template.md +23 -0
  79. data/markdown/use_cases/include/reuse_text/include.rb +5 -0
  80. data/markdown/use_cases/include/reuse_text/included.md +2 -5
  81. data/markdown/use_cases/include/reuse_text/includee.md +1 -0
  82. data/markdown/use_cases/include/reuse_text/includer.md +2 -5
  83. data/markdown/use_cases/include/reuse_text/reuse_text.rb +24 -77
  84. data/markdown/use_cases/include/reuse_text/use_case.md +69 -0
  85. data/markdown/use_cases/include/reuse_text/use_case_template.md +23 -0
  86. data/markdown/use_cases/resolve/gemify_images/gemify_images.rb +58 -29
  87. data/markdown/use_cases/resolve/gemify_images/template.md +4 -0
  88. data/markdown/use_cases/use_case.rb +45 -0
  89. data/markdown/use_cases/use_cases.md +9 -2
  90. data/markdown_helper.gemspec +1 -1
  91. metadata +70 -19
  92. data/markdown/highlighted_ruby.md +0 -12
  93. data/markdown/use_cases/include/include_generated_text/build.rb +0 -46
  94. data/markdown/use_cases/include/include_with_added_comments/include_with_added_comments.md +0 -37
  95. data/markdown/use_cases/include/reuse_text/reusable_text.md +0 -1
  96. data/markdown/use_cases/include/reuse_text/reuse_text.md +0 -54
  97. data/markdown/use_cases/include/reuse_text/template.md +0 -33
@@ -1,3 +1,3 @@
1
1
  class MarkdownHelper
2
- VERSION = '1.8.0'
2
+ VERSION = '1.9.0'
3
3
  end
@@ -4,12 +4,21 @@
4
4
 
5
5
  ## What's New?
6
6
 
7
- Command-line interface is now supported:
8
-
9
- * Added first [use cases](markdown/use_cases/use_cases.md#use-cases) (there will be more).
10
- * Deprecated treatment ```:verbatim```, changing to ```:markdown```. The older term could be confusing, because although text to be treated ```:verbatim``` is included 'verbatim' (without change), it will be processed as GitHub markdown.
11
-
12
- ## What's This?
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).
20
+
21
+ ## What's a Markdown Helper?
13
22
 
14
23
  Class <code>MarkdownHelper</code> supports:
15
24
 
@@ -23,6 +32,10 @@ The markdown helper is a preprocessor that reads a markdown document (template)
23
32
 
24
33
  The template can contain certain instructions that call for file inclusions and image resolutions.
25
34
 
35
+ ### Restriction: ```git``` Only
36
+
37
+ The helper works only in a ```git``` project: the working directory or one of ita parents must be a git directory -- one in which command ```git rev-parse --git-dir``` succeeds.
38
+
26
39
  ### Commented or Pristine?
27
40
 
28
41
  By default, the output markdown has added comments that show:
@@ -35,7 +48,7 @@ You can suppress those comments using the <code>pristine</code> option.
35
48
 
36
49
  ## File Inclusion
37
50
 
38
- ![include_icon](../images/include.png | width=50)
51
+ ![include_icon](../../images/include.png | width=50)
39
52
 
40
53
  This markdown helper enables file inclusion in GitHub markdown.
41
54
 
@@ -69,7 +82,7 @@ The markdown text is itself scanned for nested includes.
69
82
 
70
83
  #### CLI
71
84
 
72
- @[:code_block](../bin/usage/include.txt)
85
+ @[:code_block](../../bin/usage/include.txt)
73
86
 
74
87
  #### API
75
88
 
@@ -95,7 +108,7 @@ where:
95
108
 
96
109
  ## Image Path Resolution
97
110
 
98
- ![image_icon](../images/image.png | width=50)
111
+ ![image_icon](../../images/image.png | width=50)
99
112
 
100
113
  This markdown helper enables image path resolution in GitHub markdown.
101
114
 
@@ -109,7 +122,7 @@ This matters because when markdown becomes part of a Ruby gem, its images will h
109
122
 
110
123
  #### CLI
111
124
 
112
- @[:code_block](../bin/usage/resolve.txt)
125
+ @[:code_block](../../bin/usage/resolve.txt)
113
126
 
114
127
  #### API
115
128
 
@@ -133,7 +146,7 @@ where:
133
146
 
134
147
  ## Image Attributes
135
148
 
136
- ![html_icon](../images/html.png | width=50)
149
+ ![html_icon](../../images/html.png | width=50)
137
150
 
138
151
  This markdown helper enables HTML image attributes in GitHub markdown [image descriptions](https://github.github.com/gfm/#image-description).
139
152
 
@@ -145,7 +158,7 @@ Use the markdown helper to add image attributes in a markdown (</code>.md</code>
145
158
 
146
159
  #### CLI
147
160
 
148
- @[:code_block](../bin/usage/resolve.txt)
161
+ @[:code_block](../../bin/usage/resolve.txt)
149
162
 
150
163
  #### API
151
164
 
@@ -0,0 +1,12 @@
1
+ <!-- >>>>>> BEGIN GENERATED FILE (include): SOURCE highlight_ruby_template.md -->
2
+ <!-- >>>>>> BEGIN INCLUDED FILE (ruby): SOURCE markdown/readme/include.rb -->
3
+ ```include.rb```:
4
+ ```ruby
5
+ class RubyCode
6
+ def initialize
7
+ raise RuntimeError.new('I am only an example!')
8
+ end
9
+ end
10
+ ```
11
+ <!-- <<<<<< END INCLUDED FILE (ruby): SOURCE markdown/readme/include.rb -->
12
+ <!-- <<<<<< END GENERATED FILE (include): SOURCE highlight_ruby_template.md -->
@@ -1,8 +1,14 @@
1
+ require_relative 'include/include_use_case'
1
2
 
2
3
  namespace :build do
3
4
 
5
+ def camelize(snake_case_string)
6
+ snake_case_string.split('_').collect(&:capitalize).join
7
+ end
8
+
4
9
  desc 'Build use case markdown'
5
10
  task :use_cases do
11
+ # Page that links to use cases.
6
12
  File.open('use_cases.md', 'w') do |use_case_file|
7
13
  use_case_file.puts(<<EOT
8
14
  # Use Cases
@@ -10,46 +16,70 @@ namespace :build do
10
16
  EOT
11
17
  )
12
18
  dir_path = File.dirname(__FILE__)
13
- use_case_file_paths = []
14
- tbs_file_paths = []
15
19
  Dir.chdir(dir_path) do
20
+ IncludeUseCase.write_interface_file
16
21
  use_case_dirs = {
17
22
  :include => %w/
18
23
  reuse_text
19
24
  include_with_added_comments
25
+ nest_inclusions
26
+ include_markdown
27
+ include_code_block
28
+ include_highlighted_code
29
+ include_generated_text
30
+ diagnose_missing_includee
31
+ diagnose_circular_includes
20
32
  /,
21
- :resolve => %w/
22
- /
33
+ # :resolve => %w/
34
+ # resize_images
35
+ # resolve_with_added_comments
36
+ # gemify_images
37
+ # /
23
38
  }
24
39
  use_case_dirs.each_pair do |section, dir_names|
40
+
41
+ # Header for section, if any dirs therein.
25
42
  use_case_file.puts(<<EOT
26
43
  ## #{section.to_s.capitalize}
27
44
 
28
45
  EOT
29
46
  ) unless dir_names.empty?
47
+
48
+ backtrace_cases = %w/
49
+ diagnose_missing_includee
50
+ diagnose_circular_includes
51
+ /
30
52
  dir_names.each do |dir_name|
31
- ruby_file_path = File.join(
32
- dir_path,
33
- section.to_s,
34
- dir_name,
35
- "#{dir_name}.rb"
36
- )
37
- if File.exist?(ruby_file_path)
38
- command = "ruby #{ruby_file_path}"
39
- system(command)
40
- end
41
- use_case_file_path = File.join(
42
- dir_path,
43
- section.to_s,
44
- dir_name,
45
- "#{dir_name}.md"
46
- )
47
- if File.exist?(use_case_file_path)
53
+ Dir.chdir("#{section}/#{dir_name}") do
54
+ # If the dir has a Ruby file, run it.
55
+ ruby_file_path = "#{dir_name}.rb"
56
+ if File.exist?(ruby_file_path)
57
+ class_name = camelize(dir_name)
58
+ command = "ruby -I . -r #{dir_name} -e #{class_name}.build"
59
+ if backtrace_cases.include?(dir_name)
60
+ command += " 2> #{dir_name}.err"
61
+ begin
62
+ system(command)
63
+ rescue
64
+ #
65
+ ensure
66
+ end
67
+ else
68
+ system(command)
69
+ end
70
+ end
71
+
72
+ # If the dir has a use case file, link to it.
73
+ use_case_file_path = 'use_case.md'
74
+ unless File.exist?(use_case_file_path)
75
+ message = "File #{dir_name}/use_case.md does not exist"
76
+ warn(message)
77
+ next
78
+ end
48
79
  title_line = File.open(use_case_file_path).grep(/^#/).first.chomp
49
80
  title = title_line.split(/\s/, 2).pop
50
81
  use_case_file_name = File.basename(use_case_file_path)
51
- use_case_name = File.basename(use_case_file_path, '.md')
52
- use_case_anchor = use_case_name.gsub('_', '-')
82
+ use_case_anchor = dir_name.gsub('_', '-')
53
83
  use_case_relative_url = File.join(
54
84
  section.to_s,
55
85
  dir_name,
@@ -0,0 +1,26 @@
1
+ include.rb: Includes are circular: (MarkdownHelper::CircularIncludeError)
2
+ Backtrace (innermost include first):
3
+ Level 0:
4
+ Includer:
5
+ Location: markdown/use_cases/include/diagnose_circular_includes/includer_2.md:1
6
+ Include description: @[:markdown](includer_0.md)
7
+ Includee:
8
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_0.md
9
+ Level 1:
10
+ Includer:
11
+ Location: markdown/use_cases/include/diagnose_circular_includes/includer_1.md:1
12
+ Include description: @[:markdown](includer_2.md)
13
+ Includee:
14
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_2.md
15
+ Level 2:
16
+ Includer:
17
+ Location: markdown/use_cases/include/diagnose_circular_includes/includer_0.md:1
18
+ Include description: @[:markdown](includer_1.md)
19
+ Includee:
20
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_1.md
21
+ Level 3:
22
+ Includer:
23
+ Location: includer.md:1
24
+ Include description: @[:markdown](includer_0.md)
25
+ Includee:
26
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_0.md
@@ -0,0 +1,65 @@
1
+ require_relative '../include_use_case'
2
+
3
+ class DiagnoseCircularIncludes < IncludeUseCase
4
+
5
+ def self.build
6
+
7
+ use_case_name = File.basename(__FILE__, '.rb')
8
+ use_case = self.new(use_case_name)
9
+
10
+ use_case.write_ruby_file(pristine = true)
11
+
12
+ [
13
+ [0, 1],
14
+ [1, 2],
15
+ [2, 0],
16
+ ].each do |indexes|
17
+ includer_index, includee_index = *indexes
18
+ includer_file_name = "includer_#{includer_index}.md"
19
+ includee_file_name = "includer_#{includee_index}.md"
20
+ if includer_index == 0
21
+ include_description = "@[:markdown](#{includer_file_name})\n"
22
+ File.write(INCLUDER_FILE_NAME, include_description)
23
+ end
24
+ include_description = "@[:markdown](#{includee_file_name})\n"
25
+ File.write(includer_file_name, include_description)
26
+ end
27
+
28
+ File.write(
29
+ TEMPLATE_FILE_NAME,
30
+ <<EOT
31
+ ### Diagnose Circular Includes
32
+
33
+ Use the backtrace of inclusions to diagnose and correct circular inclusions: that is inclusions that directly or indirectly cause a file to include itself.
34
+
35
+ #### Files To Be Included
36
+
37
+ These files demonstrate nested inclusion, with circular inclusions.
38
+
39
+ @[markdown](includer_0.md)
40
+
41
+ @[markdown](includer_1.md)
42
+
43
+ @[markdown](includer_2.md)
44
+
45
+ #### Includer File
46
+
47
+ This file initiates the nested inclusions.
48
+
49
+ @[markdown](#{INCLUDER_FILE_NAME})
50
+
51
+ @[:markdown](../interface.md)
52
+
53
+ #### Error and Backtrace
54
+
55
+ Here's the resulting backtrace of inclusions.
56
+
57
+ @[:code_block](diagnose_circular_includes.err)
58
+ EOT
59
+ )
60
+
61
+ use_case.build
62
+
63
+ end
64
+
65
+ end
@@ -0,0 +1,5 @@
1
+ require 'markdown_helper'
2
+
3
+ # Option :pristine suppresses comment insertion.
4
+ markdown_helper = MarkdownHelper.new(:pristine => true)
5
+ markdown_helper.include('includer.md', 'included.md')
@@ -0,0 +1 @@
1
+ @[:markdown](includer_0.md)
@@ -0,0 +1 @@
1
+ @[:markdown](includer_1.md)
@@ -0,0 +1 @@
1
+ @[:markdown](includer_2.md)
@@ -0,0 +1 @@
1
+ @[:markdown](includer_0.md)
@@ -0,0 +1,98 @@
1
+ ### Diagnose Circular Includes
2
+
3
+ Use the backtrace of inclusions to diagnose and correct circular inclusions: that is inclusions that directly or indirectly cause a file to include itself.
4
+
5
+ #### Files To Be Included
6
+
7
+ These files demonstrate nested inclusion, with circular inclusions.
8
+
9
+ ```includer_0.md```:
10
+ ```markdown
11
+ @[:markdown](includer_1.md)
12
+ ```
13
+
14
+ ```includer_1.md```:
15
+ ```markdown
16
+ @[:markdown](includer_2.md)
17
+ ```
18
+
19
+ ```includer_2.md```:
20
+ ```markdown
21
+ @[:markdown](includer_0.md)
22
+ ```
23
+
24
+ #### Includer File
25
+
26
+ This file initiates the nested inclusions.
27
+
28
+ ```includer.md```:
29
+ ```markdown
30
+ @[:markdown](includer_0.md)
31
+ ```
32
+
33
+ #### CLI
34
+
35
+ You can use the command-line interface to perform the inclusion.
36
+
37
+ ##### Command
38
+
39
+ ```sh
40
+ markdown_helper include --pristine includer.md included.md
41
+ ```
42
+
43
+ (Option ```--pristine``` suppresses comment insertion.)
44
+
45
+ #### API
46
+
47
+ You can use the API to perform the inclusion.
48
+
49
+ ##### Ruby Code
50
+
51
+ ```include.rb```:
52
+ ```ruby
53
+ require 'markdown_helper'
54
+
55
+ # Option :pristine suppresses comment insertion.
56
+ markdown_helper = MarkdownHelper.new(:pristine => true)
57
+ markdown_helper.include('includer.md', 'included.md')
58
+ ```
59
+
60
+ ##### Command
61
+
62
+ ```sh
63
+ ruby include.rb
64
+ ```
65
+
66
+ #### Error and Backtrace
67
+
68
+ Here's the resulting backtrace of inclusions.
69
+
70
+ ```diagnose_circular_includes.err```:
71
+ ```
72
+ include.rb: Includes are circular: (MarkdownHelper::CircularIncludeError)
73
+ Backtrace (innermost include first):
74
+ Level 0:
75
+ Includer:
76
+ Location: markdown/use_cases/include/diagnose_circular_includes/includer_2.md:1
77
+ Include description: @[:markdown](includer_0.md)
78
+ Includee:
79
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_0.md
80
+ Level 1:
81
+ Includer:
82
+ Location: markdown/use_cases/include/diagnose_circular_includes/includer_1.md:1
83
+ Include description: @[:markdown](includer_2.md)
84
+ Includee:
85
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_2.md
86
+ Level 2:
87
+ Includer:
88
+ Location: markdown/use_cases/include/diagnose_circular_includes/includer_0.md:1
89
+ Include description: @[:markdown](includer_1.md)
90
+ Includee:
91
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_1.md
92
+ Level 3:
93
+ Includer:
94
+ Location: includer.md:1
95
+ Include description: @[:markdown](includer_0.md)
96
+ Includee:
97
+ File path: markdown/use_cases/include/diagnose_circular_includes/includer_0.md
98
+ ```
@@ -0,0 +1,27 @@
1
+ ### Diagnose Circular Includes
2
+
3
+ Use the backtrace of inclusions to diagnose and correct circular inclusions: that is inclusions that directly or indirectly cause a file to include itself.
4
+
5
+ #### Files To Be Included
6
+
7
+ These files demonstrate nested inclusion, with circular inclusions.
8
+
9
+ @[markdown](includer_0.md)
10
+
11
+ @[markdown](includer_1.md)
12
+
13
+ @[markdown](includer_2.md)
14
+
15
+ #### Includer File
16
+
17
+ This file initiates the nested inclusions.
18
+
19
+ @[markdown](includer.md)
20
+
21
+ @[:markdown](../interface.md)
22
+
23
+ #### Error and Backtrace
24
+
25
+ Here's the resulting backtrace of inclusions.
26
+
27
+ @[:code_block](diagnose_circular_includes.err)