Class: Codemerger::Merger
- Inherits:
-
Object
- Object
- Codemerger::Merger
- Defined in:
- lib/codemerger/merger.rb
Instance Attribute Summary collapse
-
#markdown ⇒ Object
readonly
Returns the value of attribute markdown.
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 ⇒ Object
- #processMarkdown(text) ⇒ Object
- #read_contents(fname) ⇒ Object
- #sanitize(fname) ⇒ Object
Constructor Details
#initialize(dir_name) ⇒ Merger
Returns a new instance of Merger.
36 37 38 |
# File 'lib/codemerger/merger.rb', line 36 def initialize(dir_name) @dir_name = dir_name end |
Instance Attribute Details
#markdown ⇒ Object (readonly)
Returns the value of attribute markdown.
35 36 37 |
# File 'lib/codemerger/merger.rb', line 35 def markdown @markdown end |
Instance Method Details
#build_html_merged_file_content(f_name) ⇒ Object
75 76 77 78 79 80 81 82 83 84 |
# File 'lib/codemerger/merger.rb', line 75 def build_html_merged_file_content(f_name) ext = f_name[/(\.[a-zA-Z]+)/] lang_str = get_language_str(ext) %Q{ <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
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/codemerger/merger.rb', line 85 def build_md_merged_file_content(f_name) ext = f_name[/(\.[a-zA-Z]+)/] lang_str = get_language_str(ext) %Q{_#{sanitize(f_name)}_ ```#{lang_str} #{read_contents(f_name)} ``` } end |
#clean_dirs ⇒ Object
39 40 41 42 43 |
# File 'lib/codemerger/merger.rb', line 39 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
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/codemerger/merger.rb', line 44 def get_language_str(ext) case ext when ".yml"; "yaml" when ".xml"; "xml" when ".html"; "xml" when ".rb"; "ruby" when ".java"; "java" when ".scala"; "scala" when ".erb"; "erb" when ".xsl"; "xslt" when ".css"; "css" when ".scss"; "scss" when ".coffee"; "coffeescript" when ".js"; "javascript" when ".sh"; "bash" when ".bat"; "batch" when ".xhtml"; "xml" when nil; "ruby" else; "text" end end |
#process_files ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
# File 'lib/codemerger/merger.rb', line 95 def process_files in_files = Dir.glob("#{@dir_name}/*.{markdown,md,html}") in_files.sort.each do |file| in_lines = IO.readlines(file).join('') is_markdown = (file =~ /markdown$/) || (file =~ /md$/) out_fname = file[/^.*\./] out_f = File.new("output/#{out_fname}html", 'w') if is_markdown in_lines.gsub!(/(\{\{[\/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 << processMarkdown(in_lines) else out_f << in_lines.gsub(/(\{\{[\/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 |
#processMarkdown(text) ⇒ Object
118 119 120 121 122 123 124 |
# File 'lib/codemerger/merger.rb', line 118 def processMarkdown(text) @markdown ||= Redcarpet::Markdown.new(HTMLwithAlbino.new, :autolink => true, :space_after_headers => true, :superscript => true, :fenced_code_blocks => true, :tables => true, :no_intra_emphasis => true) result = @markdown.render(text) result end |
#read_contents(fname) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/codemerger/merger.rb', line 68 def read_contents(fname) if fname =~ /.+:/ `git cat-file blob #{fname}` else IO.readlines(fname).join('') end end |
#sanitize(fname) ⇒ Object
65 66 67 |
# File 'lib/codemerger/merger.rb', line 65 def sanitize(fname) fname.gsub(/.*?:/, "") end |