Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Have pandoc footnotes at bottom of page or section in HTML?

I've started using inline footnotes in Markdown:

Some text^[an aside here]. More text.

When I use pandoc to export to HTML they appear at the end of the whole document, but in PDF they appear at the end of the page. I prefer them at the end of the page, and I'm wondering if there is a way to get them that way in HTML?

I realize end-of-page would get complicated with HTML; end of section would work just as well for me. In fact, putting them at end of the section in the PDF, instead of end of the page, might also be useful. (I've tried putting --- as a section break, but the footnotes still end up at the end of the document.)

(I've also tried making the pdf, then pdf2html, which kind of works but is really hard on the eyes. Pandoc doesn't seem to support pdf to html, I get "Cannot decode byte '\xd0' ...")


(This is not a duplicate of: Generate inline rather than list-style footnotes in Pandoc Markdown output? That question is about the way footnotes are handled when moving to markdown format, from another format.)

like image 803
Darren Cook Avatar asked Mar 31 '16 10:03

Darren Cook


1 Answers

No clue how to do this for a (printed) page (I'm looking for that myself), but for footnotes by section, couldn't you just break the input document into the sections before you pass them to pandoc, and then reassemble the parts afterwards?

E.g. suppose your markdown file looks like this (myfile.md):

Some text with a footnote.^[First note.]
----
Some more text with a footnote.^[Second note.]
----
And a third passage.^[Third note.]

Then you could use, e.g., the following PHP script (though I'm sure there a million ways to do this), i.e., pandoc-sections.php.

<?php

$input_file = $argv[1];

if (!file_exists($input_file)) {
    exit('File not found.');
}

$chunks = preg_split('/\n-----*\n/',file_get_contents($input_file));

for ($i=0; $i<count($chunks); $i++) {
    $chunk = $chunks[$i];
    $idprefix = "section" . ($i+1);

    $descriptorspec = array(
      0 => array("pipe", "r"), // stdin
      1 => array("pipe", "w"), // stdout 
      2 => array("pipe", "w")  // stderr
    );

    $pandoc_process = proc_open('pandoc --id-prefix=' . 
         $idprefix, $descriptorspec, $pipes);

    if (is_resource($pandoc_process)) {
      fwrite($pipes[0], $chunk);
      fclose($pipes[0]);
      echo stream_get_contents($pipes[1]);
      fclose($pipes[1]);
      fclose($pipes[2]);
      proc_close($pandoc_process);
   }

}    

?>

Then run

php pandoc-sections.php myfile.md

And you get:

<p>Some text with a footnote.<a href="#section1fn1" class="footnote-ref" id="section1fnref1"><sup>1</sup></a></p>
<section class="footnotes">
<hr />
<ol>
<li id="section1fn1"><p>First note.<a href="#section1section1fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>
<p>Some more text with a footnote.<a href="#section2fn1" class="footnote-ref" id="section2fnref1"><sup>1</sup></a></p>
<section class="footnotes">
<hr />
<ol>
<li id="section2fn1"><p>Second note.<a href="#section2section2fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>
<p>And a third passage.<a href="#section3fn1" class="footnote-ref" id="section3fnref1"><sup>1</sup></a></p>
<section class="footnotes">
<hr />
<ol>
<li id="section3fn1"><p>Third note.<a href="#section3section3fnref1" class="footnote-back">↩</a></p></li>
</ol>
</section>

Adapt as needed.

like image 121
frabjous Avatar answered Oct 31 '22 14:10

frabjous