Module: DocContract::SharedTemplateAndInstanceMethods

Included in:
ContractInstancesController, ContractTemplatesController
Defined in:
app/controllers/concerns/doc_contract/shared_template_and_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#pdfObject

rubocop:disable Metrics/MethodLength for now keep as a whole story



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
# File 'app/controllers/concerns/doc_contract/shared_template_and_instance_methods.rb', line 25

def pdf
  @record = record_class.find params[:id]
  authorize! :show, @record

  # Allow preview using the markdown parameter. Fallback to actual saved
  # version when param is not given
  @record.markdown = params[:markdown] if params[:markdown].present?
  return redirect_to @record unless @record.markdown.present?

  @tempfile_markdown = Tempfile.new(['pandoc-markdown', '.md'])
  config_addittions = {}
  if nbackground = @record.try(:titlepage_background) || @record.contract_template.titlepage_background.presence
    config_addittions['titlepage-background'] = "vendor/assets/doc-contract/titlepage-backgrounds/background#{nbackground}.pdf"
  end
  template_markdown = @record.template_markdown(config_addittions)
  @tempfile_markdown.write template_markdown
  #@tempfile_markdown.rewind
  @tempfile_markdown.close
  @tempfile_pdf = Tempfile.create(['pandoc-pdf-', '.pdf'])
  @tempfile_pdf.close
  command = %(
    pandoc --template=vendor/assets/doc-contract/pandoc-templates/eisvogel.tex
      --css=app/assets/stylesheets/doc_contract/pandoc.css #{@tempfile_markdown.path}
      -o #{@tempfile_pdf.path}
  ).strip.gsub(/\n\s*/, ' ')
  command << ' --variable top-level-division=chapter' if @record.config['book']
  #command << " --variable titlepage=true" if false # done through YAML config
  command << ' --listings' # if some conditions TODO: make this a database boolean
  command << ' --filter pandoc-latex-environment' if template_markdown['pandoc-latex-environment:']

  stdout, stderr, status = nil
  Dir.chdir DocContract::Engine.root do
    stdout, stderr, status = Open3.capture3(command)
  end

  if status.success?
    send_file @tempfile_pdf.path
  else
    redirect_back fallback_location: record_class, flash: {error: stderr}
  end
ensure
  begin
    @tempfile_markdown.unlink
    @tempfile_pdf.unlink
  rescue
    nil
  end
end

#processed_htmlObject



14
15
16
17
18
19
20
21
# File 'app/controllers/concerns/doc_contract/shared_template_and_instance_methods.rb', line 14

def processed_html
  @record = record_class.find params[:id]
  authorize! :show, @record
  # Allow preview using the markdown parameter. Fallback to actual saved
  # version when param is not given
  @record.markdown = params[:markdown] if params[:markdown].present?
  render json: {html: @record.processed_html}
end

#processed_markdownObject

POST /doc-contract/contract_templates/:id/processed_markdown?markdown=…



5
6
7
8
9
10
11
12
# File 'app/controllers/concerns/doc_contract/shared_template_and_instance_methods.rb', line 5

def processed_markdown
  @record = record_class.find params[:id]
  authorize! :show, @record
  # Allow preview using the markdown parameter. Fallback to actual saved
  # version when param is not given
  @record.markdown = params[:markdown] if params[:markdown].present?
  render json: {markdown: @record.processed_markdown}
end