Update org.pm plugin for IkiWiki 3.00.
authorW. Trevor King <wking@drexel.edu>
Wed, 6 Oct 2010 10:33:45 +0000 (06:33 -0400)
committerW. Trevor King <wking@tremily.us>
Sun, 13 Jan 2013 12:11:33 +0000 (07:11 -0500)
Also replace string postprocessing with org-export-... configuration.

IkiWiki/Plugin/org.pm

index 36e2692806782e85d4893fc1ebb066778c194520..5ddfdfe4d2682c0890c016445f19c1b653af9645 100644 (file)
@@ -1,68 +1,85 @@
 #!/usr/bin/perl
-# File: org.pm
-# Time-stamp: <2008-06-08 23:12:20 srivasta>
-#
-# Copyright (C) 2008 by Manoj Srivastava
-#
-# Author: Manoj Srivastava
-#
-# Description:
-# This allows people to write Ikiwiki content using Emacs and org-mode
-# (requires Emacs 23), and uses the html export facility of org-mode to
-# create the output. Some bits based on otl.pm.
-# Here is the code: org.pm
-
+# Emacs org-mode markup language.
 package IkiWiki::Plugin::org;
+
 use warnings;
 use strict;
-use Carp;
-use IkiWiki 2.00;
+use File::Temp;
+use IkiWiki 3.00;
 
-use File::Temp qw/ tempfile tempdir /;
+sub import {
+       hook(type => "getsetup", id => "org", call => \&getsetup);
+       hook(type => "checkconfig", id => "org", call => \&checkconfig);
+       hook(type => "htmlize", id => "org", call => \&htmlize);
+}
 
-# ------------------------------------------------------------
+sub getsetup () {
+       return
+               plugin => {
+                       safe => 1,
+                       rebuild => 1, # format plugin
+                       section => "format",
+               },
+               emacs_path => {
+                       type => "string",
+                       example => 0,
+                       description => "path to an external emacs binary",
+                       safe => 0,
+                       rebuild => undef,
+               },
+               emacs_org_options => {
+                       type => "string",
+                       example => 0,
+                       description => "emacs options to convert org-mode to html.  The string 'FILE' will be replaced with the input filename.",
+                       safe => 0,
+                       rebuild => undef,
+               },
+}
 
-sub import {
-  hook(type => "htmlize", id => "org", call => \&htmlize);
-} #
+sub checkconfig () {
+       if (! defined $config{emacs_path}) {
+               $config{emacs_path}="/usr/bin/emacs";
+       }
+       debug("Emacs: $config{emacs_path}");
+       if (! defined $config{emacs_org_options}) {
+               # See http://orgmode.org/org.html#Publishing-options
+               $config{emacs_org_options}='--batch '.
+                       '--load org '. # =$HOME/lib/emacs/org.el
+                       '--eval "(setq '.
+                               "org-export-author-info 'nil ".
+                               "org-export-headline-levels 3 ".
+                               "org-export-html-preamble 'nil ".
+                               "org-export-html-postamble 'nil ".
+                               "org-export-with-toc 'nil ".
+                               "org-export-skip-text-before-1st-heading t ".
+                       ')" '.
+                       "--visit FILE ".
+                       q{--eval "(org-export-as-html-to-buffer 'nil)" }.
+                       q{--eval '(set-visited-file-name "/dev/stdout")' }.
+                       '--funcall save-buffer ';
+       }
+}
 
+my $tempdir;
 sub htmlize (@) {
-  my %params  = @_;
-  my $dir     = File::Temp->newdir();
-
+       my $args = "$config{emacs_path} $config{emacs_org_options}";
+       my %params = @_;
+       my $fh;
+       my $filename;
 
+       if (! defined $tempdir) {
+           $tempdir = File::Temp::tempdir( CLEANUP => 1 );
+       }
+       ($fh, $filename) = File::Temp::tempfile( DIR => $tempdir, SUFFIX => '.org' );
+       binmode ($fh, ':utf8');
+       print $fh "$params{content}\n";
+       close($fh);
 
-  my $ret = open(INPUT, ">$dir/contents.org");
-  unless (defined $ret) {
-    debug("failed to open $dir/contents.org: $@");
-    return $params{content};
-  }
-  
-  print INPUT $params{content};
-  close INPUT;
-  my $args = '/usr/local/bin/emacs --batch -l org ' .
-              "--eval '(setq org-export-headline-levels 3 org-export-with-toc nil org-export-author-info nil )' " .
-              "--visit=$dir/contents.org " .
-              '--funcall org-export-as-html-batch >/dev/null 2>&1';
-  if (system($args)) {
-    debug("failed to convert $params{page}: $@");
-    return $params{content};
-  }
-  $ret = open(OUTPUT, "$dir/contents.html");
-  unless (defined $ret) {
-    debug("failed find html output for $params{page}: $@");
-    return $params{content};
-  }
-  local $/ = undef;
-  $ret = <OUTPUT>;
-  close OUTPUT;
-  $ret=~s/(.*<h1 class="title">){1}?//s;
-  $ret=~s/^(.*<\/h1>){1}?//s;
-  $ret=~s/<div id="postamble">.*//s;
-  $ret=~s/(<\/div>\s*$)//s;
-  
-  return $ret;
+       $args =~ s/FILE/$filename/g;
+       #$args =~ s/FILE/posts\/Git\/notes.org/g;
+       debug("Executing: $args");
+       my $content = `$args`;
+       return $content;
 }
 
-# ------------------------------------------------------------
-1; # modules have to return a true value
+1