summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.adoc1
-rw-r--r--lib/asciidoctor/pdf/optimizer.rb9
-rw-r--r--spec/fixtures/chronicles-abbreviated.adoc48
-rw-r--r--spec/optimizer_spec.rb9
4 files changed, 65 insertions, 2 deletions
diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc
index 80b8c28c..3a754d3d 100644
--- a/CHANGELOG.adoc
+++ b/CHANGELOG.adoc
@@ -10,6 +10,7 @@ For a detailed view of what has changed, refer to the {url-repo}/commits/main[co
Bug Fixes::
* fix crash if last fragment in TOC entry is not rendered (#2354)
+* pass `-dNEWPDF=false` to Ghostscript when optimizing PDF so it does not break internal links (#2355)
== 2.3.2 (2022-09-17) - @mojavelinux
diff --git a/lib/asciidoctor/pdf/optimizer.rb b/lib/asciidoctor/pdf/optimizer.rb
index c979a5e4..7b126fc8 100644
--- a/lib/asciidoctor/pdf/optimizer.rb
+++ b/lib/asciidoctor/pdf/optimizer.rb
@@ -4,6 +4,7 @@ require 'pathname'
require 'rghost'
require 'rghost/gs_alone'
require 'tmpdir'
+autoload :Open3, 'open3'
RGhost::GSAlone.prepend (Module.new do
def initialize params, debug
@@ -15,7 +16,11 @@ RGhost::GSAlone.prepend (Module.new do
RGhost::Config.config_platform unless File.exist? RGhost::Config::GS[:path].to_s
(cmd = @params.slice 1, @params.length).unshift RGhost::Config::GS[:path].to_s
#puts cmd if @debug
- system(*cmd)
+ _out, err, status = Open3.capture3(*cmd)
+ unless (lines = err.lines.each_with_object([]) {|l, accum| (l.include? '-dNEWPDF=') ? accum.pop : (accum << l) }).empty?
+ $stderr.write(*lines)
+ end
+ status.success?
end
end)
@@ -59,7 +64,7 @@ module Asciidoctor
else
inputs = target
end
- d = { Printed: false, CannotEmbedFontPolicy: '/Warning', CompatibilityLevel: @compatibility_level }
+ d = { CannotEmbedFontPolicy: '/Warning', CompatibilityLevel: @compatibility_level, NEWPDF: false, Printed: false }
case @compliance
when 'PDF/A', 'PDF/A-1', 'PDF/A-2', 'PDF/A-3'
d[:PDFA] = ((@compliance.split '-', 2)[1] || 1).to_i
diff --git a/spec/fixtures/chronicles-abbreviated.adoc b/spec/fixtures/chronicles-abbreviated.adoc
new file mode 100644
index 00000000..e0e07c0d
--- /dev/null
+++ b/spec/fixtures/chronicles-abbreviated.adoc
@@ -0,0 +1,48 @@
+= Chronicles Abbreviated
+:doctype: book
+:preface-title: Preface
+:icons: font
+:sectnums:
+:toc:
+:toclevels: 3
+:url-devoxx: https://devoxx.be
+:url-wolpertinger: http://en.wikipedia.org/wiki/Wolpertinger
+
+== It's a City Under Siege
+
+(((Conference,Devoxx)))
+This journey begins one late Monday afternoon at {url-devoxx}[((Devoxx))].
+
+(((Wolpertinger)))
+(((Ravenous Beast,Wolpertinger)))
+You may not be familiar with these {url-wolpertinger}[ravenous beasts].
+Trust us, they'll eat your shorts and suck loops from your code.
+In light of this danger, we've searched high and wide for the security crew's defensive operations manual.
+We can't find it and the DefOpsfootnote:defops[DefOps is a portmanteau of "`defensive`" and "`operations`".] werewolves haven't returned from their rendezvous at Bier Central.
+They've either eaten each other or fallen victim to the Wolpertingers roaming the streets of ((Antwerp)).
+
+=== Rendezvous Point
+
+Come on, [[bier-central,Bier Central]]_Bier Central_, of course!
+
+== The Ravages of Writing
+
+Crystalline XML tags relentlessly bombarded the theater.
+
+=== A Recipe for Potion
+
+==== Searching for Burdockian
+
+===== Are You Still There?
+
+====== Yo
+
+== Dawn on the Plateau
+
+Lazarus was hanging from the bottom limb of a Burdockian tree, licking the bark.
+
+== Words Seasoned with Power
+
+=== Can I Get Some Code?
+
+Let's get our #((highlighting))# on!
diff --git a/spec/optimizer_spec.rb b/spec/optimizer_spec.rb
index c317cbd9..9cebe6cf 100644
--- a/spec/optimizer_spec.rb
+++ b/spec/optimizer_spec.rb
@@ -26,6 +26,15 @@ describe 'Asciidoctor::PDF::Optimizer', if: (RSpec::ExampleGroupHelpers.gem_avai
(expect optimizer.compliance).to eql 'PDF'
end
+ it 'should not mangle internal links when optimizing PDF' do
+ input_file = Pathname.new fixture_file 'chronicles-abbreviated.adoc'
+ to_optimized_file = to_pdf_file input_file, 'chronicles-abbreviated.pdf', attribute_overrides: { 'optimize' => '' }
+ pdf = PDF::Reader.new to_optimized_file
+ toc_annotations = get_annotations pdf, 2
+ toc_annotations_with_dest = toc_annotations.select {|it| it[:Dest] }
+ (expect toc_annotations_with_dest).to have_size toc_annotations.size
+ end
+
it 'should generate optimized PDF when filename contains spaces' do
input_file = Pathname.new example_file 'basic-example.adoc'
to_file = to_pdf_file input_file, 'optimizer filename with spaces.pdf', attribute_overrides: { 'optimize' => '' }