Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Tvangeste committed Mar 7, 2011
1 parent a7cdc79 commit 0f596ad
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 38 deletions.
46 changes: 8 additions & 38 deletions dsl2txt.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# TODO:
# 1. check for key, value, desc validity
# 2. check for empty cards

require 'optparse'
require 'fileutils'

Expand All @@ -11,22 +7,19 @@

$IN = ""
$OUT = ""
$DOT = "*"
$DESC_DOT = "-"

$separotor = nil
opts = OptionParser.new
opts.on("-i FILE", "--in FILE", "input file to convert", String) {|val| $IN = val }
opts.on("-i FILE", "--in FILE", "input DSL file to convert", String) {|val| $IN = val }
opts.on("-o DIR", "--out DIR", "directory to extract txt files", String) { |val|
if ($OUT.empty?)
$OUT = val
else
$stderr.puts "Error: output file can be defined only once!\n\n"
$stderr.puts "Error: output directory can be defined only once!\n\n"
$stderr.puts opts
exit
end
}
opts.on("-s", "start each line with a separator") { $separator = $DOT }
opts.on_tail("-h", "--help", "Show this message") do
$stderr.puts opts
exit
Expand All @@ -38,44 +31,21 @@
exit
end

# load the DSL dictionary
d = nil
File.open($IN, 'rb') { |f|
d = Dictionary.load_from_dsl(f)
}

if File.exist?($OUT)
$stderr.puts "ERROR: Output directory already exist: '#{$OUT}'"
# TODO: must be enabled
# exit

# back = $OUT + ".bak"
# FileUtils.copy($OUT, back)
# $stderr.puts "Made backup copy: #{back}"
# File.open($OUT, 'rb') { |f|
# d = Dictionary.load_from_dsl(f)
#}
exit
else
$stderr.puts "Creating output directory: '#{$OUT}'"
Dir.mkdir($OUT)
end

[].each { |file_name|
desc = ""
File.foreach(file_name) { |line|
if line =~ /^(?:\xEF\xBB\xBF)?#\s*[Dd]escription\s*(.*)$/
desc = $1.strip
elsif line =~ /^\s*$/
# ignore
elsif line =~ /^(.*?):(.*)$/
key, value = $1, $2
d.add(key, value, desc)
end
}
# load the DSL dictionary
d = nil
File.open($IN, 'rb') { |f|
d = Dictionary.load_from_dsl(f)
}

d.get_txt_dicts.each { |dict|
dict.print_out("CON")
# dict.print_out("CON")
dict.extract_to_dir($OUT)
}

1 change: 1 addition & 0 deletions lib/TxtDictionary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def extract_to_dir(dir_name)
end

def print_out(out_name)
$stderr.puts "Creating file: #{out_name}"
out = File.open(out_name, 'w')
out.puts "#description #{@desc}"
out.puts ""
Expand Down

0 comments on commit 0f596ad

Please sign in to comment.