Skip to content

Commit

Permalink
merge PR #605
Browse files Browse the repository at this point in the history
resolves #262 add asciidoctor/pdf as require alias; rename Pdf module to PDF; uppercase acronyms in constants
  • Loading branch information
mojavelinux authored Apr 23, 2019
2 parents 5e545bb + ad3848a commit 0f92c93
Show file tree
Hide file tree
Showing 44 changed files with 92 additions and 81 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/master[
== Unreleased

* drop support for Ruby < 2.3 (and installation will fail for Ruby < 2.1)
* add asciidoctor/pdf and asciidoctor/pdf/version require aliases (#262)
* rename module to Asciidoctor::PDF and define Asciidoctor::Pdf alias for backwards compatibility (#262)
* switch to tilde dependency versions (flexible patch number) instead of ranges
* upgrade prawn-svg to 0.29.1; resolves numerous SVG rendering issues (#886, #430)
* drop support for Rouge < 2
Expand Down
6 changes: 3 additions & 3 deletions asciidoctor-pdf.gemspec
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
begin
require_relative 'lib/asciidoctor-pdf/version'
require_relative 'lib/asciidoctor/pdf/version'
rescue LoadError
require 'asciidoctor-pdf/version'
require 'asciidoctor/pdf/version'
end

Gem::Specification.new do |s|
s.name = 'asciidoctor-pdf'
s.version = Asciidoctor::Pdf::VERSION
s.version = Asciidoctor::PDF::VERSION
s.summary = 'Converts AsciiDoc documents to PDF using Asciidoctor and Prawn'
s.description = 'An extension for Asciidoctor that converts AsciiDoc documents to PDF using the Prawn PDF library.'
s.authors = ['Dan Allen', 'Sarah White']
Expand Down
6 changes: 3 additions & 3 deletions bin/asciidoctor-pdf
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env ruby

if File.file?(asciidoctor_pdf = (File.expand_path '../../lib/asciidoctor-pdf.rb', __FILE__))
if File.file?(asciidoctor_pdf = (File.expand_path '../../lib/asciidoctor/pdf.rb', __FILE__))
require asciidoctor_pdf
else
require 'asciidoctor-pdf'
require 'asciidoctor/pdf'
end
require 'asciidoctor/cli'

options = Asciidoctor::Cli::Options.new backend: 'pdf', header_footer: true

# FIXME provide an API in Asciidoctor for sub-components to print version information
unless ARGV != ['-v'] && (ARGV & ['-V', '--version']).empty?
$stdout.write %(Asciidoctor PDF #{Asciidoctor::Pdf::VERSION} using )
$stdout.write %(Asciidoctor PDF #{Asciidoctor::PDF::VERSION} using )
# NOTE the print_version method was added in Asciidoctor 1.5.2
if options.respond_to? :print_version
options.print_version
Expand Down
19 changes: 8 additions & 11 deletions lib/asciidoctor-pdf/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
autoload :Tempfile, 'tempfile'

module Asciidoctor
module Pdf
module PDF
class Converter < ::Prawn::Document
include ::Asciidoctor::Converter
if defined? ::Asciidoctor::Logging
Expand Down Expand Up @@ -414,7 +414,7 @@ def build_pdf_options doc, theme
}
end

# FIXME PdfMarks should use the PDF info result
# FIXME Pdfmark should use the PDF info result
def build_pdf_info doc
info = {}
# FIXME use sanitize: :plain_text once available
Expand All @@ -431,7 +431,7 @@ def build_pdf_info doc
if (doc.attr? 'publisher')
info[:Producer] = (doc.attr 'publisher').as_pdf
end
info[:Creator] = %(Asciidoctor PDF #{::Asciidoctor::Pdf::VERSION}, based on Prawn #{::Prawn::VERSION}).as_pdf
info[:Creator] = %(Asciidoctor PDF #{::Asciidoctor::PDF::VERSION}, based on Prawn #{::Prawn::VERSION}).as_pdf
info[:Producer] ||= (info[:Author] || info[:Creator])
unless doc.attr? 'reproducible'
# NOTE since we don't track the creation date of the input file, we map the ModDate header to the last modified
Expand Down Expand Up @@ -660,7 +660,7 @@ def convert_admonition node
elsif icons
if icon_path.end_with? '.svg'
begin
svg_obj = ::Prawn::Svg::Interface.new ::File.read(icon_path), self,
svg_obj = ::Prawn::SVG::Interface.new ::File.read(icon_path), self,
position: label_align,
vposition: label_valign,
width: label_width,
Expand Down Expand Up @@ -1210,7 +1210,7 @@ def convert_image node, opts = {}
svg_data = ::File.read image_path
file_request_root = ::File.dirname image_path
end
svg_obj = ::Prawn::Svg::Interface.new svg_data, self,
svg_obj = ::Prawn::SVG::Interface.new svg_data, self,
position: alignment,
width: width,
fallback_font_name: default_svg_font,
Expand Down Expand Up @@ -1465,11 +1465,7 @@ def convert_listing_or_literal node
conum_mapping ? (restore_conums fragments, conum_mapping) : fragments
else
# NOTE only format if we detect a need (callouts or inline formatting)
if XmlMarkupRx.match? source_string
text_formatter.format source_string
else
[{ text: source_string }]
end
(XMLMarkupRx.match? source_string) ? (text_formatter.format source_string) : [{ text: source_string }]
end

node.subs.replace prev_subs if prev_subs
Expand Down Expand Up @@ -2912,7 +2908,7 @@ def layout_running_content periphery, doc, opts = {}
begin
if (img_path = content[:path]).downcase.end_with? '.svg'
svg_data = ::File.read img_path
svg_obj = ::Prawn::Svg::Interface.new svg_data, self,
svg_obj = ::Prawn::SVG::Interface.new svg_data, self,
position: colspec[:align],
vposition: trim_img_valign,
width: content[:width],
Expand Down Expand Up @@ -3566,4 +3562,5 @@ def assign_missing_section_ids doc
=end
end
end
Pdf = PDF unless const_defined? :Pdf, false
end
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/formatted_text/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Asciidoctor
module Pdf
module PDF
module FormattedText
class Formatter
if defined? ::Asciidoctor::Logging
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Asciidoctor::Pdf::FormattedText
module Asciidoctor::PDF::FormattedText
module InlineDestinationMarker
module_function

Expand Down
8 changes: 4 additions & 4 deletions lib/asciidoctor-pdf/formatted_text/inline_image_arranger.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Asciidoctor::Pdf::FormattedText
module Asciidoctor::PDF::FormattedText
module InlineImageArranger
include ::Asciidoctor::Pdf::Measurements
include ::Asciidoctor::PDF::Measurements
if defined? ::Asciidoctor::Logging
include ::Asciidoctor::Logging
else
Expand All @@ -14,7 +14,7 @@ module InlineImageArranger
rescue
PlaceholderWidthCache = {}
end
TemporaryPath = ::Asciidoctor::Pdf::TemporaryPath
TemporaryPath = ::Asciidoctor::PDF::TemporaryPath

def wrap fragments
arrange_images fragments
Expand Down Expand Up @@ -63,7 +63,7 @@ def arrange_images fragments

# TODO make helper method to calculate width and height of image
if fragment[:image_format] == 'svg'
svg_obj = ::Prawn::Svg::Interface.new ::File.read(image_path), doc,
svg_obj = ::Prawn::SVG::Interface.new ::File.read(image_path), doc,
at: doc.bounds.top_left,
width: image_w,
fallback_font_name: doc.default_svg_font
Expand Down
4 changes: 2 additions & 2 deletions lib/asciidoctor-pdf/formatted_text/inline_image_renderer.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Asciidoctor::Pdf::FormattedText
module Asciidoctor::PDF::FormattedText
module InlineImageRenderer
TemporaryPath = ::Asciidoctor::Pdf::TemporaryPath
TemporaryPath = ::Asciidoctor::PDF::TemporaryPath
module_function

# Embeds the image object in this fragment into the document in place of the
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/formatted_text/inline_text_aligner.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Asciidoctor::Pdf::FormattedText
module Asciidoctor::PDF::FormattedText
module InlineTextAligner
module_function

Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/formatted_text/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@


module Asciidoctor
module Pdf
module PDF
module FormattedText
module Markup
include Treetop::Runtime
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/formatted_text/parser.treetop
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# regenerate parser.rb using `tt parser.treetop`
module Asciidoctor
module Pdf
module PDF
module FormattedText
grammar Markup
rule text
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/formatted_text/transform.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Asciidoctor
module Pdf
module PDF
module FormattedText
class Transform
LF = ?\n
Expand Down
4 changes: 2 additions & 2 deletions lib/asciidoctor-pdf/implicit_header_processor.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'asciidoctor/extensions'

module Asciidoctor
module Pdf
module PDF
# An include processor that skips the implicit author line below
# the document title within include documents.
class ImplicitHeaderProcessor < ::Asciidoctor::Extensions::IncludeProcessor
Expand Down Expand Up @@ -59,5 +59,5 @@ def update_config config
end

Asciidoctor::Extensions.register :pdf do
include_processor Asciidoctor::Pdf::ImplicitHeaderProcessor if @document.backend == 'pdf'
include_processor Asciidoctor::PDF::ImplicitHeaderProcessor if @document.backend == 'pdf'
end
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/index_catalog.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Asciidoctor; module Pdf
module Asciidoctor; module PDF
class IndexCatalog
LeadingAlphaRx = /^\p{Alpha}/

Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/measurements.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Asciidoctor; module Pdf
module Asciidoctor; module PDF
module Measurements
MeasurementValueRx = /(\d+|\d*\.\d+)(in|mm|cm|p[txc])?$/
InsetMeasurementValueRx = /(?<=^| |\()(-?\d+(?:\.\d+)?)(in|mm|cm|p[txc])(?=$| |\))/
Expand Down
6 changes: 3 additions & 3 deletions lib/asciidoctor-pdf/pdfmark.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Asciidoctor
module Pdf
module PDF
class Pdfmark
include ::Asciidoctor::Pdf::Sanitizer
include ::Asciidoctor::PDF::Sanitizer

def initialize doc
@doc = doc
Expand All @@ -17,7 +17,7 @@ def generate
/Keywords #{(doc.attr 'keywords').to_pdf}
/ModDate #{date = ::Time.now.to_pdf}
/CreationDate #{date}
/Creator (Asciidoctor PDF #{::Asciidoctor::Pdf::VERSION}, based on Prawn #{::Prawn::VERSION})
/Creator (Asciidoctor PDF #{::Asciidoctor::PDF::VERSION}, based on Prawn #{::Prawn::VERSION})
/Producer #{(doc.attr 'publisher').to_pdf}
/DOCINFO pdfmark
EOS
Expand Down
4 changes: 2 additions & 2 deletions lib/asciidoctor-pdf/prawn-svg_ext.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'prawn-svg' unless defined? Prawn::Svg::VERSION
require 'prawn-svg' unless defined? Prawn::SVG::Interface
require_relative 'prawn-svg_ext/interface'
# NOTE disable system fonts since they're non-portable
Prawn::Svg::Interface.font_path.clear
Prawn::SVG::Interface.font_path.clear
4 changes: 2 additions & 2 deletions lib/asciidoctor-pdf/prawn-svg_ext/interface.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Prawn; module Svg
module Prawn; module SVG
class Interface
def resize opts = {}
sizing = document.sizing
Expand All @@ -7,4 +7,4 @@ def resize opts = {}
sizing.calculate
end
end
end; end unless Prawn::Svg::Interface.method_defined? :resize
end; end unless Prawn::SVG::Interface.method_defined? :resize
4 changes: 2 additions & 2 deletions lib/asciidoctor-pdf/prawn_ext/extensions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
module Asciidoctor
module Prawn
module Extensions
include ::Asciidoctor::Pdf::Measurements
include ::Asciidoctor::Pdf::Sanitizer
include ::Asciidoctor::PDF::Measurements
include ::Asciidoctor::PDF::Sanitizer

FontAwesomeIconSets = %w(fab far fas)
IconSets = %w(fab far fas fi pf).to_set
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/prawn_ext/images.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def image file, opts = {}
# intrinsic width and height values (in pixels)
def intrinsic_image_dimensions path
if path.end_with? '.svg'
img_obj = ::Prawn::Svg::Interface.new ::File.read(path), self, {}
img_obj = ::Prawn::SVG::Interface.new ::File.read(path), self, {}
img_size = img_obj.document.sizing
{ width: img_size.output_width, height: img_size.output_height }
else
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/roman_numeral.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
########################################################################

module Asciidoctor
module Pdf
module PDF
class RomanNumeral
BaseDigits = {
1 => 'I',
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/rouge_ext/formatters/prawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class Prawn < Formatter
def initialize opts = {}
unless ::Rouge::Theme === (theme = opts[:theme])
unless theme && (theme = ::Rouge::Theme.find theme)
theme = ::Rouge::Themes::AsciidoctorPdfDefault
theme = ::Rouge::Themes::AsciidoctorPDFDefault
end
theme = theme.new
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Rouge
module Themes
# A variation on the pastie style from Pygments, customized for Asciidoctor PDF
# See https://bitbucket.org/birkenfeld/pygments-main/src/default/pygments/styles/pastie.py
class AsciidoctorPdfDefault < CSSTheme
class AsciidoctorPDFDefault < CSSTheme
name 'asciidoctor_pdf_default'

# Deviate from pastie here since our italic is actually a thinner font
Expand Down
24 changes: 12 additions & 12 deletions lib/asciidoctor-pdf/sanitizer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@
end

module Asciidoctor
module Pdf
module PDF
module Sanitizer
XmlSpecialChars = {
XMLSpecialChars = {
'&lt;' => ?<,
'&gt;' => ?>,
'&amp;' => ?&,
}
XmlSpecialCharsRx = /(?:#{XmlSpecialChars.keys * ?|})/
InverseXmlSpecialChars = XmlSpecialChars.invert
InverseXmlSpecialCharsRx = /[#{InverseXmlSpecialChars.keys.join}]/
XMLSpecialCharsRx = /(?:#{XMLSpecialChars.keys * ?|})/
InverseXMLSpecialChars = XMLSpecialChars.invert
InverseXMLSpecialCharsRx = /[#{InverseXMLSpecialChars.keys.join}]/
(BuiltInNamedEntities = {
'amp' => ?&,
'apos' => ?',
Expand All @@ -27,10 +27,10 @@ module Sanitizer
'nbsp' => ' ',
'quot' => ?",
}).default = ??
XmlSanitizeRx = /<[^>]+>/
XmlMarkupRx = /&#?[a-z\d]+;|</
SanitizeXMLRx = /<[^>]+>/
XMLMarkupRx = /&#?[a-z\d]+;|</
CharRefRx = /&(?:([a-z][a-z]+\d{0,2})|#(?:(\d\d\d{0,4})|x([a-f\d][a-f\d][a-f\d]{0,3})));/
SiftPcdataRx = /(&#?[a-z\d]+;|<[^>]+>)|([^&<]+)/
SiftPCDATARx = /(&#?[a-z\d]+;|<[^>]+>)|([^&<]+)/

# Strip leading, trailing and repeating whitespace, remove XML tags and
# resolve all entities in the specified string.
Expand All @@ -39,22 +39,22 @@ module Sanitizer
# FIXME add option to control escaping entities, or a filter mechanism in general
def sanitize string
string.strip
.gsub(XmlSanitizeRx, '')
.gsub(SanitizeXMLRx, '')
.tr_s(' ', ' ')
.gsub(CharRefRx) { $1 ? BuiltInNamedEntities[$1] : [$2 ? $2.to_i : ($3.to_i 16)].pack('U1') }
end

def escape_xml string
string.gsub InverseXmlSpecialCharsRx, InverseXmlSpecialChars
string.gsub InverseXMLSpecialCharsRx, InverseXMLSpecialChars
end

def encode_quotes string
(string.include? ?") ? (string.gsub ?", '&quot;') : string
end

def uppercase_pcdata string
if XmlMarkupRx.match? string
string.gsub(SiftPcdataRx) { $2 ? (uppercase_mb $2) : $1 }
if XMLMarkupRx.match? string
string.gsub(SiftPCDATARx) { $2 ? (uppercase_mb $2) : $1 }
else
uppercase_mb string
end
Expand Down
2 changes: 1 addition & 1 deletion lib/asciidoctor-pdf/temporary_path.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Asciidoctor
module Pdf
module PDF
module TemporaryPath
def unlink
::File.unlink self
Expand Down
4 changes: 2 additions & 2 deletions lib/asciidoctor-pdf/theme_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
require_relative 'measurements'

module Asciidoctor
module Pdf
module PDF
class ThemeLoader
include ::Asciidoctor::Pdf::Measurements
include ::Asciidoctor::PDF::Measurements
if defined? ::Asciidoctor::Logging
include ::Asciidoctor::Logging
else
Expand Down
Loading

0 comments on commit 0f92c93

Please sign in to comment.