Module: Vivlio::Starter::CLI::ConvertCommands

Defined in:
lib/vivlio/starter/cli/convert.rb

Overview

Module: convert(Markdown → HTML 変換)


  • 目的: Markdown を VFM で HTML に変換するコマンド群

  • 関連: 共通処理は ‘lib/vivlio/starter/cli/common.rb` を参照

Class Method Summary collapse

Class Method Details

.enable_verbose(command_or_ctx) ⇒ Object



40
41
42
# File 'lib/vivlio/starter/cli/convert.rb', line 40

def enable_verbose(command_or_ctx)
  ENV['VERBOSE'] = '1' if options_of(command_or_ctx)[:verbose]
end

.execute_convert(command_or_context, tokens_or_entries) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/vivlio/starter/cli/convert.rb', line 15

def execute_convert(command_or_context, tokens_or_entries)
  ctx = normalized_context(command_or_context)
  enable_verbose(ctx)

  base_dir = '.'
  md_files = resolve_md_files_for_convert(tokens_or_entries, base_dir)

  md_files.each do |md|
    html = md.sub(/\.md\z/, '.html')
    cmd  = %(#{Common::VFM_COMMAND} "#{md}" > "#{html}")
    ok = system(cmd)
    Common.log_warn("VFM の変換に失敗しました: #{md}") unless ok
  end

  Common.log_success('Markdown→HTML 変換が完了しました')
end

.normalized_context(command_or_ctx) ⇒ Object



33
34
35
36
37
# File 'lib/vivlio/starter/cli/convert.rb', line 33

def normalized_context(command_or_ctx)
  return command_or_ctx if command_or_ctx.is_a?(Hash)

  { options: options_of(command_or_ctx) }
end

.options_of(command_or_ctx) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/vivlio/starter/cli/convert.rb', line 45

def options_of(command_or_ctx)
  if command_or_ctx.is_a?(Hash)
    command_or_ctx[:options] || {}
  elsif command_or_ctx.respond_to?(:options)
    command_or_ctx.options || {}
  else
    {}
  end
end

.resolve_md_files_for_convert(entries_or_basenames, base_dir) ⇒ Array<String>

Entry 配列または basename 配列から Markdown ファイルパス配列を解決する

Parameters:

  • entries_or_basenames (Array<TokenResolver::Entry>, Array<String>)
  • base_dir (String)

    ベースディレクトリ(プロジェクトルート)

Returns:

  • (Array<String>)

    Markdown ファイルパスの配列



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/vivlio/starter/cli/convert.rb', line 60

def resolve_md_files_for_convert(entries_or_basenames, base_dir)
  raw = Array(entries_or_basenames).compact

  if raw.empty?
    return Dir.glob(File.join(base_dir, '*.md')).reject { |f| File.basename(f) =~ /\A(README|ROADMAP)\.md\z/ }
  end

  # Entry オブジェクトかどうかを判定
  if raw.first.respond_to?(:basename)
    raw.map { |entry| File.join(base_dir, "#{entry.basename}.md") }.uniq
  else
    # basename 配列: パスに変換
    raw.map do |bn|
      name = bn.to_s.sub(/\.md\z/, '')
      File.join(base_dir, "#{name}.md")
    end.uniq
  end
end