summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/pandoc.pm
blob: 5f960b6e9418a005f6931b6cee6285f0d6e46b9b (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::pandoc;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use FileHandle;
  7. use IPC::Open2;
  8. sub import {
  9. my $markdown_ext = $config{pandoc_markdown_ext} || "mdwn";
  10. hook(type => "getsetup", id => "pandoc", call => \&getsetup);
  11. hook(type => "htmlize", id => $markdown_ext,
  12. # longname => "Pandoc Markdown",
  13. call => sub { htmlize("markdown", @_) });
  14. if ($config{pandoc_latex}) {
  15. hook(type => "htmlize", id => "tex",
  16. call => sub { htmlize("latex", @_) });
  17. }
  18. if ($config{pandoc_rst}) {
  19. hook(type => "htmlize", id => "rst",
  20. call => sub { htmlize("rst", @_) });
  21. }
  22. }
  23. sub getsetup () {
  24. return
  25. plugin => {
  26. safe => 1,
  27. rebuild => 1,
  28. },
  29. pandoc_command => {
  30. type => "string",
  31. example => "/usr/bin/pandoc",
  32. description => "Path to pandoc executable",
  33. safe => 0,
  34. rebuild => 0,
  35. },
  36. pandoc_markdown_ext => {
  37. type => "string",
  38. example => "mdwn",
  39. description => "File extension for Markdown files",
  40. safe => 1,
  41. rebuild => 1,
  42. },
  43. pandoc_latex => {
  44. type => "boolean",
  45. example => 0,
  46. description => "Enable Pandoc processing of LaTeX documents",
  47. safe => 0,
  48. rebuild => 1,
  49. },
  50. pandoc_rst => {
  51. type => "boolean",
  52. example => 0,
  53. description => "Enable Pandoc processing of reStructuredText documents",
  54. safe => 0,
  55. rebuild => 1,
  56. },
  57. pandoc_smart => {
  58. type => "boolean",
  59. example => 1,
  60. description => "Use smart quotes, dashes, and ellipses",
  61. safe => 1,
  62. rebuild => 1,
  63. },
  64. pandoc_obfuscate => {
  65. type => "boolean",
  66. example => 1,
  67. description => "Obfuscate emails",
  68. safe => 1,
  69. rebuild => 1,
  70. },
  71. pandoc_html5 => {
  72. type => "boolean",
  73. example => 0,
  74. description => "Generate HTML5",
  75. safe => 1,
  76. rebuild => 1,
  77. },
  78. pandoc_ascii => {
  79. type => "boolean",
  80. example => 0,
  81. description => "Generate ASCII instead of UTF8",
  82. safe => 1,
  83. rebuild => 1,
  84. },
  85. pandoc_numsect => {
  86. type => "boolean",
  87. example => 0,
  88. description => "Number sections",
  89. safe => 1,
  90. rebuild => 1,
  91. },
  92. pandoc_sectdiv => {
  93. type => "boolean",
  94. example => 0,
  95. description => "Attach IDs to section DIVs instead of Headers",
  96. safe => 1,
  97. rebuild => 1,
  98. },
  99. pandoc_codeclasses => {
  100. type => "string",
  101. example => "",
  102. description => "Classes to use for indented code blocks",
  103. safe => 1,
  104. rebuild => 1,
  105. },
  106. pandoc_math => {
  107. type => "string",
  108. example => "unicode",
  109. description => "Process TeX math using",
  110. safe => 0,
  111. rebuild => 1,
  112. },
  113. }
  114. sub htmlize ($@) {
  115. my $format = shift;
  116. my %params = @_;
  117. my $page = $params{page};
  118. local(*PANDOC_IN, *PANDOC_OUT);
  119. my @args;
  120. my $command = $config{pandoc_command} || "/usr/bin/pandoc";
  121. if ($config{pandoc_smart}) {
  122. push @args, '--smart';
  123. };
  124. if ($config{pandoc_obfuscate}) {
  125. push @args, '--email-obfuscation=references';
  126. } else {
  127. push @args, '--email-obfuscation=none';
  128. };
  129. if ($config{pandoc_html5}) {
  130. push @args, '--html5';
  131. };
  132. if ($config{pandoc_ascii}) {
  133. push @args, '--ascii';
  134. };
  135. if ($config{pandoc_numsect}) {
  136. push @args, '--number-sections';
  137. };
  138. if ($config{pandoc_sectdiv}) {
  139. push @args, '--section-divs';
  140. };
  141. if ($config{pandoc_codeclasses} && ($config{pandoc_codeclasses} ne "")) {
  142. push @args, '--indented-code-classes=' . $config{pandoc_codeclasses};
  143. };
  144. for ($config{pandoc_math}) {
  145. if (/^mathjax$/) {
  146. push @args, '--mathjax=/dev/null';
  147. }
  148. elsif (/^jsmath$/) {
  149. push @args, '--jsmath';
  150. }
  151. elsif (/^latexmathml$/) {
  152. push @args, '--latexmathml';
  153. }
  154. elsif (/^mimetex$/) {
  155. push @args, '--mimetex';
  156. }
  157. elsif (/^mathtex$/) {
  158. push @args, '--mimetex=/cgi-bin/mathtex.cgi';
  159. }
  160. elsif (/^google$/) {
  161. push @args, '--webtex';
  162. }
  163. elsif (/^mathml$/) {
  164. push @args, '--mathml';
  165. }
  166. else { }
  167. }
  168. # $ENV{"LC_ALL"} = "en_US.UTF-8";
  169. my $pid = open2(*PANDOC_IN, *PANDOC_OUT, $command,
  170. '-f', $format,
  171. '-t', 'html',
  172. @args);
  173. error("Unable to open $command") unless $pid;
  174. # Workaround for perl bug (#376329)
  175. require Encode;
  176. my $content = Encode::encode_utf8($params{content});
  177. print PANDOC_OUT $content;
  178. close PANDOC_OUT;
  179. my @html = <PANDOC_IN>;
  180. close PANDOC_IN;
  181. waitpid $pid, 0;
  182. $content = Encode::decode_utf8(join('', @html));
  183. return $content;
  184. }
  185. 1