-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_anchors.rb
executable file
·100 lines (75 loc) · 2.18 KB
/
add_anchors.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
60
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
#!/usr/bin/env ruby
# Add Markdown anchors to Heading elements (only levels 1 and 2)
# The "Heading Alternate Syntax" must have at least four '=' or '-' characters otherwise the anchor is not added
# frozen_string_literal: true
require 'tempfile'
require 'fileutils'
# rubocop:disable Metrics/MethodLength
def read_header(source_file, dest_file)
line = source_file.gets
return line unless line && line.chomp == '---'
found_header_start = false
loop do
line.chomp!
dest_file.puts line
if line == '---'
return if found_header_start
found_header_start = true
end
break unless (line = source_file.gets)
end
end
# rubocop:enable Metrics/MethodLength
def contain_link?(str)
str =~ /\[.*?\]\(.*?\)/
end
def to_anchor(str)
return str if contain_link?(str)
anchor = str.downcase
.gsub(/[^a-z0-9]+/, '_')
.gsub(/_+$/, '')
"[#{str}](##{anchor})"
end
def flush_buffer(buffer, dest_file)
buffer.each { |l| dest_file.puts l }
buffer.clear
end
def heading_hash_syntax(line, buffer, dest_file)
return false unless /^(====+)|(----+)/.match?(line)
last_line = buffer.pop
flush_buffer(buffer, dest_file)
dest_file.puts to_anchor(last_line)
dest_file.puts line
true
end
def heading_alternate_syntax(line, buffer, dest_file)
return false unless (m = line.match(/(^##?[^#])(.*)/))
flush_buffer(buffer, dest_file)
dest_file.puts "#{m[1]}#{to_anchor(m[2])}"
true
end
def markdown_headings(line, buffer, dest_file)
heading_hash_syntax(line, buffer, dest_file) || heading_alternate_syntax(line, buffer, dest_file)
end
def process_file(source_file, dest_file)
buffer = []
line = read_header(source_file, dest_file)
# check the first line after the header is a markdown heading
markdown_headings(line.chomp, buffer, dest_file) if line
while (line = source_file.gets)
line.chomp!
buffer << line unless markdown_headings(line, buffer, dest_file)
end
flush_buffer(buffer, dest_file)
end
if ARGV.empty?
puts 'Specify file'
exit 1
end
file = File.open(ARGV[0], 'r')
temp_file = Tempfile.new('anchors')
process_file(file, temp_file)
file.close
temp_file.close
FileUtils.cp(temp_file.path, ARGV[0])
temp_file.unlink