contrib/lisp/org-e-texinfo: Ensure valid export of escaped characters

and remove invalid characters from menu items

* contrib/lisp/org-e-texinfo.el (org-e-texinfo--sanitize-menu): Remove
  invalid characters from menu entries and nodes.
(org-e-texinfo--sanitize-contents): Ensure @ { and } are properly
escaped in text and headlines.
(org-e-texinfo--generate-menu-items): Strip invalid characters from
menu titles.
(org-e-texinfo-fixed-width): Escape @ { and } in fixed-width
environments.
(org-e-texinfo-headline): Ensure nodes do not have invalid characters
and escape @ { and } in section titles.
(org-e-texinfo-plain-text): Use proper escape character for @ { and }.
This commit is contained in:
Jonathan Leech-Pepin 2012-08-23 10:50:24 -04:00
parent 8441782b11
commit d4d29efb53

View file

@ -526,6 +526,24 @@ retrieved."
(org-e-texinfo--sanitize-headline-contents (org-e-texinfo--sanitize-headline-contents
(org-element-contents contents) info))))) (org-element-contents contents) info)))))
;;; Menu sanitizing
(defun org-e-texinfo--sanitize-menu (title)
"Remove invalid characters from TITLE for use in menus and
nodes.
Based on TEXINFO specifications, the following must be removed:
@ { } ( ) : . ,"
(replace-regexp-in-string "[@{}():,.]" "" title))
;;; Content sanitizing
(defun org-e-texinfo--sanitize-content (text)
"Ensure characters are properly escaped when used in headlines or blocks.
Escape characters are: @ { }"
(replace-regexp-in-string "\\\([@{}]\\\)" "@\\1" text))
;;; Menu creation ;;; Menu creation
(defun org-e-texinfo--build-menu (tree level info &optional detailed) (defun org-e-texinfo--build-menu (tree level info &optional detailed)
@ -607,8 +625,9 @@ Returns a list containing the following information from each
headline: length, title, description. This is used to format the headline: length, title, description. This is used to format the
menu using `org-e-texinfo--format-menu'." menu using `org-e-texinfo--format-menu'."
(loop for headline in items collect (loop for headline in items collect
(let* ((title (org-e-texinfo--sanitize-headline (let* ((title (org-e-texinfo--sanitize-menu
(org-element-property :title headline) info)) (org-e-texinfo--sanitize-headline
(org-element-property :title headline) info)))
(descr (org-export-data (descr (org-export-data
(org-element-property :description headline) info)) (org-element-property :description headline) info))
(len (length title)) (len (length title))
@ -904,7 +923,8 @@ CONTENTS is nil. INFO is a plist holding contextual information."
CONTENTS is nil. INFO is a plist holding contextual information." CONTENTS is nil. INFO is a plist holding contextual information."
(format "@example\n%s\n@end example" (format "@example\n%s\n@end example"
(org-remove-indentation (org-remove-indentation
(org-element-property :value fixed-width)))) (org-e-texinfo--sanitize-content
(org-element-property :value fixed-width)))))
;;; Footnote Definition ;;; Footnote Definition
;; ;;
@ -939,7 +959,8 @@ holding contextual information."
(org-element-property :title headline) info)) (org-element-property :title headline) info))
;; Create node info, to insert it before section formatting. ;; Create node info, to insert it before section formatting.
(node (format "@node %s\n" (node (format "@node %s\n"
(replace-regexp-in-string "%" "%%" text))) (org-e-texinfo--sanitize-menu
(replace-regexp-in-string "%" "%%" text))))
;; Menus must be generated with first child, otherwise they ;; Menus must be generated with first child, otherwise they
;; will not nest properly ;; will not nest properly
(menu (let* ((first (org-export-first-sibling-p headline info)) (menu (let* ((first (org-export-first-sibling-p headline info))
@ -995,29 +1016,31 @@ holding contextual information."
(org-element-property :priority headline))) (org-element-property :priority headline)))
;; Create the headline text along with a no-tag version. The ;; Create the headline text along with a no-tag version. The
;; latter is required to remove tags from table of contents. ;; latter is required to remove tags from table of contents.
(full-text (if (functionp org-e-texinfo-format-headline-function) (full-text (org-e-texinfo--sanitize-content
;; User-defined formatting function. (if (functionp org-e-texinfo-format-headline-function)
(funcall org-e-texinfo-format-headline-function ;; User-defined formatting function.
todo todo-type priority text tags) (funcall org-e-texinfo-format-headline-function
;; Default formatting. todo todo-type priority text tags)
(concat ;; Default formatting.
(when todo (concat
(format "@strong{%s} " todo)) (when todo
(when priority (format "@emph{#%s} " priority)) (format "@strong{%s} " todo))
text (when priority (format "@emph{#%s} " priority))
(when tags text
(format ":%s:" (when tags
(mapconcat 'identity tags ":")))))) (format ":%s:"
(mapconcat 'identity tags ":")))))))
(full-text-no-tag (full-text-no-tag
(if (functionp org-e-texinfo-format-headline-function) (org-e-texinfo--sanitize-content
;; User-defined formatting function. (if (functionp org-e-texinfo-format-headline-function)
(funcall org-e-texinfo-format-headline-function ;; User-defined formatting function.
todo todo-type priority text nil) (funcall org-e-texinfo-format-headline-function
;; Default formatting. todo todo-type priority text nil)
(concat ;; Default formatting.
(when todo (format "@strong{%s} " todo)) (concat
(when priority (format "@emph{#%c} " priority)) (when todo (format "@strong{%s} " todo))
text))) (when priority (format "@emph{#%c} " priority))
text))))
(pre-blanks (pre-blanks
(make-string (org-element-property :pre-blank headline) 10))) (make-string (org-element-property :pre-blank headline) 10)))
(cond (cond
@ -1335,10 +1358,6 @@ contextual information."
"Transcode a TEXT string from Org to Texinfo. "Transcode a TEXT string from Org to Texinfo.
TEXT is the string to transcode. INFO is a plist holding TEXT is the string to transcode. INFO is a plist holding
contextual information." contextual information."
;; Protect @ { and }.
(while (string-match "\\([^\\]\\|^\\)\\([@{}]\\)" text)
(setq text
(replace-match (format "\\%s" (match-string 2 text)) nil t text 2)))
;; LaTeX into @LaTeX{} and TeX into @TeX{} ;; LaTeX into @LaTeX{} and TeX into @TeX{}
(let ((case-fold-search nil) (let ((case-fold-search nil)
(start 0)) (start 0))
@ -1356,8 +1375,8 @@ contextual information."
(when (plist-get info :preserve-breaks) (when (plist-get info :preserve-breaks)
(setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n" (setq text (replace-regexp-in-string "\\(\\\\\\\\\\)?[ \t]*\n" " @*\n"
text))) text)))
;; Return value. ;; Return value with @ { and } protected.
text) (org-e-texinfo--sanitize-content text))
;;; Planning ;;; Planning