From aef14adbfd1a7f515ac1df93c437be20e69e79df Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Wed, 6 Oct 2010 06:33:45 -0400 Subject: [PATCH] Update org.pm plugin for IkiWiki 3.00. Also replace string postprocessing with org-export-... configuration. --- IkiWiki/Plugin/org.pm | 127 ++++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 55 deletions(-) diff --git a/IkiWiki/Plugin/org.pm b/IkiWiki/Plugin/org.pm index 36e269280..5ddfdfe4d 100644 --- a/IkiWiki/Plugin/org.pm +++ b/IkiWiki/Plugin/org.pm @@ -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 = ; - close OUTPUT; - $ret=~s/(.*

){1}?//s; - $ret=~s/^(.*<\/h1>){1}?//s; - $ret=~s/
.*//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 -- 2.26.2