Class: Starter::Markdown

Inherits:
Object
  • Object
show all
Defined in:
lib/starter/markdown.rb,
lib/starter/markdown/extender.rb

Defined Under Namespace

Modules: Extender Classes: CodeEmbedder, FootnoteProcessor

Constant Summary collapse

InlineLinkRegex =
%r{
  \[         # Literal opening bracket
    (        # Capture what we find in here
      [^\]]+ # One or more characters other than close bracket
    )        # Stop capturing
  \]         # Literal closing bracket
  \(         # Literal opening parenthesis
    (        # Capture what we find in here
      [^)]+  # One or more characters other than close parenthesis
    )        # Stop capturing
  \)         # Literal closing parenthesis
}x
LinkDefRegex =
%r{^\[(.+)\]:(.+)$}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root_doc, options = {}) ⇒ Markdown

Returns a new instance of Markdown.



26
27
28
29
30
# File 'lib/starter/markdown.rb', line 26

def initialize(root_doc, options={})
  @linted_docs = {}
  @root_doc = root_doc
  @directory = options[:directory]
end

Instance Attribute Details

#linted_docsObject (readonly)

Returns the value of attribute linted_docs.



24
25
26
# File 'lib/starter/markdown.rb', line 24

def linted_docs
  @linted_docs
end

Instance Method Details

#gfm_toc(string) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/starter/markdown.rb', line 83

def gfm_toc(string)
  toc = []
  string.each_line do |line|
    regex = %r{^(\#{1,8})\s+(.+)$}
    if match = regex.match(line)
      _all, hashes, text = match.to_a
      depth = hashes.size - 1
      text = text.strip
      anchor = text.downcase.gsub(/[\s]+/, "-").tr(":`", "")
      puts anchor.inspect
      toc << ("    " * depth) + "* [#{text}](##{anchor})"
    end
  end
  toc.join("\n") + "\n\n" + string
end

#lintObject



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/starter/markdown.rb', line 32

def lint
  lint_doc(@root_doc)
  if @directory
    files = Dir["#{@directory}/**/*.md"]
    unseen = files.to_a - @linted_docs.keys
    puts Term::ANSIColor.yellow("Unreferenced documents:")
    unseen.each do |path|
      puts "    " + Term::ANSIColor.yellow(path)
    end
  end
end

#lint_doc(path, source = nil) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/starter/markdown.rb', line 44

def lint_doc(path, source=nil)
  # no point in repeating a check
  return if @linted_docs[path]

  begin
    string = File.read(path)
    puts "    Linting doc: #{path}"
    lint_markdown(string, path)
    @linted_docs[path] = true
  rescue Errno::ENOENT
    puts Term::ANSIColor.red("Broken link in #{source}: #{path}")
  rescue => e
    pp e
  end
end


74
75
76
77
78
79
80
81
# File 'lib/starter/markdown.rb', line 74

def lint_link(match, source)
  _m, text, url = match.to_a
  if url && url =~ /\.md$/ && url !~ /^https?:\/\/|#/
    url = (Pathname.new(source).dirname + url).to_s
    url.sub!(/#.*$/, "")
    lint_doc(url, source)
  end
end

#lint_markdown(string, source = nil) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/starter/markdown.rb', line 61

def lint_markdown(string, source=nil)
  string.each_line do |line|
    normal_link = InlineLinkRegex.match(line)
    link_def = LinkDefRegex.match(line)
    if normal_link
      lint_link(normal_link, source)
    end
    if link_def
      lint_link(link_def, source)
    end
  end
end