ox-koma-letter: Code clean-up
* contrib/lisp/ox-koma-letter.el (org-koma-letter--normalize-string, org-koma-letter--format-string-as-macro): Remove functions. (org-koma-letter--determine-to-and-from, org-koma-letter--special-contents-as-macro): Use `org-trim' instead of `org-koma-letter--normalize-string'. (org-koma-letter--special-contents-as-macro, org-koma-letter--determine-to-and-from, org-koma-letter-headline): Rewrite functions.
This commit is contained in:
parent
bf304c3485
commit
cd7adb47d8
|
@ -413,73 +413,50 @@ return a string or nil."
|
||||||
((symbolp value) (symbol-name value))
|
((symbolp value) (symbol-name value))
|
||||||
(t value))))
|
(t value))))
|
||||||
|
|
||||||
|
(defun org-koma-letter--special-contents-as-macro
|
||||||
|
(keywords &optional keep-newlines no-tag)
|
||||||
|
"Process KEYWORDS members of `org-koma-letter-special-contents'.
|
||||||
|
KEYWORDS is a list of symbols. Return them as a string to be
|
||||||
|
formatted.
|
||||||
|
|
||||||
(defun org-koma-letter--special-contents-as-macro (a-list &optional keep-newlines no-tag)
|
The function is used for inserting content of special headings
|
||||||
"Find members of `org-koma-letter-special-contents' corresponding to A-LIST.
|
|
||||||
Return them as a string to be formatted.
|
|
||||||
|
|
||||||
The function is used for inserting content of speciall headings
|
|
||||||
such as PS.
|
such as PS.
|
||||||
|
|
||||||
If KEEP-NEWLINES is t newlines will not be removed. If NO-TAG is
|
If KEEP-NEWLINES is t newlines will not be removed. If NO-TAG is
|
||||||
is t the content in `org-koma-letter-special-contents' will not
|
t the content in `org-koma-letter-special-contents' will not be
|
||||||
be wrapped in a macro named whatever the members of A-LIST are
|
wrapped in a macro named whatever the members of KEYWORDS are
|
||||||
called."
|
called."
|
||||||
(let (output)
|
(mapconcat
|
||||||
(dolist (ac* a-list output)
|
#'(lambda (keyword)
|
||||||
(let*
|
(let* ((name (org-koma-letter--get-value keyword))
|
||||||
((ac (org-koma-letter--get-value ac*))
|
(value (org-koma-letter--get-tagged-contents name)))
|
||||||
(x (org-koma-letter--get-tagged-contents ac)))
|
(when value
|
||||||
(when x
|
(if no-tag (if keep-newlines value (org-trim value))
|
||||||
(setq output
|
(format "\\%s{%s}\n"
|
||||||
(concat
|
name
|
||||||
output "\n"
|
(if keep-newlines value (org-trim value)))))))
|
||||||
;; sometimes LaTeX complains about newlines
|
keywords
|
||||||
;; at the end or beginning of macros. Remove them.
|
""))
|
||||||
(org-koma-letter--format-string-as-macro
|
|
||||||
(if keep-newlines x (org-koma-letter--normalize-string x))
|
|
||||||
(unless no-tag ac)))))))))
|
|
||||||
|
|
||||||
(defun org-koma-letter--format-string-as-macro (string &optional macro)
|
|
||||||
"Format STRING as \"string\".
|
|
||||||
If optional argument MACRO is provided, format it as
|
|
||||||
\"\\macro{string}\" instead."
|
|
||||||
(if macro
|
|
||||||
(format "\\%s{%s}" macro string)
|
|
||||||
(format "%s" string)))
|
|
||||||
|
|
||||||
(defun org-koma-letter--normalize-string (string)
|
|
||||||
"Remove new lines in the beginning and end of STRING."
|
|
||||||
(replace-regexp-in-string "\\`[ \n\t]+\\|[\n\t ]*\\'" "" string))
|
|
||||||
|
|
||||||
(defun org-koma-letter--determine-to-and-from (info key)
|
(defun org-koma-letter--determine-to-and-from (info key)
|
||||||
"Given INFO determine KEY for the letter.
|
"Given INFO determine KEY for the letter.
|
||||||
KEY should be `to' or `from'.
|
KEY should be `to' or `from'.
|
||||||
|
|
||||||
`ox-koma-letter' allows two ways to specify to and from. If both
|
`ox-koma-letter' allows two ways to specify TO and FROM. If both
|
||||||
are present return the preferred one as determined by
|
are present return the preferred one as determined by
|
||||||
`org-koma-letter-prefer-special-headings'."
|
`org-koma-letter-prefer-special-headings'."
|
||||||
(let* ((plist-alist '((from . :from-address)
|
(let* ((options-value
|
||||||
(to . :to-address)))
|
(plist-get info (if (eq key 'to) :to-address :from-address)))
|
||||||
(default-alist `((from ,org-koma-letter-from-address)
|
(headline-value (org-koma-letter--get-tagged-contents key))
|
||||||
(to "\\mbox{}")))
|
(value (or (if (plist-get info :special-headings)
|
||||||
(option-value (plist-get info (cdr-safe (assoc key plist-alist))))
|
(or headline-value option-value)
|
||||||
(head-value (org-koma-letter--get-tagged-contents key))
|
(or option-value headline-value))
|
||||||
(order (append
|
;; Fallback values.
|
||||||
(funcall
|
(if (eq key 'to) "\\mbox{}" org-koma-letter-from-address))))
|
||||||
(if (plist-get info :special-headings)
|
(and value (replace-regexp-in-string "\n" "\\\\\\\\\n" (org-trim value)))))
|
||||||
'reverse 'identity)
|
|
||||||
`(,option-value ,head-value))
|
|
||||||
(cdr-safe (assoc key default-alist))))
|
|
||||||
tmp
|
|
||||||
(adr (dolist (x order tmp)
|
|
||||||
(when (and (not tmp) x)
|
|
||||||
(setq tmp x)))))
|
|
||||||
(when adr
|
|
||||||
(replace-regexp-in-string
|
|
||||||
"\n" "\\\\\\\\\n"
|
|
||||||
(org-koma-letter--normalize-string adr)))))
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
;;; Transcode Functions
|
;;; Transcode Functions
|
||||||
|
|
||||||
;;;; Export Block
|
;;;; Export Block
|
||||||
|
@ -513,7 +490,6 @@ channel."
|
||||||
(if (equal key "KOMA-LETTER") value
|
(if (equal key "KOMA-LETTER") value
|
||||||
(org-export-with-backend 'latex keyword contents info))))
|
(org-export-with-backend 'latex keyword contents info))))
|
||||||
|
|
||||||
|
|
||||||
;; Headline
|
;; Headline
|
||||||
|
|
||||||
(defun org-koma-letter-headline (headline contents info)
|
(defun org-koma-letter-headline (headline contents info)
|
||||||
|
@ -525,24 +501,19 @@ Note that if a headline is tagged with a tag from
|
||||||
`org-koma-letter-special-tags' it will not be exported, but
|
`org-koma-letter-special-tags' it will not be exported, but
|
||||||
stored in `org-koma-letter-special-contents' and included at the
|
stored in `org-koma-letter-special-contents' and included at the
|
||||||
appropriate place."
|
appropriate place."
|
||||||
(let*
|
(unless (let ((tag (car (org-export-get-tags headline info))))
|
||||||
((tags (org-export-get-tags headline info))
|
(and tag
|
||||||
(tag* (car tags))
|
(member-ignore-case
|
||||||
(tag (when tag*
|
tag (mapcar #'symbol-name (plist-get info :special-tags)))
|
||||||
(car (member-ignore-case
|
;; Store association for later use and bail out.
|
||||||
tag*
|
(push (cons tag contents) org-koma-letter-special-contents)))
|
||||||
(mapcar 'symbol-name (plist-get info :special-tags)))))))
|
;; Opening is not defined yet: use headline's title.
|
||||||
(if tag
|
(when (and org-koma-letter-headline-is-opening-maybe
|
||||||
(progn
|
(not (plist-get info :opening)))
|
||||||
(push (cons tag contents)
|
(plist-put info :opening
|
||||||
org-koma-letter-special-contents)
|
(org-export-data (org-element-property :title headline) info)))
|
||||||
nil)
|
;; In any case, insert contents in letter's body.
|
||||||
(unless (or (plist-get info :opening)
|
contents))
|
||||||
(not org-koma-letter-headline-is-opening-maybe))
|
|
||||||
(plist-put info :opening
|
|
||||||
(org-export-data (org-element-property :title headline) info)))
|
|
||||||
contents)))
|
|
||||||
|
|
||||||
|
|
||||||
;;;; Template
|
;;;; Template
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue