Module: Wordsmith::Generate

Included in:
Wordsmith
Defined in:
lib/wordsmith/generate.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



5
6
7
# File 'lib/wordsmith/generate.rb', line 5

def config
  @config
end

#filesObject (readonly)

Returns the value of attribute files.



5
6
7
# File 'lib/wordsmith/generate.rb', line 5

def files
  @files
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/wordsmith/generate.rb', line 5

def name
  @name
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/wordsmith/generate.rb', line 5

def output
  @output
end

Instance Method Details

#generate(args = []) ⇒ Object



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
# File 'lib/wordsmith/generate.rb', line 25

def generate(args = [])
  setup

  formats = args.empty? ? OUTPUT_TYPES : args

  formats.each do |format|
    raise "No generator found for #{format}" unless respond_to?("to_#{format}")

    run_callback("before_#{format}")
    out = if format == "mobi"
      send("to_#{format}")
    else
      run_command(send("to_#{format}"))
    end
    run_callback("after_#{format}")

    if $?.exitstatus == 0 && out.empty? || format == "mobi" && $?.exitstatus == 1
      info "Created #{output}" +
        (format == "html" ? "/index.html" : ".#{format}")
    else
      raise "#{format} generator failed"
    end
  end
  run_callback("after_all")
end

#run_callback(meth) ⇒ Object



51
52
53
# File 'lib/wordsmith/generate.rb', line 51

def run_callback(meth)
  run_command(config[meth]) if config[meth]
end

#setupObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/wordsmith/generate.rb', line 7

def setup
  @name = File.basename(local("."))
  @config = YAML::parse(File.open(local(".wordsmith"))).
    transform rescue {}

  run_callback("before_all")
  content_dir = local(File.join("content"))
  @files = Dir.glob(content_dir + "/**/*.*").sort.join(" \\\n")

  if files.empty?
    raise "Exiting.. Nothing to generate in #{content_dir}." +
      "\nHave you run 'wordsmith new'?"
  end

  Dir.mkdir(local("final")) unless File.exists?(local("final"))
  @output = local(File.join("final", name))
end

#to_epubObject



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/wordsmith/generate.rb', line 69

def to_epub
  info "Generating epub..."

  

  cmd = "pandoc -f markdown_mmd -S -o #{output}.epub -t epub"
  cmd += " \\\n--epub-metadata=#{}" if 
  cmd += " \\\n--epub-cover-image=#{cover}" if cover
  cmd += " \\\n--epub-stylesheet=#{epub_stylesheet}" if epub_stylesheet
  cmd += " \\\n#{files}"
  cmd
end

#to_htmlObject



55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/wordsmith/generate.rb', line 55

def to_html
  info "Generating html..."

  compile_stylesheets
  copy_assets

  cmd = "pandoc -f markdown_mmd -s -S --toc -o #{File.join(output, "index.html")} -t html"
  stylesheets.each { |stylesheet| cmd += " -c #{stylesheet}" }
  cmd += " -B #{header}" if header
  cmd += " -A #{footer}" if footer
  cmd += " \\\n#{files}"
  cmd
end

#to_mobiObject



82
83
84
85
86
87
88
89
# File 'lib/wordsmith/generate.rb', line 82

def to_mobi
  if File.exists?(output + ".epub")
    info "Generating mobi..."
    Kindlegen.run("#{output}.epub", "-o", "#{name}.mobi")
  else
    info "Skipping .mobi (#{name}.epub doesn't exist)"
  end
end

#to_pdfObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/wordsmith/generate.rb', line 91

def to_pdf
  info "Generating pdf..."

  engine = ""

  [["xetex", "xelatex"], ["pdftex", "pdflatex"], "lualatex"].each do |e|
    if e.is_a? Array
      cmd, name = e
    else
      cmd = name = e
    end
    if can_run?(cmd + " -v")
      engine = name
      break
    end
  end

  cmd = "pandoc -f markdown_mmd -N --toc -o #{output}.pdf #{files}"
  cmd += " --latex-engine=#{engine}" unless engine.empty?
  cmd += " -V mainfont='#{config['font']}'" unless (config.fetch('font', '')).empty?
  cmd
end