From 94fe07a5f5a6260d930ad664b981d09c9750dfd9 Mon Sep 17 00:00:00 2001 From: Jambunathan K Date: Thu, 8 Mar 2012 13:25:58 +0530 Subject: [PATCH] org-e-html: Improve handling of images, headlines and inlinetasks 1. Handle inlining of remote images 2. Proper handling of "alt" attribute for regular imgages and dvipnged fragments. 3. Support for Inlinetasks 4. Headlines are "transcoded" in three places: - org-e-html-headline (`org-e-html-format-headline') - org-e-html-inlinetask - org-e-html-toc (`org-e-html-format-toc-headline') These are now conveniently wrapped and cleared through 'org-e-html-format-headline--wrap'. The regular and toc headlines formatters mentioned afore take a superset of arguments allowed by `org-e-html-format-headline-function'. Augmenting the signature of the `org-e-html-format-headline-function' is worth considering. --- EXPERIMENTAL/org-e-html.el | 338 +++++++++++++++++++-------------------------- 1 file changed, 144 insertions(+), 194 deletions(-) diff --git a/EXPERIMENTAL/org-e-html.el b/EXPERIMENTAL/org-e-html.el index 7928489e1..8cd90fe27 100644 --- a/EXPERIMENTAL/org-e-html.el +++ b/EXPERIMENTAL/org-e-html.el @@ -163,12 +163,6 @@ Intended to be locally bound around a call to `org-export-as-html'." ) (defvar org-e-html-table-cur-rowgrp-is-hdr) (defvar org-lparse-table-is-styled) - -(defvar org-e-html-headline-formatter - (lambda (level snumber todo todo-type priority - title tags target extra-targets extra-class) - (concat snumber " " title))) - ;;; User Configuration Variables @@ -879,7 +873,6 @@ order to reproduce the default set-up: :group 'org-export-e-html :type 'function) - ;;;; Emphasis (defcustom org-e-html-emphasis-alist @@ -931,16 +924,6 @@ to typeset and try to protect special characters." ;;;; Links -(defcustom org-e-html-image-default-option "width=.9\\linewidth" - "Default option for images." - :group 'org-export-e-html - :type 'string) - -(defcustom org-e-html-default-figure-position "htb" - "Default position for latex figures." - :group 'org-export-e-html - :type 'string) - (defcustom org-e-html-inline-image-rules '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'") ("http" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\|svg\\)\\'") @@ -1174,29 +1157,25 @@ ATTR is a string of other attributes of the \"a\" element." (defun org-e-html-format-inline-image (src &optional caption label attr standalone-p) - "Create image tag with source and attributes." - (let* ((caption (or caption (org-find-text-property-in-string - 'org-caption src))) - (label (or label (org-find-text-property-in-string - 'org-label src))) - (attr (or attr (org-find-text-property-in-string - 'org-attributes src))) - (img-extras (if (string-match "^ltxpng/" src) + (let* ((id (if (not label) "" + (format " id=\"%s\"" (org-export-solidify-link-text label)))) + (attr (concat attr + (cond + ((string-match "\\" src img-extras))) - (cond - (standalone-p - (format "\n%s\n" - (if (not label) "" - (format " id=\"%s\"" (org-export-solidify-link-text label))) - (concat (format "\n

%s

" img) - (when caption (format "\n

%s

" caption))))) - (t img))))) + (org-e-html-encode-plain-text + (org-find-text-property-in-string + 'org-latex-src src)))) + (t (format " alt=\"%s\"" + (file-name-nondirectory src))))))) + (cond + (standalone-p + (let ((img (format "" src attr))) + (format "\n%s%s\n" + id (format "\n

%s

" img) + (when caption (format "\n

%s

" caption))))) + (t (format "" src (concat attr id)))))) ;;;; Bibliography @@ -1298,26 +1277,6 @@ that uses these same face definitions." (if (looking-at " +") (replace-match "")) (goto-char (point-min))) -(defun org-e-html-format-toc-entry (snumber todo headline tags href) - (setq headline (concat - ;; section number - (and org-export-with-section-numbers (concat snumber " ")) - ;; headline - headline - ;; tags - (and tags (concat - "   " - (format "%s" tags))))) - ;; fontify headline based on TODO keyword - (when todo - (setq headline (format "%s" headline))) - (format "%s" href headline)) - -(defun org-e-html-toc-entry-formatter - (level snumber todo todo-type priority - headline tags target extra-targets extra-class) - (org-e-html-format-toc-entry snumber todo headline tags target)) - (defun org-e-html-make-string (n string) (let (out) (dotimes (i n out) (setq out (concat string out))))) @@ -1344,13 +1303,25 @@ that uses these same face definitions." (org-e-html-make-string (- prev-level start-level) "\n\n")))) +(defun* org-e-html-format-toc-headline + (todo todo-type priority text tags + &key level section-number headline-label &allow-other-keys) + (let ((headline (concat + section-number (and section-number ". ") + text + (and tags "   ") (org-e-html--tags tags)))) + (format "%s" + headline-label + (if (not nil) headline + (format "%s" todo-type headline))))) + (defun org-e-html-toc (depth info) (assert (wholenump depth)) (let* ((headlines (org-export-collect-headlines info depth)) (toc-entries (loop for headline in headlines collect - (list (org-e-html-headline-text - headline info 'org-e-html-toc-entry-formatter) + (list (org-e-html-format-headline--wrap + headline info 'org-e-html-format-toc-headline) (org-export-get-relative-level headline info))))) (when toc-entries (let* ((lang-specific-heading @@ -1479,18 +1450,6 @@ This is used to choose a separator for constructs like \\verb." when (not (string-match (regexp-quote (char-to-string c)) s)) return (char-to-string c)))) -(defun org-e-html--make-option-string (options) - "Return a comma separated string of keywords and values. -OPTIONS is an alist where the key is the options keyword as -a string, and the value a list containing the keyword value, or -nil." - (mapconcat (lambda (pair) - (concat (first pair) - (when (> (length (second pair)) 0) - (concat "=" (second pair))))) - options - ",")) - (defun org-e-html--quotation-marks (text info) "Export quotation marks depending on language conventions. TEXT is a string containing quotation marks to be replaced. INFO @@ -1766,6 +1725,40 @@ original parsed data. INFO is a plist holding export options." +;;; Transcode Helpers + +(defun org-e-html--todo (todo) + (when todo + (format "%s" + (if (member todo org-done-keywords) "done" "todo") + org-e-html-todo-kwd-class-prefix (org-e-html-fix-class-name todo) + todo))) + +(defun org-e-html--tags (tags) + (when tags + (format "%s" + (mapconcat + (lambda (tag) + (format "%s" + (concat org-e-html-tag-class-prefix + (org-e-html-fix-class-name tag)) + tag)) + (org-split-string tags ":") " ")))) + +(defun* org-e-html-format-headline + (todo todo-type priority text tags + &key level section-number headline-label &allow-other-keys) + (let ((section-number + (when section-number + (format "%s " + level section-number))) + (todo (org-e-html--todo todo)) + (tags (org-e-html--tags tags))) + (concat section-number todo (and todo " ") text + (and tags "   ") tags))) + + + ;;; Transcode Functions ;;;; Block @@ -2066,19 +2059,18 @@ CONTENTS is nil. INFO is a plist holding contextual information." ;;;; Headline -(defun org-e-html-todo (todo) - (when todo - (format "%s" - (if (member todo org-done-keywords) "done" "todo") - org-e-html-todo-kwd-class-prefix (org-e-html-fix-class-name todo) - todo))) - -(defun org-e-html-headline-text (headline info &optional formatter) +(defun org-e-html-format-headline--wrap (headline info + &optional format-function + &rest extra-keys) "Transcode an HEADLINE element from Org to HTML. CONTENTS holds the contents of the headline. INFO is a plist holding contextual information." - (let* ((numberedp (org-export-numbered-headline-p headline info)) - (level (org-export-get-relative-level headline info)) + (let* ((level (+ (org-export-get-relative-level headline info) + (1- org-e-html-toplevel-hlevel))) + (headline-number (org-export-get-headline-number headline info)) + (section-number (and (org-export-numbered-headline-p headline info) + (mapconcat 'number-to-string + headline-number "."))) (todo (and (plist-get info :with-todo-keywords) (let ((todo (org-element-property :todo-keyword headline))) @@ -2091,20 +2083,21 @@ holding contextual information." (org-element-property :title headline) 'e-html info)) (tags (and (plist-get info :with-tags) (org-element-property :tags headline))) - - (headline-no (org-export-get-headline-number headline info)) - (headline-label - (format "sec-%s" (mapconcat 'number-to-string headline-no "-"))) - (headline-labels (list headline-label)) - (headline-no (org-export-get-headline-number headline info)) - (section-no (mapconcat 'number-to-string headline-no ".")) - (primary-target (car (last headline-labels))) - (secondary-targets (butlast headline-labels)) - (extra-class nil) - (formatter (or (and (functionp formatter) formatter) - org-e-html-headline-formatter))) - (funcall formatter level section-no todo todo-type priority - text tags primary-target secondary-targets extra-class))) + (headline-label (concat "sec-" (mapconcat 'number-to-string + headline-number "-"))) + (format-function (cond + ((functionp format-function) format-function) + ((functionp org-e-html-format-headline-function) + (function* + (lambda (todo todo-type priority text tags + &allow-other-keys) + (funcall org-e-html-format-headline-function + todo todo-type priority text tags)))) + (t 'org-e-html-format-headline)))) + (apply format-function + todo todo-type priority text tags + :headline-label headline-label :level level + :section-number section-number extra-keys))) (defun org-e-html-headline (headline contents info) "Transcode an HEADLINE element from Org to HTML. @@ -2124,20 +2117,12 @@ holding contextual information." (org-element-property :tags headline))) (priority (and (plist-get info :with-priority) (org-element-property :priority headline))) + (section-number (and (org-export-numbered-headline-p headline info) + (mapconcat 'number-to-string + (org-export-get-headline-number + headline info) "."))) ;; Create the headline text. - (full-text (if (functionp org-e-html-format-headline-function) - ;; User-defined formatting function. - (funcall org-e-html-format-headline-function - todo todo-type priority text tags) - ;; Default formatting. - (concat - ;; (when todo - ;; (format "\\textbf{\\textsf{\\textsc{%s}}} " todo)) - (org-e-html-todo todo) " " - (when priority (format "\\framebox{\\#%c} " priority)) - text - ;; (when tags (format "\\hfill{}\\textsc{%s}" tags)) - )))) + (full-text (org-e-html-format-headline--wrap headline info))) (cond ;; Case 1: This is a footnote section: ignore it. ((org-element-property :footnote-section-p headline) nil) @@ -2160,45 +2145,23 @@ holding contextual information." (let* ((extra-class (org-element-property :html-container-class headline)) (extra-ids (list (org-element-property :custom-id headline) (org-element-property :id headline))) + (extra-ids + (mapconcat + (lambda (x) + (when x + (let ((id (org-solidify-link-text + (if (org-uuidgen-p x) (concat "ID-" x) x)))) + (format "" id id)))) + extra-ids "")) (level1 (+ level (1- org-e-html-toplevel-hlevel))) - (title - (concat - ;; extra-ids - (mapconcat - (lambda (x) - (when x - (let ((id (org-solidify-link-text - (if (org-uuidgen-p x) (concat "ID-" x) x)))) - (format "" id id)))) - extra-ids "") - ;; section number - (and (org-export-numbered-headline-p headline info) - (format "%s " - level1 - (mapconcat - 'number-to-string - (org-export-get-headline-number headline info) "."))) - ;; full-text - full-text - ;; tags - (and (plist-get info :with-tags) tags - (concat - "   " - (format "%s" - (mapconcat - (lambda (tag) - (format "%s" - (concat org-e-html-tag-class-prefix - (org-e-html-fix-class-name tag)) - tag)) - (org-split-string tags ":") " ")))))) (id (mapconcat 'number-to-string (org-export-get-headline-number headline info) "-"))) (format "
%s%s
\n" (format "outline-container-%s" id) (concat (format "outline-%d" level1) (and extra-class " ") extra-class) - (format "\n%s\n" level1 id title level1) + (format "\n%s%s\n" + level1 id extra-ids full-text level1) contents)))))) @@ -2240,42 +2203,25 @@ contextual information." "Transcode an INLINETASK element from Org to HTML. CONTENTS holds the contents of the block. INFO is a plist holding contextual information." - (let ((title (org-export-secondary-string - (org-element-property :title inlinetask) 'e-html info)) - (todo (and (plist-get info :with-todo-keywords) - (let ((todo (org-element-property - :todo-keyword inlinetask))) - (and todo - (org-export-secondary-string todo 'e-html info))))) - (todo-type (org-element-property :todo-type inlinetask)) - (tags (and (plist-get info :with-tags) - (org-element-property :tags inlinetask))) - (priority (and (plist-get info :with-priority) - (org-element-property :priority inlinetask)))) - ;; If `org-e-html-format-inlinetask-function' is provided, call it - ;; with appropriate arguments. - (if (functionp org-e-html-format-inlinetask-function) - (funcall org-e-html-format-inlinetask-function - todo todo-type priority title tags contents) - ;; Otherwise, use a default template. - (org-e-html--wrap-label - inlinetask - (let ((full-title - (concat - (when todo (format "\\textbf{\\textsf{\\textsc{%s}}} " todo)) - (when priority (format "\\framebox{\\#%c} " priority)) - title - (when tags (format "\\hfill{}\\textsc{%s}" tags))))) - (format (concat "\\begin{center}\n" - "\\fbox{\n" - "\\begin{minipage}[c]{.6\\textwidth}\n" - "%s\n\n" - "\\rule[.8em]{\\textwidth}{2pt}\n\n" - "%s" - "\\end{minipage}\n" - "}\n" - "\\end{center}") - full-title contents)))))) + (cond + ;; If `org-e-html-format-inlinetask-function' is provided, call it + ;; with appropriate arguments. + ((functionp org-e-html-format-inlinetask-function) + (let ((format-function + (function* + (lambda (todo todo-type priority text tags + &key contents &allow-other-keys) + (funcall org-e-html-format-inlinetask-function + todo todo-type priority text tags contents))))) + (org-e-html-format-headline--wrap + inlinetask info format-function :contents contents))) + ;; Otherwise, use a default template. + (t (org-e-html--wrap-label + inlinetask + (format + "\n
\n%s
\n%s\n
" + (org-e-html-format-headline--wrap inlinetask info) + contents))))) ;;;; Item @@ -2385,8 +2331,8 @@ CONTENTS is nil. INFO is a plist holding contextual information." (org-element-property :caption latex-environment) (org-element-property :name latex-environment) info)) - (label (org-element-property :name latex-environment)) - (attr nil)) + (attr nil) ; FIXME + (label (org-element-property :name latex-environment))) (cond ((member processing-type '(t mathjax)) (org-e-html-format-latex latex-frag 'mathjax)) @@ -2397,8 +2343,7 @@ CONTENTS is nil. INFO is a plist holding contextual information." (string-match "file:\\([^]]*\\)" formula-link)) (org-e-html-format-inline-image (match-string 1 formula-link) caption label attr t)))) - (t - latex-frag))))) + (t latex-frag))))) ;;;; Latex Fragment @@ -2430,14 +2375,18 @@ CONTENTS is nil. INFO is a plist holding contextual information." ;;;; Link -(defun org-e-html-link--inline-image (link info) +(defun org-e-html-link--inline-image (link desc info) "Return HTML code for an inline image. LINK is the link pointing to the inline image. INFO is a plist used as a communication channel." - (let* ((parent (org-export-get-parent-paragraph link info)) - (path (let ((raw-path (org-element-property :path link))) - (if (not (file-name-absolute-p raw-path)) raw-path - (expand-file-name raw-path)))) + (let* ((type (org-element-property :type link)) + (raw-path (org-element-property :path link)) + (path (cond ((member type '("http" "https")) + (concat type ":" raw-path)) + ((file-name-absolute-p raw-path) + (expand-file-name raw-path)) + (t raw-path))) + (parent (org-export-get-parent-paragraph link info)) (caption (org-e-html--caption/label-string (org-element-property :caption parent) (org-element-property :name parent) @@ -2453,9 +2402,8 @@ used as a communication channel." ;; value if nothing is left. (setq attr (if (not attr) "" (org-trim attr))) ;; Return proper string, depending on DISPOSITION. - (let ((href (and label (org-export-solidify-link-text label)))) - (org-e-html-format-inline-image - path caption href attr (org-e-html-standalone-image-p link info))))) + (org-e-html-format-inline-image + path caption label attr (org-e-html-standalone-image-p link info)))) (defvar org-e-html-standalone-image-predicate) (defun org-e-html-standalone-image-p (element info &optional predicate) @@ -2513,8 +2461,6 @@ INFO is a plist holding contextual information. See (raw-path (org-element-property :path link)) ;; Ensure DESC really exists, or set it to nil. (desc (and (not (string= desc "")) desc)) - (imagep (org-export-inline-image-p - link org-e-html-inline-image-rules)) (path (cond ((member type '("http" "https" "ftp" "mailto")) (concat type ":" raw-path)) @@ -2530,7 +2476,10 @@ INFO is a plist holding contextual information. See protocol) (cond ;; Image file. - (imagep (org-e-html-link--inline-image link info)) + ((and (or (eq t org-e-html-inline-images) + (and org-e-html-inline-images (not desc))) + (org-export-inline-image-p link org-e-html-inline-image-rules)) + (org-e-html-link--inline-image link desc info)) ;; Radioed target: Target's name is obtained from original raw ;; link. Path is parsed and transcoded in order to have a proper ;; display of the contents. @@ -2581,7 +2530,7 @@ INFO is a plist holding contextual information. See ((org-e-html-standalone-image-p destination info) (org-export-get-ordinal (assoc 'link (org-element-contents destination)) - info 'link nil 'org-e-html-standalone-image-p)) + info 'link 'org-e-html-standalone-image-p)) (t (org-export-get-ordinal destination info)))) (setq desc (when number (if (atom number) (number-to-string number) @@ -3232,6 +3181,7 @@ Return output file's name." ;;;; org-solidify-link-text ;;;; class for anchors ;;;; org-export-with-section-numbers, body-only +;;;; org-export-mark-todo-in-toc (provide 'org-e-html) ;;; org-e-html.el ends here -- 2.11.4.GIT