class JekyllAsciidoctorPdf::ADoc
Public Class Methods
fixCommonIssues(content, base_url)
click to toggle source
Search and replace expression to clean up the original file
Common Issues:
- Relative links: link:relative.html -> link:<BASE_URL>.html - Checkmark symbol: ✓ -> icon:check[] - Remove YAML front matter (we could use skip-front-matter attribute) Previous code subs = { YAML_FRONT_MATTER_REGEXP => '', RELATIVE_LINK_REGEXP => '\1link:' + base_url, CHECK_REGEXP => 'icon:check[]' } combined_regexp = Regexp.union(subs.keys) return content.gsub(combined_regexp, subs)
Previous code didn't work properly
TODO: We should try to perform mutilple regexp subs at once.
# File lib/jekyll_asciidoctor_pdf/adoc.rb, line 40 def self.fixCommonIssues(content, base_url) temp = content.gsub(YAML_FRONT_MATTER_REGEXP, '') temp = temp.gsub(RELATIVE_LINK_REGEXP, '\1link:' + base_url ) temp = temp.gsub(CHECK_REGEXP, ' icon:check[] ') return temp end
generateBookTypePdf(filename, filename_path, product_name, title, base_path, stream_adoc, parameters, cover_page, theme_pdf, revision, authors)
click to toggle source
We abstract the call to Asciidoctor to convert Stream => PDF
Arguments:
- filename: name of the output file (should include extension e.i. name.pdf) - filename_path: output path for the filename - title: title of the cover page - subtitle: subtitle of the cover page - base_path: this is the root path to execture the convert process (relative :imagedir attribute use that path) - stream_adoc: the Asciidoc file as string - parameters: configuration from YAML file - cover_page: absolute image for the cover page - theme_pdf: absolute YAML file with the theme configuration of Asciidoctor-pdf NOTE: * We set the mode to :unsafe, due to the error "image has illegal reference to ancestor of jail;" e.i :imagedirs: ../media * We allow to read external files with the attribute 'allow-uri-read' => '' e.i image:http://example.org/image.png (we may want to block this feature is too expensive)
# File lib/jekyll_asciidoctor_pdf/adoc.rb, line 116 def self.generateBookTypePdf(filename, filename_path, product_name, title, base_path, stream_adoc, parameters, cover_page, theme_pdf, revision, authors) Asciidoctor.convert( getFullsiteTemplate(title, product_name, stream_adoc, parameters, cover_page, theme_pdf, revision, authors), backend: 'pdf', doctype: 'book', to_file: filename, to_dir: filename_path, mkdirs: true, safe: :unsafe, attributes: { 'allow-uri-read' => '' }, base_dir: base_path ); end
generateCoverPage(title, subtitle, authors, revision, abstract, background_image, file)
click to toggle source
# File lib/jekyll_asciidoctor_pdf/adoc.rb, line 190 def self.generateCoverPage(title, subtitle, authors, revision, abstract, background_image, file) info = { :Title => title, :Author => authors, :Subject => subtitle, :Keywords => "", :Creator => "NetApp", :Producer => "NetApp", :CreationDate => Time.now } Prawn::Document.generate(file, :page_size => 'A4', :margin => 0, :info => info) do stroke_bounds image background_image, :position => :left, :vposition => :top, :fit => [680,3508] bounding_box([60, 700], :width => 500, :height => 650) do font "Times-Roman" #stroke_bounds #stroke_circle [0, 0], 10 move_down 120 text "Technical Report", size: 12, color: "4B4b4b" move_down 10 text title, size: 20, font: 'Times-Roman', color: "1667c5" move_down 5 text subtitle, size: 16, font: 'Times-Roman', color: "999999" move_down 10 text authors, size: 12, font: 'Times-Roman', color: "4B4b4b" text revision, size: 12, font: 'Times-Roman', color: "4B4b4b" #move_down 300 #text "Abstract", size: 14, font: "Times-Roman", color: "0000FF" #move_down 10 #font_size(12) { text abstract, font: "Times-Roman" } end end end
generatePdf(product_name, file, output_path, parameters, cover_page, theme_pdf, revision, authors, base_url) { |front_matter, stream_adoc| ... }
click to toggle source
Generate a PDF of an individual page
- Remove the jekyll frontmatter - Read the content - Execute regexp to fix issues - Execute the convert process - Let the caller to collect information from the front_matter and from the file itself
# File lib/jekyll_asciidoctor_pdf/adoc.rb, line 68 def self.generatePdf(product_name, file, output_path, parameters, cover_page, theme_pdf, revision, authors, base_url) front_matter = {} content = File.read(file) if content =~ YAML_FRONT_MATTER_REGEXP #content = $POSTMATCH front_matter = SafeYAML.load(Regexp.last_match(1)) end stream_adoc = fixCommonIssues(content, base_url) # We should think about the performance of get the title in that way adoc = Asciidoctor.load stream_adoc title = adoc.doctitle filename = File.basename(file,'.*') + '.pdf' filename_path = output_path base_path = File.dirname(file) generateBookTypePdf(filename, filename_path, product_name, title, base_path, stream_adoc, parameters, cover_page, theme_pdf, revision, authors) yield(front_matter, stream_adoc) if block_given? end
getAuthorsList(file)
click to toggle source
# File lib/jekyll_asciidoctor_pdf/adoc.rb, line 48 def self.getAuthorsList(file) names = %x[ git log --pretty=format:"%an" #{file} | sort | uniq ] # last_commit_date can be nil iff the file was not committed. if (names.nil? || names.empty?) return 'NetApp' end return names.split(/\n+/).join(', ') end
getCopyright()
click to toggle source
# File lib/jekyll_asciidoctor_pdf/adoc.rb, line 236 def self.getCopyright() text = <<~TEXT <<< *Copyright Information* Copyright © 2019–2020 NetApp, Inc. All rights reserved. Printed in the U.S. No part of this document covered by copyright may be reproduced in any form or by any means-graphic, electronic, or mechanical, including photocopying, recording, taping, or storage in an electronic retrieval system- without prior written permission of the copyright owner. Software derived from copyrighted NetApp material is subject to the following license and disclaimer: THIS SOFTWARE IS PROVIDED BY NETAPP “AS IS” AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL NETAPP BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. NetApp reserves the right to change any products described herein at any time, and without notice. NetApp assumes no responsibility or liability arising from the use of products described herein, except as expressly agreed to in writing by NetApp. The use or purchase of this product does not convey a license under any patent rights, trademark rights, or any other intellectual property rights of NetApp. The product described in this manual may be protected by one or more U.S. patents, foreign patents, or pending applications. RESTRICTED RIGHTS LEGEND: Use, duplication, or disclosure by the government is subject to restrictions as set forth in subparagraph (c)(1)(ii) of the Rights in Technical Data and Computer Software clause at DFARS 252.277-7103 (October 1988) and FAR 52-227-19 (June 1987). *Trademark Information* NETAPP, the NETAPP logo, and the marks listed at http://www.netapp.com/TM are trademarks of NetApp, Inc. Other company and product names may be trademarks of their respective owners. TEXT return text end
getFullsiteTemplate(title, product_name, fullsite, parameters, cover_page, theme_pdf, revision, authors)
click to toggle source
Basic Template for the fullsite pdf
TODO: Add more configuracion
# File lib/jekyll_asciidoctor_pdf/adoc.rb, line 136 def self.getFullsiteTemplate(title, product_name, fullsite, parameters, cover_page, theme_pdf, revision, authors) copyright = getCopyright() fullsite_config = parameters['fullsite'] if (authors.nil?) authors_s = fullsite_config['authors'] else authors_s = authors end if (revision.nil?) revision_s = DateTime.now.strftime('%m/%d/%Y') else revision_s = revision.strftime('%m/%d/%Y') end temp_pdf = File.join(File.dirname(cover_page), 'temp-cover.pdf') generateCoverPage(title, product_name, authors_s, revision_s, '', cover_page, temp_pdf) text = <<~TEXT = #{title} : #{product_name} #{authors_s} #{revision_s} :notitle: :doctype: book :experimental: :reproducible: :icons: font :listing-caption: Listing :imagesdir: ./media/ :toc: :toclevels: 2 ifeval::["{asciidoctor-version}" < "1.5.7"] :legacy-footnoteref: endif::[] ifdef::backend-pdf[] :pdf-theme: #{theme_pdf} :title-page: :front-cover-image: image:#{temp_pdf}[] :source-highlighter: rouge endif::[] :chapter-label: :leveloffset: +1 #{fullsite} :leveloffset: -1 #{copyright} TEXT return text end