Class: Plans::Publish

Inherits:
Command show all
Defined in:
lib/plans/publish.rb

Instance Attribute Summary

Attributes inherited from Command

#options, #shell

Instance Method Summary collapse

Methods inherited from Command

#check_plans_pathname_exists, #initialize, #pathname, #plans_pathname, #raise_error, source_root

Constructor Details

This class inherits a constructor from Plans::Command

Instance Method Details

#check_pandoc_return(pandoc_return) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/plans/publish.rb', line 48

def check_pandoc_return(pandoc_return)
  unless pandoc_return == 0
    say "Problem publishing #{doc_type}. (Pandoc ERR: #{pandoc_return})", :red
    say "  #{source_file}"
    raise_error("Pandoc ERR: #{pandoc_return}")
  end
end

#check_reference_docx(reference_docx) ⇒ Object



56
57
58
59
60
61
62
63
# File 'lib/plans/publish.rb', line 56

def check_reference_docx(reference_docx)
  unless reference_docx.exist?
    say 'Could not find the reference.docx for this type of file in the .plans directory.', :red
    say 'Check to make sure that reference.docx is available here:'
    say "  #{reference_docx}"
    raise_error('Reference.docx not found.')
  end
end

#check_source_file(source_file) ⇒ Object



65
66
67
68
69
70
71
72
# File 'lib/plans/publish.rb', line 65

def check_source_file(source_file)
  unless File.exist? source_file
    say 'Markdown file to be published does not exist.', :red
    say "  #{source_file}"
    say 'Are you in the right directory?'
    raise_error('README.md not found.')
  end
end

#do(path) ⇒ Object



5
6
7
8
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
# File 'lib/plans/publish.rb', line 5

def do(path)
  # Create the thumbnails first.
  say 'Updating thumbnails.'
  Thumbs.new(shell, options).do(path)

  path = pathname(path)
  toc_flag = options[:toc] ? "--toc" : ""
  open_flag = options[:open]

  plans_path = plans_pathname(options[:'plans-path'])
  check_plans_pathname_exists(plans_path)

  source_file = path + 'README.md'
  check_source_file(source_file)

  doc_type = get_doctype(path)

  # Set the path to the reference doc for this type of document.
  reference_docx = plans_path + doc_type + 'reference.docx'
  check_reference_docx(reference_docx)

  output_dir = path + 'publish'
  # Make the publish directory if it does not exist.
  FileUtils.mkdir output_dir unless Dir.exist? output_dir
  output_file = output_dir + "#{doc_type}.docx"

  # We need to set the current working directory to where the markdown file is so
  # the images render correctly.
  # Pandoc only looks in the current working directory.
  Dir.chdir(path) do
    `pandoc #{toc_flag} #{source_file} --reference-doc=#{reference_docx} --standalone -f markdown+backtick_code_blocks -t docx -o #{output_file}`
  end

  # Get the return code from pandoc.
  pandoc_return = $?.to_i
  check_pandoc_return(pandoc_return)

  say "#{doc_type} published.", :green

  # Open the word doc if requested
  `open #{output_file}` if open_flag
end

#get_doctype(path) ⇒ String

Get the document type from the YAML file next to the document.

Parameters:

  • The (Pathname)

    path to the location of the template.yml file.

Returns:

  • (String)

    the type of the document found in the YAML file.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/plans/publish.rb', line 78

def get_doctype(path)
  doc_type = nil
  begin
     = YAML.load_file(path + 'template.yml')
    doc_type = ['type']
    if doc_type.nil?
      say 'Type value not found. Check template.yml in the document directory', :red
      say 'Make sure there is an entry `type: DOC_TYPE` in the file.'
      say "  #{path}"
      raise_error('DOC_TYPE not found in template.yml')
    end
  rescue Errno::ENOENT # File not found
    say 'No template.yml found in the document directory. Did you forget to add it?', :red
    say 'Did you run the command in the directory where the document is located?'
    say "  #{path}"
    raise_error('template.yml not found')
  end
  return doc_type
end