Class: Snaptoken::Commands::Build

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

Instance Attribute Summary

Attributes inherited from BaseCommand

#config

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseCommand

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

Constructor Details

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

Class Method Details

.nameObject



2
3
4
# File 'lib/snaptoken/commands/build.rb', line 2

def self.name
  "build"
end

.summaryObject



6
7
8
# File 'lib/snaptoken/commands/build.rb', line 6

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

.usageObject



10
11
12
# File 'lib/snaptoken/commands/build.rb', line 10

def self.usage
  "[-q]"
end

Instance Method Details

#runObject



20
21
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
# File 'lib/snaptoken/commands/build.rb', line 20

def run
  args = @opts[:quiet] ? ["--quiet"] : []

  needs! :config, :repo

  @git.load!(full_diffs: true, diffs_ignore_whitespace: true) do |step_num|
    print "\r\e[K[repo/ -> Tutorial] Step #{step_num}" unless @opts[:quiet]
  end
  puts unless @opts[:quiet]

  num_steps = @tutorial.num_steps

  if @tutorial.config[:diff_transformers]
    transformers = @tutorial.config[: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
      Snaptoken::DiffTransformers.const_get(transformer).new(options)
    end

    @tutorial.transform_diffs(transformers) do |step_num|
      print "\r\e[K[Transform diffs] Step #{step_num}/#{num_steps}" unless @opts[:quiet]
    end
    puts unless @opts[:quiet]
  end

  FileUtils.mkdir_p(File.join(@tutorial.config[:path], "template"))
  FileUtils.cd(File.join(@tutorial.config[:path], "template")) do
    FileUtils.rm_rf("../build")
    FileUtils.mkdir_p("../build/html")
    FileUtils.mkdir_p("../build/html-offline")

    include_default_css = true
    if File.exist?("page.html.erb")
      @tutorial.page_template = File.read("page.html.erb")
      include_default_css = false
    end

    if File.exist?("step.html.erb")
      @tutorial.step_template = File.read("step.html.erb")
    end
    @tutorial.step_template.gsub!(/\\\s*/, "")

    @tutorial.pages.each do |page|
      print "\r\e[K[Tutorial -> build/] Page #{page.filename}" unless @opts[:quiet]

      html = page.to_html(@tutorial, false)
      File.write("../build/html/#{page.filename}.html", html)

      offline_html = page.to_html(@tutorial, true)
      File.write("../build/html-offline/#{page.filename}.html", offline_html)
    end
    puts unless @opts[:quiet]

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

      next if %w(page.html.erb step.html.erb).include? name
      next if name.start_with? "_"

      # XXX: currently only processes top-level ERB template files.
      if name.end_with? ".erb"
        output = Snaptoken::Template.new(File.read(f), @tutorial, offline: false).render_template
        File.write("../build/html/#{name[0..-5]}", output)

        output = Snaptoken::Template.new(File.read(f), @tutorial, offline: true).render_template
        File.write("../build/html-offline/#{name[0..-5]}", output)
      else
        FileUtils.cp_r(f, "../build/html/#{name}")
        FileUtils.cp_r(f, "../build/html-offline/#{name}")
      end
    end

    if include_default_css && !File.exist?("../build/html/style.css")
      output = Snaptoken::Template.new(Snaptoken::DefaultTemplates::CSS, @tutorial, offline: false).render_template
      File.write("../build/html/style.css", output)
    end
    if include_default_css && !File.exist?("../build/html-offline/style.css")
      output = Snaptoken::Template.new(Snaptoken::DefaultTemplates::CSS, @tutorial, offline: true).render_template
      File.write("../build/html-offline/style.css", output)
    end
  end
end

#setopts!(o) ⇒ Object



14
15
16
17
18
# File 'lib/snaptoken/commands/build.rb', line 14

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