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

初期化

Parameters:

  • opt (Hash)

    オプション

  • eruby_variable_str (String)

    2回の置き換えが必要なeRubyスクリプト

  • eruby_static_str (String)

    1回の置き換えが必要なeRubyスクリプト

  • obj_by_yaml (Hash)

    eRubyスクリプト向け置換用ハッシュ

  • mes (Messagex) (defaults to: nil)

    Messagexクラスのインスタンス



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

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
  unless @mes
    if opts[:debug]
      @mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :debug)
    elsif opts[:verbose]
      @mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0, :verbose)
    else
      @mes = Messagex::Messagex.new("EXIT_CODE_NORMAL_EXIT", 0)
    end
  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) ⇒ Object

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

Parameters:

  • opt (Hash)

    オプション

  • fname_variable_str (String)

    2回の置き換えが必要なeRubyスクリプトファイル名

  • fname_static_str (String)

    1回の置き換えが必要なeRubyスクリプトファイル名

  • root_settingfile (String)

    eRubyスクリプト向け置換用YAML形式ファイル名

  • mes (Messagex)

    Messagexクラスのインスタンス



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

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) ⇒ Object

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

Parameters:

  • root_dir (String)

    ルートディレクト(eruby_fnameが示す相対パスの起点)

  • eruby_fname (String)

    fileReadメソッド呼び出しを含むeRubyスクリプトファイル名

  • objx (Hash)

    eRubyスクリプト向け置換用ハッシュ



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
136
# File 'lib/mdextab/makemdtab.rb', line 106

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
    if @eruby_variable_str.empty?
      eruby_exanpded_str = ""
    else
      # 変数parent_dirをobjyで定義された値に置換て、fileReadメソッドの定義を完成させる
      # 置換して得られた文字列を、もう一度eRubyスクリプトにする
      eruby_exanpded_str = ["<% ", 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) ⇒ Object

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

Parameters:

  • datayamlfname (String)

    eRubyスクリプトファイルでもあるYAML形式ファイル

  • templatefile (String)

    YAML形式をテーブル拡張Markdwon形式に変換するeRubyスクリプトファイル

  • objx (Hash)

    eRubyスクリプト向け置換用ハッシュ



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

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スクリプトとは無関係である
  array = [Filex::Filex.expand_str(dx, objy, @mes, { "datayamlfname" => datayamlfname, "templatefile" => templatefile })]

  array
end

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

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

Parameters:

  • root_dir (String)

    ルートディレクト(templatefileが示す相対パスの起点)

  • templatefile (String, nil) (defaults to: nil)

    テーブル拡張Makrdown形式の変換元YAML形式ファイルへの相対パス

  • auxhs (Hash) (defaults to: {})

    eRubyスクリプト向け置換用ハッシュ(@obj_by_yamlにマージする)



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

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.

終了処理



171
172
173
174
# File 'lib/mdextab/makemdtab.rb', line 171

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