From 2ab98307ef3ced319e989b1b6c8d756d23b52624 Mon Sep 17 00:00:00 2001 From: Michael Olson Date: Sat, 23 Dec 2006 22:23:55 +0000 Subject: [PATCH] muse-latex2png: handle publishing to styles other than HTML 2006-12-23 Michael Olson * lisp/muse-latex2png.el: Update header, since this has been rewritten sufficiently to not need an assignment from the original author. (muse-latex2png-use-xhtml): Remove, since we now autodetect this. (muse-latex2png): Use two underscores to separate prefix and hash. (muse-latex2png-region): New function split from muse-publish-latex-tag that can be used easily by other code. Detect whether we are using an HTML-based publishing style, and insert a simpler markup if we are not. If we are using a Latex-based publishing style, do not generate an image, and leave the region alone. Return the path of the generated image, in case other functions want to use this programmatically. (muse-publish-latex-tag): Set a default prefix based on the name of the current file. (muse-publish-math-tag): New tag that surrounds the region with "$" characters, so that it becomes a Latex math region, and then publishes it. * lisp/muse-publish.el (muse-style-derived-p): New function that returns non-nil if a given style, or the current style if omitted, is equal to or derived from the given base style. This is useful in tags, because it allows the user to specify markup that is only to be inserted for one particular style. git-archimport-id: mwolson@gnu.org--2006/muse--main--1.0--patch-261 --- AUTHORS | 5 +- ChangeLog | 26 ++++++++++ lisp/muse-latex2png.el | 135 +++++++++++++++++++++++++++++++------------------ lisp/muse-publish.el | 13 +++++ 4 files changed, 127 insertions(+), 52 deletions(-) diff --git a/AUTHORS b/AUTHORS index 4945381..bd1458c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -87,8 +87,9 @@ Dale P. Smith: Contributor John Sullivan: Contributor -- documentation (assigned past and future changes) -Ganesh Swami: Author of muse-latex2png.el - (assignment pending) +Ganesh Swami: Original author of latex2png.el, which was the basis for + muse-latex2png.el. It has since been extensively + rewritten, so we no longer need an assignment. G. J. Teale: Contributor: - muse-project.el: 2 lines changed diff --git a/ChangeLog b/ChangeLog index 305331a..5e0d048 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,29 @@ +2006-12-23 Michael Olson + + * lisp/muse-latex2png.el: Update header, since this has been + rewritten sufficiently to not need an assignment from the original + author. + (muse-latex2png-use-xhtml): Remove, since we now autodetect this. + (muse-latex2png): Use two underscores to separate prefix and hash. + (muse-latex2png-region): New function split from + muse-publish-latex-tag that can be used easily by other code. + Detect whether we are using an HTML-based publishing style, and + insert a simpler markup if we are not. If we are using a + Latex-based publishing style, do not generate an image, and leave + the region alone. Return the path of the generated image, in case + other functions want to use this programmatically. + (muse-publish-latex-tag): Set a default prefix based on the name + of the current file. + (muse-publish-math-tag): New tag that surrounds the region with + "$" characters, so that it becomes a Latex math region, and then + publishes it. + + * lisp/muse-publish.el (muse-style-derived-p): New function that + returns non-nil if a given style, or the current style if omitted, + is equal to or derived from the given base style. This is useful + in tags, because it allows the user to specify markup that + is only to be inserted for one particular style. + 2006-12-22 Michael Olson * lisp/muse-html.el (muse-html-markup-tags): Add tag. diff --git a/lisp/muse-latex2png.el b/lisp/muse-latex2png.el index a6a41b4..1aea298 100644 --- a/lisp/muse-latex2png.el +++ b/lisp/muse-latex2png.el @@ -1,10 +1,9 @@ ;; muse-latex2png.el --- generate PNG images from inline LaTeX code -;; Copyright (C) 2004, 2005 Ganesh Swami ;; Copyright (C) 2005, 2006 Free Software Foundation, Inc. -;; Author: Ganesh Swami -;; Created: May 01 2004 +;; Author: Michael Olson (mwolson AT gnu DOT org) +;; Created: 12-Oct-2005 ;; This file is part of Emacs Muse. It is not part of GNU Emacs. @@ -25,15 +24,15 @@ ;;; Commentary: -;; Read The Fine Documentation at -;; http://www.sfu.ca/~gswamina/EmacsWikiBlog.html +;; This was taken from latex2png.el, by Ganesh Swami , which was made for emacs-wiki. It has since +;; been extensively rewritten for Muse. -;; Michael Olson adapted this for Muse. +;;; To do -;;; To Do: - -;; If we are publishing a LaTeX-based style, the -;; muse-publish-latex-tag function should insert the LaTeX code as-is. +;; Remove stale image files. This could be done by making a function +;; for `muse-before-publish-hook' that deletes according to +;; (muse-page-name). ;;; Code @@ -81,12 +80,6 @@ This is relative to the current publishing directory." :type 'string :group 'muse-latex2png) -(defcustom muse-latex2png-use-xhtml nil - "Indicate whether the output must be valid XHTML. -This is used for inserting the IMG tag." - :type 'boolean - :group 'muse-latex2png) - (defun muse-latex2png-move2pubdir (file prefix pubdir) "Move FILE to the PUBDIR folder. @@ -107,39 +100,6 @@ Old files with PREFIX in the name are deleted." (concat muse-latex2png-img-dest "/" (file-name-nondirectory file))) (message "Cannot find %s!" file)))) -(defun muse-publish-latex-tag (beg end attrs) - (let ((end-marker (set-marker (make-marker) (1+ end))) - (pubdir (expand-file-name - muse-latex2png-img-dest - (file-name-directory muse-publishing-current-output-path)))) - (save-restriction - (narrow-to-region beg end) - (let* ((text (buffer-substring-no-properties beg end)) - ;; the prefix given to the image file. - (prefix (cdr (assoc "prefix" attrs))) - ;; preamble (for extra options) - (preamble (cdr (assoc "preamble" attrs))) - ;; display inline or as a block - (display (car (assoc "inline" attrs)))) - (delete-region beg end) - (goto-char (point-min)) - (unless (file-directory-p pubdir) - (make-directory pubdir)) - (let ((path (muse-latex2png-move2pubdir - (muse-latex2png text prefix preamble) - prefix pubdir))) - (when path - (muse-insert-markup - "\"latex2png" - ">")) - (muse-insert-markup "") - (goto-char (point-max)))))))) - (defun muse-latex2png (code prefix preamble) "Convert the LaTeX CODE into a png file beginning with PREFIX. PREAMBLE indicates extra packages and definitions to include." @@ -153,7 +113,7 @@ PREAMBLE indicates extra packages and definitions to include." (temp-directory)) (t "/tmp"))) (texfile (expand-file-name - (concat prefix "_" (format "%d" (abs (sxhash code)))) + (concat prefix "__" (format "%d" (abs (sxhash code)))) tmpdir)) (defalt-directory default-directory)) (with-temp-file (concat texfile ".tex") @@ -187,11 +147,86 @@ PREAMBLE indicates extra packages and definitions to include." (message (concat "Failed to create dvi file " texfile)) nil))) +(defun muse-latex2png-region (beg end attrs) + "Generate an image for the Latex code between BEG and END. +If a Muse page is currently being published, replace the given +region with the appropriate markup that displays the image. +Otherwise, just return the path of the generated image. + +Valid keys for the ATTRS alist are as follows. + +prefix: The prefix given to the image file. +preamble: Extra text to add to the Latex preamble. +inline: Display image as inline, instead of a block." + (let ((end-marker (set-marker (make-marker) (1+ end))) + (pubdir (expand-file-name + muse-latex2png-img-dest + (file-name-directory muse-publishing-current-output-path)))) + (save-restriction + (narrow-to-region beg end) + (let* ((text (buffer-substring-no-properties beg end)) + ;; the prefix given to the image file. + (prefix (cdr (assoc "prefix" attrs))) + ;; preamble (for extra options) + (preamble (cdr (assoc "preamble" attrs))) + ;; display inline or as a block + (display (car (assoc "inline" attrs)))) + (when muse-publishing-p + (delete-region beg end) + (goto-char (point-min))) + (unless (file-directory-p pubdir) + (make-directory pubdir)) + (let ((path (muse-latex2png-move2pubdir + (muse-latex2png text prefix preamble) + prefix pubdir))) + (when path + (when muse-publishing-p + (muse-insert-markup + (if (muse-style-derived-p "html") + (concat "\"latex2png" + ">") + (muse-insert-markup "")) + (let ((ext (or (file-name-extension path) "")) + (path (muse-path-sans-extension path))) + (muse-markup-text 'image path ext)))) + (goto-char (point-max))) + path)))))) + +(defun muse-publish-latex-tag (beg end attrs) + "If the current style is not Latex-based, generate an image for the +given Latex code. Otherwise, don't do anything to the region. +See `muse-latex2png-region' for valid keys for ATTRS." + (unless (assoc "prefix" attrs) + (setq attrs (cons (cons "prefix" + (concat "latex2png-" (muse-page-name))) + attrs))) + (unless (muse-style-derived-p "latex") + (muse-latex2png-region beg end attrs))) + +(defun muse-publish-math-tag (beg end) + "Surround the given region with \"$\" characters. Then, if the +current style is not Latex-based, generate an image for the given +Latex math code." + (insert "$") + (goto-char end) + (insert "$") + (unless (muse-style-derived-p "latex") + (muse-latex2png-region beg (point) '(("inline" . t))))) + ;;; Insinuate with muse-publish (add-to-list 'muse-publish-markup-tags '("latex" t t nil muse-publish-latex-tag) t) +(add-to-list 'muse-publish-markup-tags + '("math" t nil nil muse-publish-latex-tag) + t) + (provide 'muse-latex2png) ;;; muse-latex2png.el ends here diff --git a/lisp/muse-publish.el b/lisp/muse-publish.el index 70449ec..a28a666 100644 --- a/lisp/muse-publish.el +++ b/lisp/muse-publish.el @@ -368,6 +368,19 @@ If STYLE is not specified, use current style." (if base (muse-style-element elem base direct)))))) +(defun muse-style-derived-p (base &optional style) + "Return non-nil if STYLE is equal to or derived from BASE, +non-nil otherwise. + +BASE should be a string." + (if (and (stringp style) + (string= style base)) + t + (setq style (muse-style style)) + (let ((value (muse-get-keyword :base style))) + (when value + (muse-style-derived-p base value))))) + (defun muse-find-markup-element (keyword ident style) (let ((def (assq ident (muse-style-element keyword style)))) (if def -- 2.11.4.GIT