Module: Zettacode::Parse
- Defined in:
- lib/zettacode/parse/parse.rb
Class Method Summary collapse
- .call(filepath) ⇒ Object
- .clean(dirty_examples) ⇒ Object
- .echo(title, text) ⇒ Object
- .read_content_from(filepath) ⇒ Object
- .save_files(examples, folder) ⇒ Object
- .scrap_examples_from(content) ⇒ Object
Class Method Details
.call(filepath) ⇒ Object
12 13 14 15 16 17 |
# File 'lib/zettacode/parse/parse.rb', line 12 def self.call(filepath) raw_content, to_folder = read_content_from filepath dirty_examples = scrap_examples_from raw_content clean_examples = clean dirty_examples save_files clean_examples, to_folder end |
.clean(dirty_examples) ⇒ Object
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 |
# File 'lib/zettacode/parse/parse.rb', line 59 def self.clean(dirty_examples) examples = [] dirty_examples.each do |example| # lang = /header\|([\w\d\s.\-_*\(\)]+)/.match(example[:lang])&.captures&.first lang = example[:lang][11, example[:lang].size - 15] filename = lang.tr(" ", "_").downcase filename.tr!("/", "-") syntax = nil lines = example[:code] raw = lines.join("\n") # Clean syntaxhighlight tag lines.each do |line| next if line == "" # <syntaxhighlight lang="LANG"> filter = /<syntaxhighlight\s+lang="([\w\d]+)">/ value = filter.match(line)&.captures&.first unless value.nil? syntax = value line.gsub!("<syntaxhighlight lang=\"#{value}\">", "") else line.gsub!("</text>", "") line.gsub!("</revision>", "") line.gsub!("</page>", "") line.gsub!("</mediawiki>", "") line = nil if line.strip.start_with? "<sha1>" end end code = lines.join("\n") code.gsub!("</syntaxhighlight>", "") code.gsub!("<pre>", "") code.gsub!("</pre>", "") code.gsub!("<", "<") code.gsub!(">", ">") code.gsub!("<code>", "'") code.gsub!("</code>", "'") examples << { lang: lang, filename: filename, syntax: syntax, code: code, raw: raw } end examples end |
.echo(title, text) ⇒ Object
7 8 9 10 |
# File 'lib/zettacode/parse/parse.rb', line 7 def self.echo(title, text) print title.rjust(12).colorize(:green) puts " #{text}" end |
.read_content_from(filepath) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/zettacode/parse/parse.rb', line 19 def self.read_content_from(filepath) unless File.exist?(filepath) echo "ERROR", "File not found! (#{filepath})" exit 1 end echo "Parsing", filepath content = File.read(filepath) title = /<title>([\w\d\s._-]+.[\w\d\s._-]+)<\/title>/ name = title.match(content).captures.first name.tr!("/", ".") name.tr!(" ", "_") name.downcase! folder = File.join(Zettacode::OUTPUT_FOLDER, name) FileUtils.mkdir_p(folder) unless Dir.exist? folder [content, folder] end |
.save_files(examples, folder) ⇒ Object
107 108 109 110 111 112 113 114 |
# File 'lib/zettacode/parse/parse.rb', line 107 def self.save_files(examples, folder) examples.each do |example| filepath = File.join(folder, "#{example[:filename]}.txt") File.open(filepath, "w") { |file| file.write(example[:code]) } end echo "Folder", folder echo "Saving", "#{examples.size} files" end |
.scrap_examples_from(content) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/zettacode/parse/parse.rb', line 37 def self.scrap_examples_from(content) examples = [] example = nil skip = true content.split("\n").each do |line| if line.start_with? "=={{header|" skip = false examples << example unless example.nil? example = {lang: line, code: []} elsif line.downcase.start_with? "{{out}}" skip = true next elsif skip next else example[:code] << line end end examples << example examples end |