Module: Softcover::Commands::Generator

Extended by:
Generator
Includes:
Output
Included in:
Generator
Defined in:
lib/softcover/commands/generator.rb

Instance Method Summary collapse

Methods included from Output

should_output?, silence!, silent?, #system, unsilence!

Instance Method Details

#all_files_and_directoriesObject

Returns a list of all the files and directories used to build the book.



90
91
92
# File 'lib/softcover/commands/generator.rb', line 90

def all_files_and_directories
  files_directories_maybe_markdown
end

#directoriesObject

Returns just the directories used for building the book.



109
110
111
# File 'lib/softcover/commands/generator.rb', line 109

def directories
  all_files_and_directories.select { |path| File.directory?(path) }
end

#filesObject

Returns just the files (not directories) used for building the book.



114
115
116
# File 'lib/softcover/commands/generator.rb', line 114

def files
  all_files_and_directories.reject { |path| File.directory?(path) }
end

#files_directories_maybe_markdownObject

Returns the files and directories based on the input format.



95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/softcover/commands/generator.rb', line 95

def files_directories_maybe_markdown
  fds = Dir.glob(
          File.join(Softcover::Utils.template_dir(article: @article),
                    "**/*"), File::FNM_DOTMATCH)
  if markdown?
    # Skip the PolyTeX chapter files, which will be generated later.
    fds.reject { |e| e =~ /\/chapters\/.*\.tex/ }
  else
    # Skip the Markdown files.
    fds.reject { |e| e =~ /chapters\/.*\.md/ }
  end
end

#generate_file_tree(name, options = {}) ⇒ Object

Generates the default book file tree.



9
10
11
12
13
14
15
16
17
18
19
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
# File 'lib/softcover/commands/generator.rb', line 9

def generate_file_tree(name, options = {})
  @name = name
  @markdown = !options[:polytex]
  @article = options[:article]
  template_dir = Softcover::Utils.template_dir(article: @article)

  thor = Thor::Shell::Basic.new

  puts "Generating directory: #{name}"

  overwrite_all = false

  FileUtils.mkdir name unless File.exist?(name)
  Dir.chdir name

  # Create the directories.
  # There was some trouble with MathJax where it was trying to copy a
  # file before the directory had been created, so we now create all
  # the directories first.
  directories.each do |path|
    (cp_path = path.dup).slice! template_dir + "/"
    unless File.exist?(cp_path)
      puts "Creating #{cp_path}" unless cp_path =~ /MathJax/
      FileUtils.mkdir cp_path
    end
  end

  # Create the files.
  files.each do |path|
    next if path =~ /\/.$|\/..$/

    (cp_path = path.dup).slice! template_dir + "/"

    if path =~ /book\.tex/
      cp_path = "#{name}.tex"
    elsif path =~ /\.erb/
      cp_path = File.join(File.dirname(cp_path),
                          File.basename(path.dup, '.erb'))
    elsif path =~ /gitignore/
      next if  markdown? && path =~ /latex_gitignore/
      next if !markdown? && path =~ /markdown_gitignore/
      cp_path = '.gitignore'
    end

    display_path = File.join name, cp_path

    if File.exist?(cp_path) && !overwrite_all
      res = thor.ask "#{display_path} already exists. " \
                     "Overwrite? (yes,no,all):"

      overwrite = case res
      when /y|yes/ then true
      when /n|no/  then false
      when /a|all/ then
        overwrite_all = true
        true
      end

      next unless overwrite
    end

    puts "Creating #{cp_path}" unless cp_path =~ /MathJax/
    if path =~ /\.erb/
      erb = ERB.new(File.read(path)).result(binding)
      File.open(cp_path, 'w') { |f| f.write erb }
    else
      FileUtils.cp path, cp_path
    end
  end

  # Symlink the images directory.
  Dir.chdir "html"
  FileUtils.rm_f("images") if File.directory?("images")
  File.symlink("../images", "images")

  Dir.chdir "../.."
  book_yml = File.join(Softcover::Directories::CONFIG, 'book.yml')
  puts "Done. Please update #{book_yml}"
end

#markdown?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'lib/softcover/commands/generator.rb', line 118

def markdown?
  @markdown
end

#verify!Object



122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/softcover/commands/generator.rb', line 122

def verify!
  generated_files = Dir.glob("**/*", File::FNM_DOTMATCH).map do |f|
    File.basename(f)
  end

  Softcover::Commands::Generator.all_files_and_directories.each do |file|
    path = if file =~ /book\.tex/
             "#{@name}.tex"
           elsif file =~ /\.erb/
             File.basename(file, '.erb')
           elsif file =~ /gitignore/
             '.gitignore'
           else
             File.basename(file)
           end
    raise "missing #{file}" unless generated_files.include?(path)
  end
end