diff --git a/summarizeMD b/summarizeMD index 7ee042e..c426db5 100755 --- a/summarizeMD +++ b/summarizeMD @@ -7,7 +7,7 @@ require 'optparse/time' require 'ostruct' require 'yaml' -VERSION = "0.1.0" +VERSION = "0.2.0" class MDUtils @@ -24,11 +24,9 @@ class MDUtils 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") + temp_file = Tempfile.new("summarizeMd") - summary = "\#Summary \n\n" + summary = generate_sumary_heading(options) forbidden_words = ['Table of contents', 'define', 'pragma'] File.open(filename, 'r') do |f| @@ -49,27 +47,79 @@ class MDUtils 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" + temp_file.close end - end #(end summarize) + summary += " \n\n" + + with_output_file(options, filename) do |output_file| + if (options.replace_summary_token.empty?) + prepend_to_file(summary, temp_file, output_file) + else + replace_token_with(options.replace_summary_token, summary, temp_file, output_file) + end + end + + temp_file.unlink + puts "Done.\n" + end #(end summarize) + + def self.prepend_to_file(toPrepend, source_file, target_file) + File.open(target_file, 'w') do |fo| + fo.puts(toPrepend) + File.foreach(source_file) do |li| + fo.puts li + end + end + end + + def self.replace_token_with(token, toPrepend, source_file, target_file) + File.open(target_file, 'w') do |fo| + File.foreach(source_file) do |li| + if li.include?(token) + fo.puts toPrepend + else + fo.puts li + end + end + end + end + + def self.with_output_file(options, filename) + 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 + + if File.file?(outputFilename) && !options.force + puts "output file exists. Use --force or -f to overwrite it." + exit 1 + elsif File.directory?(outputFilename) + puts "output file cannot be a directory" + exit 2 + else + File.open(outputFilename, "w") do |output_file| + yield output_file + end + end + end + + def self.generate_sumary_heading(options) + if options.heading_level == 0 + return String.new + else + heading = String.new + options.heading_level.times { heading << "\#" } + heading << " Summary \n\n" + return heading + end + end end @@ -87,7 +137,10 @@ class OptparseSummarize options = OpenStruct.new options.encoding = "utf8" options.verbose = false + options.force = false options.output = String.new + options.replace_summary_token = String.new + options.heading_level = 1 opt_parser = OptionParser.new do |opts| opts.banner = "Usage: summarizeMD [options]" @@ -100,27 +153,48 @@ class OptparseSummarize "Specify output filename") do |out| options.output << out end + + opts.on("-t", "--replace_summary_token [summary_token]", + "Replace the line containing the supplied token, with the sumrmary, instead of putting the summary in the top of the file. If the token is not found, then no summary is inserted.") do |out| + options.replace_summary_token << out + end + + opts.on("-l", "--heading_level [heading level]", Integer, + "Specify the heading level, eg. The number of ## to insert before the Sumary heading. Set this to 0 to not add a heading to the summary.") do |l| + options.heading_level = l + end # Boolean switch. opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| options.verbose = v end + + # Boolean switch. + opts.on("-f", "--[no-]force", "force writing the output file, even if its overwrite an existing file") do |f| + options.force = f + 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 + opts.on("-h", "--help", "Show this message") do puts opts exit end # Another typical switch to print the version. - opts.on_tail("--version", "Show version") do + opts.on("--version", "Show version") do puts "#{VERSION}" exit end + + opts.separator "" + opts.separator "Exit codes:" + opts.separator " 1 = output file exists, and called without --force (-f)" + opts.separator " 2 = output file cannot be a directory" + opts.separator " 3 = --heading_level (-l) must be a positive integer or zero" end opt_parser.parse!(args) @@ -134,6 +208,10 @@ end # class OptparseExample options = OptparseSummarize.parse(ARGV) +if options.heading_level < 0 + puts "--heading_level (-l) must be a positive integer or zero" + exit 3 +end mdUtils = MDUtils.summarize([ARGV[0], options]);