Class: MarkdownIncluder

Inherits:
MarkdownHelper show all
Defined in:
lib/markdown_helper/markdown_includer.rb

Defined Under Namespace

Classes: Heading, Inclusion

Constant Summary collapse

INCLUDE_REGEXP =
/^@\[([^\[]+)\]\(([^)]+)\)$/
INCLUDE_MARKDOWN_REGEXP =
/^@\[:markdown\]\(([^)]+)\)$/

Constants inherited from MarkdownHelper

MarkdownHelper::VERSION

Instance Attribute Summary

Attributes inherited from MarkdownHelper

#pristine

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from MarkdownHelper

comment, #generate_file, git_clone_dir_path, #initialize, path_in_project, #run_irb

Constructor Details

This class inherits a constructor from MarkdownHelper

Class Method Details

.backtrace_inclusions(inclusions) ⇒ Object



277
278
279
280
281
282
283
284
285
# File 'lib/markdown_helper/markdown_includer.rb', line 277

def self.backtrace_inclusions(inclusions)
  lines = ['  Backtrace (innermost include first):']
  inclusions.reverse.each_with_index do |inclusion, i|
    lines.push("#{'    Level'} #{i}:")
    level_lines = inclusion.to_lines(indentation_level = 3)
    lines.push(*level_lines)
  end
  lines.join("\n")
end

.details(text) ⇒ Object



23
24
25
# File 'lib/markdown_helper/markdown_includer.rb', line 23

def MarkdownIncluder.details(text)
  "<details>\n#{text}</details>"
end

.pre(text) ⇒ Object



19
20
21
# File 'lib/markdown_helper/markdown_includer.rb', line 19

def MarkdownIncluder.pre(text)
  "<pre>\n#{text}</pre>"
end

Instance Method Details

#add_inclusion_comments(treatment, includee_file_path, lines) ⇒ Object



193
194
195
196
197
198
199
200
201
202
# File 'lib/markdown_helper/markdown_includer.rb', line 193

def add_inclusion_comments(treatment, includee_file_path, lines)
  unless pristine
    path_in_project = MarkdownHelper.path_in_project(includee_file_path)
    treatment = treatment.sub(/^:/, '')
    comment = format(' >>>>>> BEGIN INCLUDED FILE (%s): SOURCE %s ', treatment, path_in_project)
    lines.unshift(MarkdownHelper.comment(comment))
    comment = format(' <<<<<< END INCLUDED FILE (%s): SOURCE %s ', treatment, path_in_project)
    lines.push(MarkdownHelper.comment(comment))
  end
end

#check_circularity(inclusion) ⇒ Object



248
249
250
251
252
253
254
255
256
257
258
259
260
261
# File 'lib/markdown_helper/markdown_includer.rb', line 248

def check_circularity(inclusion)
  included_file_paths = @inclusions.collect { |x| x.includee_real_file_path}
  previously_included = included_file_paths.include?(inclusion.includee_real_file_path)
  if previously_included
    @inclusions.push(inclusion)
    message = [
        'Includes are circular:',
        MarkdownIncluder.backtrace_inclusions(@inclusions),
    ].join("\n")
    e = CircularIncludeError.new(message)
    e.set_backtrace([])
    raise e
  end
end

#check_includee(inclusion) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'lib/markdown_helper/markdown_includer.rb', line 263

def check_includee(inclusion)
  unless File.readable?(inclusion.includee_absolute_file_path)
    @inclusions.push(inclusion)
    message = [
        'Could not read includee file:',
        MarkdownIncluder.backtrace_inclusions(@inclusions),
    ].join("\n")
    e = UnreadableIncludeeError.new(message)
    e.set_backtrace([])
    raise e
  end

end

#check_template(template_file_path) ⇒ Object



180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/markdown_helper/markdown_includer.rb', line 180

def check_template(template_file_path)
  unless File.readable?(template_file_path)
    path_in_project = MarkdownHelper.path_in_project(template_file_path)
    message = [
      "Could not read template file: #{path_in_project}",
      MarkdownIncluder.backtrace_inclusions(@inclusions),
    ].join("\n")
    e = UnreadableTemplateError.new(message)
    e.set_backtrace([])
    raise e
  end
end

#gather_markdown(template_file_path) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/markdown_helper/markdown_includer.rb', line 27

def gather_markdown(template_file_path)
  template_dir_path = File.dirname(template_file_path)
  template_file_name = File.basename(template_file_path)
  Dir.chdir(template_dir_path) do
    check_template(template_file_name)
    markdown_lines = []
    template_lines = File.readlines(template_file_path)
    template_lines.each_with_index do |template_line, i|
      template_line.chomp!
      treatment, includee_file_path = *parse_include(template_line)
      unless treatment == ':markdown'
        markdown_lines.push(template_line)
        next
      end
      inclusion = Inclusion.new(
          includer_file_path: template_file_path,
          include_pragma: template_line,
          includer_line_number: i,
          treatment: treatment,
          cited_includee_file_path: includee_file_path,
          inclusions: @inclusions
      )
      check_includee(inclusion)
      check_circularity(inclusion)
      @inclusions.push(inclusion)
      includee_lines = gather_markdown(File.absolute_path(includee_file_path))
      markdown_lines.concat(includee_lines)
      @inclusions.pop
      add_inclusion_comments(treatment, includee_file_path, markdown_lines)
    end
    markdown_lines
  end
end

#get_page_toc_lines(template_file_path) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/markdown_helper/markdown_includer.rb', line 61

def get_page_toc_lines(template_file_path)
  markdown_lines = gather_markdown(template_file_path)
  toc_line_index = nil
  toc_title = nil
  anchor_counts = Hash.new(0)
  markdown_lines.each_with_index do |markdown_line, i|
    match_data = markdown_line.match(INCLUDE_REGEXP)
    next unless match_data
    treatment = match_data[1]
    next unless treatment == ':page_toc'
    unless toc_line_index.nil?
      message = 'Multiple page TOC not allowed'
      raise MultiplePageTocError.new(message)
    end
    toc_line_index = i
    toc_title = match_data[2]
    title_regexp = /^\#{1,6}\s/
    unless toc_title.match(title_regexp)
      message = "TOC title must be a valid markdown header, not #{toc_title}"
      raise InvalidTocTitleError.new(message)
    end
  end
  return markdown_lines unless toc_line_index
  toc_lines = [toc_title]
  first_heading_level = nil
  markdown_lines.each_with_index do |input_line, i|
    line = input_line.chomp
    heading = Heading.parse(line)
    next unless heading
    if i < toc_line_index
      heading.link(anchor_counts)
      next
    end
    first_heading_level ||= heading.level
    indentation = '  ' * (heading.level - first_heading_level)
    toc_line = "#{indentation}- #{heading.link(anchor_counts)}"
    toc_lines.push(toc_line)
  end
  toc_lines
end

#include(template_file_path, markdown_file_path) ⇒ Object



8
9
10
11
12
13
14
15
16
17
# File 'lib/markdown_helper/markdown_includer.rb', line 8

def include(template_file_path, markdown_file_path)
  @inclusions = []
  generate_file(:include, template_file_path, markdown_file_path) do |output_lines|
    Dir.chdir(File.dirname(template_file_path)) do
      page_toc_lines = get_page_toc_lines(template_file_path)
      include_all(template_file_path, page_toc_lines, is_nested = false, output_lines)
    end
  end

end

#include_all(includer_file_path, page_toc_lines, is_nested, output_lines) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/markdown_helper/markdown_includer.rb', line 102

def include_all(includer_file_path, page_toc_lines, is_nested, output_lines)
  includer_dir_path = File.dirname(includer_file_path)
  includer_file_name = File.basename(includer_file_path)
  absolute_includer_file_path = File.absolute_path(includer_file_path)
  Dir.chdir(includer_dir_path) do
    check_template(includer_file_name)
    template_lines = File.readlines(includer_file_name)
    if is_nested
      add_inclusion_comments(':markdown', includer_file_path, template_lines)
    end
    template_lines.each_with_index do |template_line, i|
      treatment, includee_file_path = *parse_include(template_line)
      if treatment.nil?
        output_lines.push(template_line)
        next
      end
      if treatment == ':page_toc'
        output_lines.concat(page_toc_lines)
        next
      end
      inclusion = Inclusion.new(
          includer_file_path: absolute_includer_file_path,
          include_pragma: template_line,
          includer_line_number: i,
          treatment: treatment,
          cited_includee_file_path: includee_file_path,
          inclusions: @inclusions
      )
      @inclusions.push(inclusion)
      check_includee(inclusion)
      case treatment
      when ':comment'
        include_comment(includee_file_path, treatment, inclusion, output_lines)
      when ':pre'
        include_pre(includee_file_path, treatment, inclusion, output_lines)
      when ':details'
        include_details(includee_file_path, treatment, inclusion, output_lines)
      when ':markdown'
        include_all(includee_file_path, page_toc_lines, is_nested = true, output_lines)
      else
        include_file(includee_file_path, treatment, inclusion, output_lines)
      end
      @inclusions.pop
    end
  end
end

#include_comment(includee_file_path, treatment, inclusion, output_lines) ⇒ Object



149
150
151
152
153
# File 'lib/markdown_helper/markdown_includer.rb', line 149

def include_comment(includee_file_path, treatment, inclusion, output_lines)
  text = File.read(includee_file_path)
  output_lines.push(MarkdownHelper.comment(text))
  add_inclusion_comments(treatment, includee_file_path, output_lines)
end

#include_details(includee_file_path, treatment, inclusion, output_lines) ⇒ Object



161
162
163
164
165
# File 'lib/markdown_helper/markdown_includer.rb', line 161

def include_details(includee_file_path, treatment, inclusion, output_lines)
  text = File.read(includee_file_path)
  output_lines.push(MarkdownIncluder.details(text))
  add_inclusion_comments(treatment, includee_file_path, output_lines)
end

#include_file(includee_file_path, treatment, inclusion, output_lines) ⇒ Object



167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/markdown_helper/markdown_includer.rb', line 167

def include_file(includee_file_path, treatment, inclusion, output_lines)
  file_marker = format('```%s```:', File.basename(includee_file_path))
  begin_backticks = '```'
  end_backticks = '```'
  begin_backticks += treatment unless treatment.start_with?(':')
  includee_lines = File.read(includee_file_path).split("\n")
  includee_lines.unshift(begin_backticks)
  includee_lines.unshift(file_marker)
  includee_lines.push(end_backticks)
  add_inclusion_comments(treatment, includee_file_path, includee_lines)
  output_lines.concat(includee_lines)
end

#include_pre(includee_file_path, treatment, inclusion, output_lines) ⇒ Object



155
156
157
158
159
# File 'lib/markdown_helper/markdown_includer.rb', line 155

def include_pre(includee_file_path, treatment, inclusion, output_lines)
  text = File.read(includee_file_path)
  output_lines.push(MarkdownIncluder.pre(text))
  add_inclusion_comments(treatment, includee_file_path, output_lines)
end

#parse_include(line) ⇒ Object



204
205
206
207
208
209
210
# File 'lib/markdown_helper/markdown_includer.rb', line 204

def parse_include(line)
  match_data = line.match(INCLUDE_REGEXP)
  return [nil, nil] unless match_data
  treatment = match_data[1]
  includee_file_path = match_data[2]
  [treatment, includee_file_path]
end