Class: Mdextab::Makemdtab

Inherits:
Object
  • Object
show all
Defined in:
lib/mdextab/makemdtab.rb

Overview

テーブル拡張Markdown生成クラス

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts, eruby_variable_str, eruby_static_str, obj_by_yaml, mes = nil) ⇒ Makemdtab

初期化

Options Hash (opts):

  • :debug (Symbol)

    Messagexクラスのインスタンスに与えるデバッグモード



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/mdextab/makemdtab.rb', line 19

def initialize(opts, eruby_variable_str, eruby_static_str, obj_by_yaml, mes=nil)
  @dataop = opts[:dataop]
  @datayamlfname = opts[:data]
  @eruby_variable_str = eruby_variable_str
  @eruby_static_str = eruby_static_str
  @outputfname = opts[:output]
  @obj_by_yaml = obj_by_yaml

  @mes = mes
  @mes ||= if opts[:debug]
             Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :debug)
           elsif opts[:verbose]
             Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :verbose)
           else
             Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0)
           end
  @mes.add_exitcode("EXIT_CODE_ILLEGAL_DATAOP")
  Filex::Filex.setup(@mes)

  @output = @mes.exc_file_write(@outputfname) { File.open(@outputfname, "w") }
end

Class Method Details

.create(opts, fname_variable, fname_static, root_settingfile, mes) ⇒ Makemdtab

ファイルから生成(使われていない?)

Options Hash (opts):

  • :debug (String)

    Messagexクラスのインスタンスに与えるデバッグモード

  • :output (IO)

    出力先IO



52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/mdextab/makemdtab.rb', line 52

def self.create(opts, fname_variable, fname_static, root_settingfile, mes)
  Filex::Filex.setup(mes)

  unless File.exist?(opts[:output])
    mes.output_fatal("Can't find #{opts[:output]}")
    exit(mes.ec("EXIT_CODE_CANNOT_FIND_FILE"))
  end
  obj_by_yaml = Filex::Filex.check_and_load_yamlfile(root_settingfile, mes)

  str_variable = Filex::Filex.check_and_load_file(fname_variable, mes) if fname_variable
  str_static = ["<% ", Filex::Filex.check_and_expand_file(fname_static, obj_by_yaml, mes), "%>"].join("\n") if fname_static

  Makemdtab.new(opts, str_variable, str_static, obj_by_yaml, mes)
end

Instance Method Details

#load_file_include(root_dir, eruby_fname, objx) ⇒ Array<String>

eRubyスクリプト取り込み処理



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/mdextab/makemdtab.rb', line 105

def load_file_include(root_dir, eruby_fname, objx)
  # eruby_fnameはfileReadメソッド呼び出しを含むeRubyスクリプトファイル
  # fileReadメソッドは、引数を読み込むべきファイルへのパスに変換して、ファイルを読み込む

  # fileReadメソッドで参照するparentDirという変数に、root_dirの値を割り当てる
  objy = { "parentDir" => "%q!#{root_dir}!" }
  # @eruby_variable_strにfileReadメソッドの定義が含まれる
  eruby_exanpded_str = ""
  if @eruby_variable_str
    eruby_exanpded_str = if @eruby_variable_str.empty?
                           ""
                         else
                           # 変数parent_dirをobjyで定義された値に置換て、fileReadメソッドの定義を完成させる
                           # 置換して得られた文字列を、もう一度eRubyスクリプトにする
                           ["<% ", Filex::Filex.expand_str(@eruby_variable_str, objy, @mes), " %>"].join("\n")
                         end
  end
  # fileReadメソッド呼び出しを含むeRubyスクリプトファイルを読み込む
  mbstr = Filex::Filex.check_and_load_file(eruby_fname, @mes)
  # fileReadメソッド定義とそれ以外のメソッド定義と読み込んだeRubuスクリプトを連結して一つのeRubyスクリプトにする
  dx = [eruby_exanpded_str, @eruby_static_str, mbstr].join("\n")
  if dx.strip.empty?
    puts "empty eruby_fname=#{eruby_fname}"
  else
    # ハッシュobjxは、メソッドfileReadの実引数を、読み込むべきファイルの相対パスに変換する定義を含んでいる
    # Filex::Filex.expand_strに渡すハッシュは、エラーメッセージに用いるためのものであり、eRubyスクリプトとは無関係である
    array = [Filex::Filex.expand_str(dx, objx, @mes, { "eruby_fname" => eruby_fname })]
  end

  array
end

#load_yaml_to_md(datayamlfname, templatefile, objx) ⇒ Array<String>

eRubyスクリプトファイルでもあるYAML形式ファイルからテーブル拡張Markdown形式に変換



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/mdextab/makemdtab.rb', line 144

def load_yaml_to_md(datayamlfname, templatefile, objx)
  @mes.output_debug("datayamlfname=#{datayamlfname}")
  @mes.output_debug("objx=#{objx}")

  # いったんeRubyスクリプトファイルとして読み込んで置換したあと、YAML形式として再度読み込み、Rubyのハッシュに変換する
  objy = Filex::Filex.check_and_expand_yamlfile(datayamlfname, objx, @mes)
  @mes.output_debug("objy=#{objy}")
  @mes.output_debug("templatefile=#{templatefile}")

  # YAML形式をテーブル拡張Markdwon形式に変換するeRubyスクリプトファイルを読み込む
  erubystr = Filex::Filex.check_and_load_file(templatefile, @mes)
  @mes.output_debug("erubystr=#{erubystr}")
  # メソッド定義を含むeRubyスクリプトとtemplatefileを一つのeRubyスクリプトにする
  dx = [@eruby_static_str, erubystr].join("\n")
  @mes.output_debug("dx=#{dx}")
  # eRubyスクリプトにdatayamlfnameの内容を置換用ハッシュとして適用して、テーブル拡張Markdown形式に変換する
  # Filex::Filex.expand_strに渡すハッシュは、エラーメッセージに用いるためのものであり、eRubyスクリプトとは無関係である
  [Filex::Filex.expand_str(dx, objy, @mes, { "datayamlfname" => datayamlfname, "templatefile" => templatefile })]
end

#make_md2(root_dir, templatefile = nil, auxhs = {}) ⇒ void

This method returns an undefined value.

YAML形式ファイルからテーブル拡張Markdown形式ファイルを生成



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/mdextab/makemdtab.rb', line 74

def make_md2(root_dir, templatefile=nil, auxhs={})
  # 補助的な置換用ハッシュを@obj_by_yamlにマージする
  objx = @obj_by_yaml.merge(auxhs)
  case @dataop
  when :FILE_INCLUDE
    # ハッシュobjxは、メソッドfileReadの実引数を、読み込むべきファイルの相対パスに変換する定義を含んでいる
    array = load_file_include(root_dir, @datayamlfname, objx)
  when :YAML_TO_MD
    unless templatefile
      @mes.output_fatal("Not specified templatefile")
      exit(@mes.ec("EXIT_CODE_NOT_SPECIFIED_FILE"))
    end
    if templatefile.strip.empty?
      @mes.output_fatal("Not specified templatefile")
      exit(@mes.ec("EXIT_CODE_NOT_SPECIFIED_FILE"))
    end
    # YAMLファイルを、eRubyスクリプトであるtemplatefileを用いてテーブル拡張Markdown形式に変換する
    array = load_yaml_to_md(@datayamlfname, templatefile, objx)
  else
    array = []
  end
  array.map { |x| @mes.exc_file_write(@outputfname) { @output.puts(x) } }
end

#post_processvoid

This method returns an undefined value.

終了処理



168
169
170
171
# File 'lib/mdextab/makemdtab.rb', line 168

def post_process
  @mes.exc_file_close(@outputfname) { @output&.close }
  @output = nil
end