Class: OrigenDocHelpers::PDF
- Inherits:
-
Object
- Object
- OrigenDocHelpers::PDF
- Includes:
- Origen::Callbacks
- Defined in:
- lib/origen_doc_helpers/pdf.rb
Constant Summary collapse
- WKHTMLTOPDF =
'/run/pkg/wkhtmltopdf-/0.12.1/bin/wkhtmltopdf'
Instance Attribute Summary collapse
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#index ⇒ Object
readonly
Returns the value of attribute index.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#title ⇒ Object
readonly
Returns the value of attribute title.
Class Method Summary collapse
- .enabled? ⇒ Boolean
-
.register(options) ⇒ Object
This is called by the searchable doc layout template.
Instance Method Summary collapse
-
#before_deploy_site ⇒ Object
This is a callback handler to have the PDF creation invoked prior to deploying a pre-built website.
- #create ⇒ Object
- #create_topic_pages ⇒ Object
- #delete_topic_pages ⇒ Object
- #generate_pdf ⇒ Object
-
#initialize(options) ⇒ PDF
constructor
A new instance of PDF.
- #pdf_output_dir ⇒ Object
- #topic_dirs ⇒ Object
- #topic_wrapper_string ⇒ Object
- #version ⇒ Object
Constructor Details
#initialize(options) ⇒ PDF
Returns a new instance of PDF.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/origen_doc_helpers/pdf.rb', line 23 def initialize() @title = [:pdf_title] @index = [:index] @root = Pathname.new([:root]) if @root.absolute? if File.exist?("#{Origen.root}/app/") @root = @root.relative_path_from(Pathname.new("#{Origen.root}/app/templates/web")) else @root = @root.relative_path_from(Pathname.new("#{Origen.root}/templates/web")) end end require 'nokogiri' end |
Instance Attribute Details
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
5 6 7 |
# File 'lib/origen_doc_helpers/pdf.rb', line 5 def filename @filename end |
#index ⇒ Object (readonly)
Returns the value of attribute index.
5 6 7 |
# File 'lib/origen_doc_helpers/pdf.rb', line 5 def index @index end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
5 6 7 |
# File 'lib/origen_doc_helpers/pdf.rb', line 5 def root @root end |
#title ⇒ Object (readonly)
Returns the value of attribute title.
5 6 7 |
# File 'lib/origen_doc_helpers/pdf.rb', line 5 def title @title end |
Class Method Details
.enabled? ⇒ Boolean
19 20 21 |
# File 'lib/origen_doc_helpers/pdf.rb', line 19 def self.enabled? Origen.running_on_linux? && File.exist?(WKHTMLTOPDF) end |
.register(options) ⇒ Object
This is called by the searchable doc layout template
10 11 12 13 14 15 16 17 |
# File 'lib/origen_doc_helpers/pdf.rb', line 10 def self.register() if [:pdf_title] && enabled? @doc_pdfs ||= {} name = [:pdf_title].to_s.symbolize @doc_pdfs[name] ||= new() @doc_pdfs[name].filename end end |
Instance Method Details
#before_deploy_site ⇒ Object
This is a callback handler to have the PDF creation invoked prior to deploying a pre-built website
39 40 41 |
# File 'lib/origen_doc_helpers/pdf.rb', line 39 def before_deploy_site create end |
#create ⇒ Object
47 48 49 50 51 52 53 54 |
# File 'lib/origen_doc_helpers/pdf.rb', line 47 def create puts '' puts "Generating PDF: #{filename}..." puts '' create_topic_pages generate_pdf delete_topic_pages end |
#create_topic_pages ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/origen_doc_helpers/pdf.rb', line 89 def create_topic_pages topic_dirs do |topic, pages, topic_dir| topic_page = "#{topic_dir}/topic_page.html" page_str = "<h1>#{topic}</h1>\n" pages.each do |page_path, _page_heading| page_file = page_path.to_s.split('_').last page_file = "#{topic_dir}/#{page_file}/index.html" doc = Nokogiri::HTML(File.read(page_file)) page_str += doc.xpath('//article').to_html page_str += "\n" end File.open(topic_page, 'w') do |file| file.puts topic_wrapper_string.sub('SUB_TOPIC_CONTENT_HERE', page_str) end end end |
#delete_topic_pages ⇒ Object
106 107 108 109 110 |
# File 'lib/origen_doc_helpers/pdf.rb', line 106 def delete_topic_pages topic_dirs do |_topic, _pages, topic_dir| FileUtils.rm_f("#{topic_dir}/topic_page.html") end end |
#generate_pdf ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/origen_doc_helpers/pdf.rb', line 60 def generate_pdf cmd = "#{WKHTMLTOPDF} --print-media-type " cmd += "--footer-line --footer-font-size 8 --footer-left 'Freescale Internal Use Only' --footer-right [page] " cmd += "--header-left '#{title} (#{version})' --header-line --header-font-size 8 " index.each do |topic, pages| dir = "#{Origen.root}/web/output/#{root}" if topic topic_dir = pages.keys.first.to_s.split('_').first file = "#{dir}/#{topic_dir}/topic_page.html" cmd += "#{file} " else pages.each do |page_path, _page_heading| file = "#{dir}/#{page_path}/index.html" cmd += "#{file} " end end end cmd += "#{pdf_output_dir}/#{filename}" system cmd end |
#pdf_output_dir ⇒ Object
81 82 83 84 85 86 87 |
# File 'lib/origen_doc_helpers/pdf.rb', line 81 def pdf_output_dir @pdf_output_dir ||= begin dir = "#{Origen.root}/web/output/doc_helpers/pdfs" FileUtils.mkdir_p(dir) unless File.exist?(dir) dir end end |
#topic_dirs ⇒ Object
112 113 114 115 116 117 118 119 120 |
# File 'lib/origen_doc_helpers/pdf.rb', line 112 def topic_dirs index.each do |topic, pages| if topic topic_dir = pages.keys.first.to_s.split('_').first topic_dir = "#{Origen.root}/web/output/#{root}/#{topic_dir}" yield topic, pages, topic_dir end end end |
#topic_wrapper_string ⇒ Object
122 123 124 125 126 127 128 |
# File 'lib/origen_doc_helpers/pdf.rb', line 122 def topic_wrapper_string if File.exist?("#{Origen.root}/app/") @topic_wrapper_string ||= File.read("#{Origen.root!}/app/templates/pdf/topic_wrapper.html") else @topic_wrapper_string ||= File.read("#{Origen.root!}/templates/pdf/topic_wrapper.html") end end |