Class: Carta::CLI::Compile

Inherits:
Object
  • Object
show all
Defined in:
lib/carta/cli/compile.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thor) ⇒ Compile

Returns a new instance of Compile.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/carta/cli/compile.rb', line 23

def initialize(thor)
  if Dir.exists?('manuscript')
    @thor = thor
    @PROJECT_DIR    = Dir.pwd
    @BUILD_DIR      = "build"
    @LAYOUT_DIR     = "#{@PROJECT_DIR}/layouts"
    @MANUSCRIPT_DIR = "#{@PROJECT_DIR}/manuscript"
    @FIGURE_DIR     = "#{@MANUSCRIPT_DIR}/figures"
    @ASSET_DIR      = "#{@PROJECT_DIR}/assets"
    @EPUB_DIR       = "#{@PROJECT_DIR}/assets"
    @HTML_DIR       = "#{@PROJECT_DIR}/assets"
    @ASSET_FILES    = 'css,otf,woff,mov,m4v,mp4,mp3,jpeg,jpg,png,svg,gif'
    @book = YAML.load_file("#{@MANUSCRIPT_DIR}/book.yaml")
  else
    thor.error 'No book found to compile!'
  end
end

Instance Attribute Details

#ASSET_DIRObject (readonly)

Returns the value of attribute ASSET_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def ASSET_DIR
  @ASSET_DIR
end

#ASSET_FILESObject (readonly)

Returns the value of attribute ASSET_FILES.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def ASSET_FILES
  @ASSET_FILES
end

#bookObject (readonly)

Returns the value of attribute book.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def book
  @book
end

#BUILD_DIRObject (readonly)

Returns the value of attribute BUILD_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def BUILD_DIR
  @BUILD_DIR
end

#EPUB_DIRObject (readonly)

Returns the value of attribute EPUB_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def EPUB_DIR
  @EPUB_DIR
end

#FIGURE_DIRObject (readonly)

Returns the value of attribute FIGURE_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def FIGURE_DIR
  @FIGURE_DIR
end

#HTML_DIRObject (readonly)

Returns the value of attribute HTML_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def HTML_DIR
  @HTML_DIR
end

#LAYOUT_DIRObject (readonly)

Returns the value of attribute LAYOUT_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def LAYOUT_DIR
  @LAYOUT_DIR
end

#MANUSCRIPT_DIRObject (readonly)

Returns the value of attribute MANUSCRIPT_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def MANUSCRIPT_DIR
  @MANUSCRIPT_DIR
end

#PROJECT_DIRObject (readonly)

Returns the value of attribute PROJECT_DIR.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def PROJECT_DIR
  @PROJECT_DIR
end

#thorObject (readonly)

Returns the value of attribute thor.



11
12
13
# File 'lib/carta/cli/compile.rb', line 11

def thor
  @thor
end

Instance Method Details

#cleanObject



46
47
48
# File 'lib/carta/cli/compile.rb', line 46

def clean
  thor.remove_dir "#{@PROJECT_DIR}/build"
end

#copy_filesObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/carta/cli/compile.rb', line 116

def copy_files
  assets = FileList.new("#{@FIGURE_DIR}/**/*.{#{@ASSET_FILES}}",
                        "#{@ASSET_DIR}/**/*.{#{@ASSET_FILES}}",
                        "#{@MANUSCRIPT_DIR}/cover.{#{@ASSET_FILES}}")

  epub_dest = assets.pathmap("%{^#{@ASSET_DIR},#{@BUILD_DIR}/epub/EPUB}p")
                    .pathmap("%{^#{@FIGURE_DIR},#{@BUILD_DIR}/epub/EPUB/figures}p")

  html_dest = assets.pathmap("%{^#{@ASSET_DIR},#{@BUILD_DIR}/html}p")
                    .pathmap("%{^#{@FIGURE_DIR},#{@BUILD_DIR}/html/figures}p")

  assets.each_with_index do |file, index|
    thor.copy_file file, epub_dest[index]
    thor.copy_file file, html_dest[index]
  end
end

#generate_epubObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/carta/cli/compile.rb', line 133

def generate_epub
  files = FileList.new("#{@BUILD_DIR}/epub/**/*.*")
  zip_path = files.pathmap("%{^#{@BUILD_DIR}/epub/,}p")
  zip = Zip::OutputStream.new("#{@BUILD_DIR}/#{book['title']}.epub")
  zip.put_next_entry('mimetype', nil, nil, Zip::Entry::STORED, Zlib::NO_COMPRESSION)
  zip.write "application/epub+zip"
  zip_list = {}
  zip_path.each_with_index do |value, i|
    zip_list[value] = files[i]
  end
  zip_list.keys.each do |key|
    zip.put_next_entry key, nil, nil, Zip::Entry::DEFLATED, Zlib::BEST_COMPRESSION
    zip.write IO.read(zip_list[key])
  end
  zip.close

end

#generate_epub_manifestObject



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
# File 'lib/carta/cli/compile.rb', line 89

def generate_epub_manifest
  files   = FileList.new("#{@LAYOUT_DIR}/epub/EPUB/*.erb")
                    .pathmap("%{^#{@LAYOUT_DIR}/epub/EPUB/,}X")
                    .exclude('**/*.opf*')
                    .exclude('content.xhtml', 'nav.xhtml')
                    .exclude('**/*.ncx*')
                    .add("#{@FIGURE_DIR}/**/*.{#{@ASSET_FILES}}",
                         "#{@ASSET_DIR}/**/*.{#{@ASSET_FILES}}",
                         "#{@MANUSCRIPT_DIR}/cover.{#{@ASSET_FILES}}")
                    .pathmap("%{^#{@ASSET_DIR}/,}p")
                    .pathmap("%{^#{@FIGURE_DIR},figures}p")

  book['manifest'] = []

  files.each do |file|
    media_type = MIME::Types.type_for(file).first.content_type
    if file.include? 'cover'
      book['cover'] = { filename: file, media_type: media_type }
    else
      book['manifest'] << { filename: file, media_type: media_type }
    end
  end
  copy_files
  render_epub_layouts
  generate_epub
end

#generate_htmlObject

Generates our HTML from markdown files and creates an outline



51
52
53
54
55
56
57
58
59
60
# File 'lib/carta/cli/compile.rb', line 51

def generate_html
  html_renderer = Carta::CLI::HTMLRenderer.new(@PROJECT_DIR)
  # puts @MANUSCRIPT_DIR
  book['html']      = html_renderer.manuscript_html
  book['outline']   = html_renderer.outline
  book['epub_toc_html']  = html_renderer.epub_toc_html
  book['html_toc_html']  = html_renderer.html_toc_html

  generate_epub_manifest
end

#render_epub_layoutsObject

Runs through our ERBs



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
# File 'lib/carta/cli/compile.rb', line 63

def render_epub_layouts
  FileList.new("#{@LAYOUT_DIR}/epub/**/*.erb").each do |layout|
    filename = layout.pathmap("%{^#{@LAYOUT_DIR},#{@BUILD_DIR}}X")
    path = filename.pathmap('%d')

    FileUtils.mkpath(path) unless File.exists? path

    template = ERB.new(File.read(layout), nil, '-')
    File.open(filename, 'w+') do |handle|
      handle.write template.result(binding)
    end
  end

  FileList.new("#{@LAYOUT_DIR}/html/**/*.erb").each do |layout|
    filename = layout.pathmap("%{^#{@LAYOUT_DIR},#{@BUILD_DIR}}X")
    path = filename.pathmap('%d')

    FileUtils.mkpath(path) unless File.exists? path

    template = ERB.new(File.read(layout), nil, '-')
    File.open(filename, 'w+') do |handle|
      handle.write template.result(binding)
    end
  end
end

#runObject



41
42
43
44
# File 'lib/carta/cli/compile.rb', line 41

def run
  clean
  generate_html if Dir.exists?('manuscript')
end