Class: Codemerger::Merger
- Inherits:
-
Object
- Object
- Codemerger::Merger
- Defined in:
- lib/codemerger/merger.rb
Instance Method Summary collapse
- #build_html_merged_file_content(f_name) ⇒ Object
- #build_md_merged_file_content(f_name) ⇒ Object
- #clean_dirs ⇒ Object
- #get_language_str(ext) ⇒ Object
-
#initialize(dir_name) ⇒ Merger
constructor
A new instance of Merger.
- #process_files(html: false) ⇒ Object
- #process_markdown(text) ⇒ Object
- #read_contents(fname) ⇒ Object
- #sanitize(fname) ⇒ Object
Constructor Details
#initialize(dir_name) ⇒ Merger
Returns a new instance of Merger.
7 8 9 |
# File 'lib/codemerger/merger.rb', line 7 def initialize(dir_name) @dir_name = dir_name end |
Instance Method Details
#build_html_merged_file_content(f_name) ⇒ Object
51 52 53 54 55 56 57 58 59 60 |
# File 'lib/codemerger/merger.rb', line 51 def build_html_merged_file_content(f_name) ext = f_name[/(\.[a-zA-Z]+)/] lang_str = get_language_str(ext) %( <b>#{sanitize(f_name)}</b> <pre line="1" lang="#{lang_str}"> #{read_contents(f_name)} </pre> ) end |
#build_md_merged_file_content(f_name) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/codemerger/merger.rb', line 62 def build_md_merged_file_content(f_name) ext = f_name[/(\.[a-zA-Z]+)/] if ext !~ /^\.(md|markdown)$/ lang_str = get_language_str(ext) %(_#{sanitize(f_name)}_{:.code-title} ~~~ #{lang_str} #{read_contents(f_name)} ~~~ ) else %( #{read_contents(f_name)} ) end end |
#clean_dirs ⇒ Object
11 12 13 14 15 |
# File 'lib/codemerger/merger.rb', line 11 def clean_dirs FileUtils.rm_rf "output" FileUtils.mkdir_p "output" FileUtils.mkdir_p "output/#{@dir_name}" if @dir_name != "." end |
#get_language_str(ext) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/codemerger/merger.rb', line 17 def get_language_str(ext) case ext when ".yml" then "yaml" when ".xml" then "xml" when ".html" then "xml" when ".rb" then "ruby" when ".java" then "java" when ".scala" then "scala" when ".erb" then "erb" when ".xsl" then "xslt" when ".css" then "css" when ".scss" then "scss" when ".coffee" then "coffeescript" when ".js" then "javascript" when ".sh" then "bash" when ".bat" then "batch" when ".xhtml" then "xml" when nil then "ruby" else; "text" end end |
#process_files(html: false) ⇒ Object
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 |
# File 'lib/codemerger/merger.rb', line 81 def process_files(html: false) in_files = if html Dir.glob("#{@dir_name}/**/*.{markdown,md,html}") else Dir.glob("#{@dir_name}/**/*.{markdown,md}") end in_files.sort.each do |file| in_lines = IO.readlines(file).join("") is_markdown = (file =~ /markdown$/) || (file =~ /md$/) out_fname = file[/^.*\./] out_f_name = "output/#{out_fname}html" out_d = File.dirname(out_f_name) FileUtils.mkdir_p out_d File.open(out_f_name, "w:utf-8") do |out_f| if is_markdown in_lines.gsub!(%r{(\{\{[/a-zA-Z0-9:_]+(?>\.[a-z:_A-Z0-9]{2,}){0,3}\}\})}) do |f_name_match| puts "Merging #{f_name_match}" f_name = f_name_match[2..-3] build_md_merged_file_content(f_name) end out_f << process_markdown(in_lines) else out_f << in_lines.gsub(%r{(\{\{[/a-zA-Z0-9_]+(?>\.[a-zA-Z0-9]{2,}){0,3}\}\})}) do |f_name_match| puts "Merging #{f_name_match}" f_name = f_name_match[2..-3] build_html_merged_file_content(f_name) end end end end end |
#process_markdown(text) ⇒ Object
113 114 115 116 117 |
# File 'lib/codemerger/merger.rb', line 113 def process_markdown(text) template = "string://#{File.read(File.join(File.dirname(File.expand_path(__FILE__)), 'document_with_css.html.erb'))}" doc = Kramdown::Document.new(text, syntax_highlighter: :rouge, header_links: true, auto_ids: true, template: template) doc.to_html end |
#read_contents(fname) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/codemerger/merger.rb', line 43 def read_contents(fname) if fname =~ /.+:/ `git cat-file blob #{fname}` else IO.readlines(fname).join("") end end |
#sanitize(fname) ⇒ Object
39 40 41 |
# File 'lib/codemerger/merger.rb', line 39 def sanitize(fname) fname.gsub(/.*?:/, "") end |