Class: MarkdownHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/markdown_helper/version.rb,
lib/markdown_helper/markdown_helper.rb

Direct Known Subclasses

MarkdownIncluder, MarkdownIrbRunner

Defined Under Namespace

Classes: CircularIncludeError, InvalidTocTitleError, MarkdownHelperError, MultiplePageTocError, OptionError, UnreadableIncludeeError, UnreadableTemplateError, UnwritableMarkdownError

Constant Summary collapse

VERSION =
'2.5.4'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ MarkdownHelper

Returns a new instance of MarkdownHelper.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/markdown_helper/markdown_helper.rb', line 27

def initialize(options = {})
  # Confirm that we're in a git project.
  # This is necessary so that we can prune file paths in the tests,
  # which otherwise would fail because of differing installation directories.
  # It also allows pruned paths to be used in the inserted comments (when not pristine).
  MarkdownHelper.git_clone_dir_path
  default_options = {
      :pristine => false,
  }
  merged_options = default_options.merge(options)
  merged_options.each_pair do |method, value|
    unless self.respond_to?(method)
      raise OptionError.new("Unknown option: #{method}")
    end
    setter_method = "#{method}="
    send(setter_method, value)
    merged_options.delete(method)
  end
end

Instance Attribute Details

#pristineObject

Returns the value of attribute pristine.



3
4
5
# File 'lib/markdown_helper/markdown_helper.rb', line 3

def pristine
  @pristine
end

Class Method Details

.comment(text) ⇒ Object



71
72
73
# File 'lib/markdown_helper/markdown_helper.rb', line 71

def self.comment(text)
  "<!--#{text}-->"
end

.git_clone_dir_pathObject



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/markdown_helper/markdown_helper.rb', line 14

def MarkdownHelper.git_clone_dir_path
  git_dir = `git rev-parse --show-toplevel`.chomp
  unless $?.success?
    message = <<EOT

Markdown helper must run inside a .git project.
That is, the working directory one of its parents must be a .git directory.
EOT
    raise RuntimeError.new(message)
  end
  git_dir
end

.path_in_project(file_path) ⇒ Object



75
76
77
78
# File 'lib/markdown_helper/markdown_helper.rb', line 75

def self.path_in_project(file_path)
  abs_path = File.absolute_path(file_path)
  abs_path.sub(MarkdownHelper.git_clone_dir_path + '/', '')
end

Instance Method Details

#generate_file(method, template_file_path, markdown_file_path) {|output_lines| ... } ⇒ Object

Yields:

  • (output_lines)


47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/markdown_helper/markdown_helper.rb', line 47

def generate_file(method, template_file_path, markdown_file_path)
  template_path_in_project = MarkdownHelper.path_in_project(template_file_path)
  output_lines = []
  yield output_lines
  output_lines = output_lines.collect { |line| line.chomp }
  unless pristine
    output_lines.unshift(MarkdownHelper.comment(" >>>>>> BEGIN GENERATED FILE (#{method}): SOURCE #{template_path_in_project} "))
    output_lines.push(MarkdownHelper.comment(" <<<<<< END GENERATED FILE (#{method}): SOURCE #{template_path_in_project} "))
  end
  output_lines.push('')
  output = output_lines.join("\n")
  File.write(markdown_file_path, output)
end

#include(template_file_path, markdown_file_path) ⇒ Object



66
67
68
69
# File 'lib/markdown_helper/markdown_helper.rb', line 66

def include(template_file_path, markdown_file_path)
  includer = MarkdownIncluder.new(:pristine => pristine)
  includer.include(template_file_path, markdown_file_path)
end

#run_irb(template_file_path, markdown_file_path) ⇒ Object



61
62
63
64
# File 'lib/markdown_helper/markdown_helper.rb', line 61

def run_irb(template_file_path, markdown_file_path)
  irb_runner = MarkdownIrbRunner.new(:pristine => pristine)
  irb_runner.run_irb(template_file_path, markdown_file_path)
end