Permalink
Browse files

Tweaked and tested.

Too many little changes to list, but so far it appears that things are
working properly.
  • Loading branch information...
1 parent 5d25f40 commit a80a34c56343cc91806ec8df779d290f6dc25271 @chrismgray committed Mar 14, 2012
Showing with 84 additions and 68 deletions.
  1. +50 −46 lisp/ikiwiki-org-plugin.el
  2. +34 −22 perl/new_org.pm
@@ -28,7 +28,6 @@
(while (and subdirs-match-index (< subdirs-match-index best-link-len) (< subdirs-match-index destpage-len)
(string= (substring best-link 0 subdirs-match-index) (substring destpage 0 subdirs-match-index)))
(setq matching-subdirs-index subdirs-match-index)
- (message (int-to-string matching-subdirs-index))
(setq subdirs-match-index (string-match "/" destpage (1+ subdirs-match-index))))
matching-subdirs-index))
(link-prefix "")
@@ -41,49 +40,57 @@
link-prefix)))
(concat link-prefix (substring best-link (1+ matching-subdirs-index))))))
-(defun ikiwiki-org-linkify (infile outfile sentinel-file)
- (find-file outfile)
- (with-current-buffer outfile
+(defun ikiwiki-org-linkify (infile outfile destpage link-hash)
+ (with-temp-buffer
(insert-file-contents infile)
- (let ((destpage (with-temp-buffer
- (insert-file-contents sentinel-file)
- (buffer-string))))
- (goto-char (point-min))
- (while (re-search-forward org-bracket-link-regexp (point-max) t)
- (let* ((url-part (match-string-no-properties 1))
- (text-part (match-string-no-properties 3))
- (best-link (save-match-data (ikiwiki-org-bestlink page url-part get-response-fn)))
- (image? (save-match-data (string-match (org-image-file-name-regexp) url-part))))
- (if best-link
- ;; internal page
- (let* ((corrected-link (save-match-data
- (ikiwiki-org-correct-link best-link destpage))))
- (replace-match (concat "[[./" corrected-link "][" (or (when image? corrected-link) text-part url-part) "]]") t t))
- ;; external page -- put a slash in front if no text part
- ;; otherwise, leave the same
- (when (not text-part)
- (replace-match (concat "\\[[" url-part "]]") t t))))))
- (save-buffer))
- (kill-buffer outfile)
- (delete-file sentinel-file))
+ (goto-char (point-min))
+ (while (re-search-forward org-bracket-link-regexp (point-max) t)
+ (let* ((url-part (match-string-no-properties 1))
+ (text-part (match-string-no-properties 3))
+ (best-link (gethash url-part link-hash))
+ (image? (save-match-data (string-match (org-image-file-name-regexp) url-part))))
+ (message url-part)
+ (when best-link (message best-link))
+ (if best-link
+ ;; internal page
+ (let* ((corrected-link (save-match-data
+ (ikiwiki-org-correct-link best-link destpage))))
+ (replace-match (concat "[[./" corrected-link "][" (or (when image? corrected-link) text-part url-part) "]]") t t))
+ ;; external page -- put a slash in front if no text part
+ ;; otherwise, leave the same
+ (when (not text-part)
+ (replace-match (concat "\\[[" url-part "]]") t t)))))
+ (append-to-file (point-min) (point-max) outfile)))
-(defun ikiwiki-org-scan (infile outfile sentinel-file)
- (find-file infile)
- (find-file outfile)
- (with-current-buffer infile
- (goto-char (point-min))
- (while (re-search-forward org-any-link-re (point-max) t)
- (with-current-buffer outfile
- (goto-char (point-max))
- (insert (match-string 0))
- (insert "\n")))
- (save-buffer outfile))
- (kill-buffer outfile)
- (kill-buffer infile)
- (delete-file sentinel-file))
+(defun ikiwiki-org-scan (infile outfile)
+ (with-temp-buffer
+ (insert-file-contents infile)
+ (org-mode)
+ (let ((org-info (org-infile-export-plist)))
+ (when (plist-get org-info :title)
+ (save-excursion
+ (with-temp-buffer
+ (insert "#+TITLE: ")
+ (insert (plist-get org-info :title))
+ (insert "\n")
+ (append-to-file (point-min) (point-max) outfile))))
+ (when (plist-get org-info :author)
+ (save-excursion
+ (with-temp-buffer
+ (insert "#+AUTHOR: ")
+ (insert (plist-get org-info :author))
+ (insert "\n")
+ (append-to-file (point-min) (point-max) outfile)))))
+ (goto-char (point-min))
+ (while (re-search-forward org-bracket-link-regexp (point-max) t)
+ (let ((url (match-string-no-properties 1)))
+ (save-excursion
+ (with-temp-buffer
+ (insert url)
+ (insert "\n")
+ (append-to-file (point-min) (point-max) outfile)))))))
-(defun ikiwiki-org-htmlize (infile outfile sentinel-file)
- (find-file outfile)
+(defun ikiwiki-org-htmlize (infile outfile)
(let* ((org-export-html-preamble nil)
(org-export-html-postamble nil)
(org-export-with-sub-superscripts nil)
@@ -99,12 +106,9 @@
(org-export-as-html 3 t nil 'string t))))
(ret-html (cadr org-info))
(title (plist-get (car org-info) :title)))
- (with-current-buffer outfile
- (insert (concat "[[!meta title=\"" title "\"]]\n"))
+ (with-temp-buffer
(insert ret-html)
- (save-buffer))
- (kill-buffer outfile)
- (delete-file sentinel-file)))
+ (append-to-file (point-min) (point-max) outfile))))
(provide 'ikiwiki-org-plugin)
View
@@ -6,37 +6,34 @@ use strict;
use IkiWiki 3.00;
use File::Temp qw/ tempfile unlink0 /;
-my $org_file_regexp = /\.org$/;
+my $org_file_regexp = qr/\.org$/;
sub run_func_in_emacs($$;$) {
my $func = shift;
my $content = shift;
my $args = shift;
- my ($tf1, $tn1) = tempfile(); # Perl output, emacs input
- my ($tf2, $tn2) = tempfile(); # Perl input, emacs output
- my ($tf3, $tn3) = tempfile(); # Signal/args file
- print $tf1, $content;
- print $tf3, $args;
- system("emacs -s org-ikiwiki-compiler --eval \"(ikiwiki-org-$func $tn1 $tn2 $tn3)\"");
+ my ($tf1, $tn1) = tempfile("/tmp/ikiwiki-org-1-XXXXX"); # Perl output, emacs input
+ my ($tf2, $tn2) = tempfile("/tmp/ikiwiki-org-2-XXXXX"); # Perl input, emacs output
+ print $tf1 $content;
+ if (!defined $args) { $args = ""; }
+ # WARNING: possible security hole
+ $args =~ s/'/'\\''/g;
+ my $argstring = qq/emacsclient -s org-ikiwiki-compiler --eval '(ikiwiki-org-$func "$tn1" "$tn2" $args)'/;
+ system($argstring);
# Wait for emacs to finish
- while (-e $tn3) {
- usleep(100);
- }
- my $ret = <$tf2>;
- # After debugging
- # unlink0($tf1, $tn1);
- # unlink0($tf2, $tn2);
- # unlink0($tf3, $tn3);
- return $ret;
+ my @ret = <$tf2>;
+ unlink0($tf1, $tn1);
+ unlink0($tf2, $tn2);
+ return @ret;
}
sub import {
system("emacsclient -s org-ikiwiki-compiler --eval \"(+ 1 1)\"");
if ($? != 0) {
system("emacs --daemon --eval \"(progn (require 'ikiwiki-org-plugin) (setq server-name \\\"org-ikiwiki-compiler\\\") (server-start))\"");
if ($? != 0) {
- print STDERR, "Failed to start emacs. Will not continue with new_org setup.\n";
- return
+ print STDERR "Failed to start emacs. Will not continue with new_org setup.\n";
+ return;
}
}
hook(type => "htmlize", id => "org", call => \&htmlize, first => 1);
@@ -48,7 +45,16 @@ sub linkify(@) {
my %params = @_;
my $page_file_name = $pagesources{$params{page}};
if ($page_file_name =~ $org_file_regexp) {
- return $params{content} = run_func_in_emacs("linkify", $params{content}, $params{destpage});
+ my $hash_adds = "";
+ my @ls = @{$links{$params{page}}};
+ foreach my $link (@ls) {
+ my $bp = bestlink($params{page}, $link);
+ if ($bp) {
+ $hash_adds .= qq/ (puthash "$link" "$bp" page-hash)/;
+ }
+ }
+ my $page_hash = "(let ((page-hash (make-hash-table :test 'equal))) $hash_adds page-hash)";
+ return $params{content} = join('', run_func_in_emacs("linkify", $params{content}, "\"$params{destpage}\" $page_hash"));
}
return $params{content}
}
@@ -57,15 +63,21 @@ sub scan(@) {
my %params = @_;
my $page_file_name = $pagesources{$params{page}};
if ($page_file_name =~ $org_file_regexp) {
- my @lines = split /\n/, run_func_in_emacs("scan", $params{content});
+ my @lines = run_func_in_emacs("scan", $params{content});
foreach my $line (@lines) {
chomp $line;
- add_link($params{page}, $line);
+ if ($line =~ /#\+TITLE: (.*)$/) {
+ $pagestate{$params{page}}{meta}{title} = $1;
+ } elsif ($line =~ /#\+AUTHOR: (.*)$/) {
+ $pagestate{$params{page}}{meta}{author} = $1;
+ } else {
+ add_link($params{page}, $line);
+ }
}
}
}
sub htmlize(@) {
my %params = @_;
- return run_func_in_emacs("htmlize", $params{content});
+ return join('', run_func_in_emacs("htmlize", $params{content}));
}

0 comments on commit a80a34c

Please sign in to comment.