Module: Fbtxt::Export

Defined in:
lib/fbtxt/document/export_utils.rb

Class Method Summary collapse

Class Method Details

._merge_matches(*docs) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/fbtxt/document/export_utils.rb', line 77

def self._merge_matches( *docs )
    last_name = nil
    matches   = []

    docs.each do |doc|
      name         = doc.title

      if last_name && name != last_name
         puts "!! ERROR - league names do NOT match; cannot merge/concat - sorry"
         puts "   #{last_name} != #{name}"
         exit 1
      end

      last_name = name
      matches  += doc.matches
    end

    data = { 'name'    => last_name,
             'matches' => matches.as_json }
    data
end

.genjson(config, outdir: '.', indir: '.') ⇒ Object

usage for config e.g. [['1930/worldcup.json', ['worldcup/1930--uruguay/cup.txt']], ['1934/worldcup.json', ['worldcup/1934--italy/cup.txt']], ['2026/worldcup.json', ['worldcup/2026--usa/cup.txt', 'worldcup/2026--usa/cup_finals.txt']]]

todo/check:

change/rename indir to sourcedir/rootdir or such - why? why not?


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/fbtxt/document/export_utils.rb', line 24

def self.genjson( config, outdir: '.',
                          indir: '.' )


  config.each do |outfile, infiles|

    ##  step 1
    ## collect (parse) docs
      docs = []
      infiles.each_with_index do |infile,i|
        inpath = File.join( indir, infile )

        print "==> reading"
        print " [#{i+1}/#{infiles.size}]"  if infiles.size > 1
        print " #{inpath}...\n"

        doc = Document.read( inpath )

        if doc.errors?
          puts "!! #{doc.errors.size} parse error(s) in #{inpath}:"
          pp doc.errors
          exit 1
        end

        docs << doc
      end

      data = _merge_matches( *docs )

      outpath = File.join( outdir, outfile )

    ####################
    ## hack - use pretty_inspect for json pretty print
    txtjson =  data.pretty_inspect
    txtjson = txtjson.gsub( '=>', ': ' )
    ## puts txtjson[0,100] + "..."
    ## double check for syntax errors
    json = JSON.parse( txtjson )

    ##
    ## try alternate pretty print
    ##   puts JSON.pretty_generate( data, object_nl: "\n", array_nl: "\n", indent: 2)
    ##
    ## write_json( outpath, data )

    puts "     writing to >#{outpath}<"
    puts txtjson[0,100] + "..."

    write_text( outpath, txtjson )
  end
end