Class: Leg::Commands::Build

Inherits:
BaseCommand show all
Defined in:
lib/leg/commands/build.rb

Instance Attribute Summary

Attributes inherited from BaseCommand

#config, #opts

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

#git_to_litdiff!, inherited, #initialize, #litdiff_to_git!, #needs!, #output, #parseopts!

Constructor Details

This class inherits a constructor from Leg::Commands::BaseCommand

Class Method Details

.nameObject



4
5
6
# File 'lib/leg/commands/build.rb', line 4

def self.name
  "build"
end

.summaryObject



8
9
10
# File 'lib/leg/commands/build.rb', line 8

def self.summary
  "Render repo/ into an HTML or Markdown book."
end

.usageObject



12
13
14
# File 'lib/leg/commands/build.rb', line 12

def self.usage
  "[-q]"
end

Instance Method Details

#runObject



22
23
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/leg/commands/build.rb', line 22

def run
  needs! :config, :repo

  tutorial = @git.load!(full_diffs: true, diffs_ignore_whitespace: true) do |step_num|
    output "\r\e[K[repo/ -> Tutorial] Step #{step_num}"
  end
  output "\n"

  num_steps = tutorial.num_steps

  if @config.options[:diff_transformers].nil?
    @config.options[:diff_transformers] = [
      { 'FoldSections' => {
        unfold_before_new_section: true,
        section_types: [
          { name: 'comments', start: "^/\\*\\*\\*.+\\*\\*\\*/$", end: nil },
          { name: 'braces', start: "^\\S.*{$", end: "^}( \\w+)?;?$" }
        ]
      }},
      'TrimBlankLines',
      'OmitAdjacentRemovals'
    ]
  end

  if @config.options[:diff_transformers]
    transformers = @config.options[:diff_transformers].map do |transformer_config|
      if transformer_config.is_a? String
        transformer = transformer_config
        options = {}
      else
        transformer = transformer_config.keys.first
        options = transformer_config.values.first
      end
      Leg::DiffTransformers.const_get(transformer).new(options)
    end

    tutorial.transform_diffs(transformers) do |step_num|
      output "\r\e[K[Transform diffs] Step #{step_num}/#{num_steps}"
    end
    output "\n"
  end

  templates = Dir[File.join(@config.path, "template{,-?*}")].map do |template_dir|
    [template_dir, File.basename(template_dir).split("-")[1] || "html"]
  end
  if templates.empty?
    templates = [[nil, "html"], [nil, "md"]]
  end

  FileUtils.rm_rf(File.join(@config.path, "build"))
  templates.each do |template_dir, format|
    FileUtils.cd(@config.path) do
      FileUtils.mkdir_p("build/#{format}")

      include_default_css = (format == "html")
      page_template = Leg::DefaultTemplates::PAGE[format]
      if template_dir && File.exist?(File.join(template_dir, "page.#{format}.erb"))
        page_template = File.read(File.join(template_dir, "page.#{format}.erb"))
        include_default_css = false
      end
      page_template.gsub!(/\\\s*/, "")

      step_template = Leg::DefaultTemplates::STEP[format]
      if template_dir && File.exist?(File.join(template_dir, "step.#{format}.erb"))
        step_template = File.read(File.join(template_dir, "step.#{format}.erb"))
      end
      step_template.gsub!(/\\\s*/, "")

      tutorial.pages.each do |page|
        output "\r\e[K[Tutorial -> build/] Page #{page.filename}"

        content = Leg::Template.render_page(page_template, step_template, format, page, tutorial, @config)
        File.write("build/#{format}/#{page.filename}.#{format}", content)
      end
      output "\n"

      if template_dir
        FileUtils.cd(template_dir) do
          Dir["*"].each do |f|
            name = File.basename(f)

            next if ["page.#{format}.erb", "step.#{format}.erb"].include? name
            next if name.start_with? "_"

            # XXX: currently only processes top-level ERB template files.
            if name.end_with? ".erb"
              content = Leg::Template.render(File.read(f), tutorial, @config)
              File.write("../build/#{format}/#{name[0..-5]}", content)
            else
              FileUtils.cp_r(f, "../build/#{format}/#{name}")
            end
          end
        end
      end

      if include_default_css && !File.exist?("build/#{format}/style.css")
        content = Leg::Template.render(Leg::DefaultTemplates::CSS, tutorial, @config)
        File.write("build/#{format}/style.css", content)
      end
    end
  end
end

#setopts!(o) ⇒ Object



16
17
18
19
20
# File 'lib/leg/commands/build.rb', line 16

def setopts!(o)
  o.on("-q", "--quiet", "Don't output progress") do |q|
    @opts[:quiet] = q
  end
end