-
Notifications
You must be signed in to change notification settings - Fork 0
/
summarizeMD
executable file
·139 lines (106 loc) · 3.23 KB
/
summarizeMD
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
101
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
#!/usr/bin/env ruby
require 'tempfile'
require 'fileutils'
require 'optparse'
require 'optparse/time'
require 'ostruct'
require 'yaml'
VERSION = "0.1.0"
class MDUtils
def self.summarize(args)
filename = args[0]
options = args[1]
if !File.exist?(filename)
abort("Please enter a valid MD file.")
end
puts "Generating summary for file " + filename
# In order to be sure to not write on existing file
timeNow = Time.now.to_f
tmpfilename = "tmp_00_" + timeNow.inspect + ".md"
temp_file = File.new(tmpfilename, "w")
summary = "\#Summary \n\n"
forbidden_words = ['Table of contents', 'define', 'pragma']
File.open(filename, 'r') do |f|
if options.verbose
puts "Generate summary for section(s): "
end
f.each_line do |line|
if !(!line.start_with?("#") || forbidden_words.any? { |w| line =~ /#{w}/ })
title = line.gsub("#", "").strip
href = "section-id-#{$.}"
anchor = "<div id='#{href}'/>\n\n"
temp_file.puts anchor + line
summary += " " * (line.count("#")-1) + "- [#{title}](\##{href})" + "\n"
if options.verbose
puts "\t- [#{title}]"
end
else
temp_file.puts line
end
end
temp_file.close
summary += " \n\n"
outputFilename = "summarized_" + filename
if !options.output.empty?
outputFilename = options.output
outputComponents = outputFilename.split(".")
if outputComponents.last.downcase != "md"
outputFilename += ".md"
if options.verbose
puts "Forcing adding extension."
end
end
end
system("echo \"#{summary}\" | cat - #{tmpfilename} > #{outputFilename}")
system("rm #{tmpfilename}")
puts "Done.\n"
end
end #(end summarize)
end
class OptparseSummarize
CODES = %w[iso-2022-jp shift_jis euc-jp utf8 binary]
CODE_ALIASES = { "jis" => "iso-2022-jp", "sjis" => "shift_jis" }
#
# Return a structure describing the options.
#
def self.parse(args)
# The options specified on the command line will be collected in *options*.
# We set default values here.
options = OpenStruct.new
options.encoding = "utf8"
options.verbose = false
options.output = String.new
opt_parser = OptionParser.new do |opts|
opts.banner = "Usage: summarizeMD <filename> [options]"
opts.separator ""
opts.separator "Specific options:"
# Output name.
opts.on("-o", "--output [filename]",
"Specify output filename") do |out|
options.output << out
end
# Boolean switch.
opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
options.verbose = v
end
opts.separator ""
opts.separator "Common options:"
# No argument, shows at tail. This will print an options summary.
# Try it and see!
opts.on_tail("-h", "--help", "Show this message") do
puts opts
exit
end
# Another typical switch to print the version.
opts.on_tail("--version", "Show version") do
puts "#{VERSION}"
exit
end
end
opt_parser.parse!(args)
options
end # parse()
end # class OptparseExample
## ---- Main ----
options = OptparseSummarize.parse(ARGV)
mdUtils = MDUtils.summarize([ARGV[0], options]);