Skip to contentSkip to conversation

docs-parse-support-md: Prepare for copying with pandoc versions

PATCH In review Ian Jackson 3 participants 4 messages Jun 9, 2020

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 docs-parse-support-md: Prepare for copying with pandoc versions

Different pandoc versions generate, and expect, a different toplevel structure for their json output and inpout. Newer pandoc's toplevel is a hash. We are going to want to support this. We can tell what kind of output we should produce by looking at the input we got (which itself came from pandoc). So:

* Make space for code to read toplevel objects which are not arrays. Currently this code is absent and we just die explicitly (rather than dying because we tried to use a hashref as an array ref).

* Move generation of the toplevel json structure out of pandoc2html_inline, and abstract it away through a subref which is set up when we read the input file.

This is just prep work. No functional change other than a change to an error message.

---
 docs/parse-support-md | 28 +++++++++++++++++++++-------
 1 file changed, 21 insertions(+), 7 deletions(-)
docs/parse-support-md +21 -7
index 84f0a96a0f..b9978bfb4d 100755
@@ -232,6 +232,8 @@ sub r_content ($) {
     }
 }
 
+our $pandoc_toplevel_constructor;
+
 sub r_toplevel ($) {
     my ($i) = @_;
 
@@ -241,9 +243,21 @@ sub r_toplevel ($) {
     $had_unknown = undef;
     $had_feature = undef;
 
-    foreach my $e (@$i) {
-        next unless ref $e eq 'ARRAY';
-        r_content $e;
+    my $blocks;
+    if (ref $i eq 'ARRAY') {
+	$pandoc_toplevel_constructor = sub {
+	    my ($blocks) = @_;
+	    return [
+		    { unMeta => { } },
+		    $blocks,
+		   ];
+	};
+	foreach my $e (@$i) {
+	    next unless ref $e eq 'ARRAY';
+	    r_content $e;
+	}
+    } else {
+	die;
     }
 }
 
@@ -274,10 +288,10 @@ sub pandoc2html_inline ($) {
     my ($content) = @_;
 
     my $json_fh = IO::File::new_tmpfile or die $!;
-    my $j = to_json([
-                     { unMeta => { } },
-                     [{ t => 'Para', c => $content }],
-                    ]) or die $!;
+
+    my $blocks = [{ t => 'Para', c => $content }];
+    my $data = $pandoc_toplevel_constructor->($blocks);
+    my $j = to_json($data) or die $!;
     print $json_fh $j;
     flush $json_fh or die $!;
     seek $json_fh,0,0 or die $!;
Signature · 1 line
2.20.1
#1 lore raw

PATCH 2/2 docs-parse-support-md: Cope with buster's pandoc

Provide the implementation for newer pandoc json.

I have done an adhoc test and this now works on both buster and stretch and seems to produce the expected support matrix when run using the example rune (which processes unstable and 4.11).

---
 docs/parse-support-md | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
docs/parse-support-md +20 -0
index b9978bfb4d..a397637639 100755
@@ -243,6 +243,15 @@ sub r_toplevel ($) {
     $had_unknown = undef;
     $had_feature = undef;
 
+    # Pandoc's JSON output changed some time between 1.17.2 (stretch)
+    # and 2.2.1 (buster).  I can find no documentation about this
+    # change or about the compatibility rules.  (It seems that
+    # processing the parse tree *is* supported upstream: they offer
+    # many libraries to do this inside the pandoc process.)
+    # Empirically, what has changed is just the top level structure.
+    # Also pandoc wants the same structure back that it spat out,
+    # when we ask it to format snippets.
+
     my $blocks;
     if (ref $i eq 'ARRAY') {
 	$pandoc_toplevel_constructor = sub {
@@ -256,6 +265,17 @@ sub r_toplevel ($) {
 	    next unless ref $e eq 'ARRAY';
 	    r_content $e;
 	}
+    } elsif (ref $i eq 'HASH') {
+	my $api_version = $i->{'pandoc-api-version'};
+	$pandoc_toplevel_constructor = sub {
+	    my ($blocks) = @_;
+	    return {
+		    blocks => $blocks,
+		    meta => { },
+		    'pandoc-api-version' => $api_version,
+		   };
+	};
+	r_content $i->{blocks};
     } else {
 	die;
     }
Signature · 1 line
2.20.1
#2 lore raw
In reply to Ian Jackson

Ian Jackson writes ("[XEN PATCH for-4.14 1/2] docs-parse-support-md: Prepare for copying with pandoc versions"):

Quoted 5 lines

Different pandoc versions generate, and expect, a different toplevel structure for their json output and inpout. Newer pandoc's toplevel is a hash. We are going to want to support this. We can tell what kind of output we should produce by looking at the input we got (which itself came from pandoc). So:

Having just sent this I see the typo `copying' in the title... I have fixed that in my tree.

Ian.

#3 lore raw
Quoted 9 lines

-----Original Message-----
From: Ian Jackson <[email protected]>
Sent: 09 June 2020 12:34
To: [email protected]; Andrew Cooper <[email protected]>; Paul Durrant <[email protected]>
Subject: Re: [XEN PATCH for-4.14 1/2] docs-parse-support-md: Prepare for copying with pandoc versions

Ian Jackson writes ("[XEN PATCH for-4.14 1/2] docs-parse-support-md: Prepare for copying with pandoc versions"):

Quoted 9 lines
Quoted 5 lines

Different pandoc versions generate, and expect, a different toplevel structure for their json output and inpout. Newer pandoc's toplevel is a hash. We are going to want to support this. We can tell what kind of output we should produce by looking at the input we got (which itself came from pandoc). So:

Having just sent this I see the typo `copying' in the title... I have fixed that in my tree.

With the typo fixed...