org-lparse.el 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190
  1. ;;; org-lparse.el --- Line-oriented parser-exporter for Org-mode
  2. ;; Copyright (C) 2010, 2011
  3. ;; Jambunathan <kjambunathan at gmail dot com>
  4. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  5. ;; Keywords: outlines, hypermedia, calendar, wp
  6. ;; Homepage: http://orgmode.org
  7. ;; Version: 0.8
  8. ;; This file is not (yet) part of GNU Emacs.
  9. ;; However, it is distributed under the same license.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  21. ;;
  22. ;;; Commentary:
  23. ;; `org-lparse' is the entry point for the generic line-oriented
  24. ;; exporter. `org-do-lparse' is the genericized version of the
  25. ;; original `org-export-as-html' routine.
  26. ;; `org-lparse-native-backends' is a good starting point for
  27. ;; exploring the generic exporter.
  28. ;; Following new interactive commands are provided by this library.
  29. ;; `org-lparse', `org-lparse-and-open', `org-lparse-to-buffer'
  30. ;; `org-replace-region-by', `org-lparse-region'.
  31. ;; Note that the above routines correspond to the following routines
  32. ;; in the html exporter `org-export-as-html',
  33. ;; `org-export-as-html-and-open', `org-export-as-html-to-buffer',
  34. ;; `org-replace-region-by-html' and `org-export-region-as-html'.
  35. ;; The new interactive command `org-lparse-convert' can be used to
  36. ;; convert documents between various formats. Use this to command,
  37. ;; for example, to convert odt file to doc or pdf format.
  38. ;; See README.org file that comes with this library for answers to
  39. ;; FAQs and more information on using this library.
  40. ;;; Code:
  41. (require 'org-exp)
  42. (require 'org-list)
  43. ;;;###autoload
  44. (defun org-lparse-and-open (target-backend native-backend arg)
  45. "Export outline to TARGET-BACKEND via NATIVE-BACKEND and open exported file.
  46. If there is an active region, export only the region. The prefix
  47. ARG specifies how many levels of the outline should become
  48. headlines. The default is 3. Lower levels will become bulleted
  49. lists."
  50. ;; (interactive "Mbackend: \nP")
  51. (interactive
  52. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  53. (all-backends (org-lparse-all-backends))
  54. (target-backend
  55. (funcall input "Export to: " all-backends nil t nil))
  56. (native-backend
  57. (or
  58. ;; (and (org-lparse-backend-is-native-p target-backend)
  59. ;; target-backend)
  60. (funcall input "Use Native backend: "
  61. (cdr (assoc target-backend all-backends)) nil t nil))))
  62. (list target-backend native-backend current-prefix-arg)))
  63. (let (f (file-or-buf (org-lparse target-backend native-backend
  64. arg 'hidden)))
  65. (when file-or-buf
  66. (setq f (cond
  67. ((bufferp file-or-buf) buffer-file-name)
  68. ((file-exists-p file-or-buf) file-or-buf)
  69. (t (error "org-lparse-and-open: This shouldn't happen"))))
  70. (message "Opening file %s" f)
  71. (org-open-file f)
  72. (when org-export-kill-product-buffer-when-displayed
  73. (kill-buffer (current-buffer))))))
  74. ;;;###autoload
  75. (defun org-lparse-batch (target-backend &optional native-backend)
  76. "Call the function `org-lparse'.
  77. This function can be used in batch processing as:
  78. emacs --batch
  79. --load=$HOME/lib/emacs/org.el
  80. --eval \"(setq org-export-headline-levels 2)\"
  81. --visit=MyFile --funcall org-lparse-batch"
  82. (setq native-backend (or native-backend target-backend))
  83. (org-lparse target-backend native-backend
  84. org-export-headline-levels 'hidden))
  85. ;;;###autoload
  86. (defun org-lparse-to-buffer (backend arg)
  87. "Call `org-lparse' with output to a temporary buffer.
  88. No file is created. The prefix ARG is passed through to
  89. `org-lparse'."
  90. (interactive "Mbackend: \nP")
  91. (let ((tempbuf (format "*Org %s Export*" (upcase backend))))
  92. (org-lparse backend backend arg nil nil tempbuf)
  93. (when org-export-show-temporary-export-buffer
  94. (switch-to-buffer-other-window tempbuf))))
  95. ;;;###autoload
  96. (defun org-replace-region-by (backend beg end)
  97. "Assume the current region has org-mode syntax, and convert it to HTML.
  98. This can be used in any buffer. For example, you could write an
  99. itemized list in org-mode syntax in an HTML buffer and then use this
  100. command to convert it."
  101. (interactive "Mbackend: \nr")
  102. (let (reg backend-string buf pop-up-frames)
  103. (save-window-excursion
  104. (if (org-mode-p)
  105. (setq backend-string (org-lparse-region backend beg end t 'string))
  106. (setq reg (buffer-substring beg end)
  107. buf (get-buffer-create "*Org tmp*"))
  108. (with-current-buffer buf
  109. (erase-buffer)
  110. (insert reg)
  111. (org-mode)
  112. (setq backend-string (org-lparse-region backend (point-min)
  113. (point-max) t 'string)))
  114. (kill-buffer buf)))
  115. (delete-region beg end)
  116. (insert backend-string)))
  117. ;;;###autoload
  118. (defun org-lparse-region (backend beg end &optional body-only buffer)
  119. "Convert region from BEG to END in org-mode buffer to HTML.
  120. If prefix arg BODY-ONLY is set, omit file header, footer, and table of
  121. contents, and only produce the region of converted text, useful for
  122. cut-and-paste operations.
  123. If BUFFER is a buffer or a string, use/create that buffer as a target
  124. of the converted HTML. If BUFFER is the symbol `string', return the
  125. produced HTML as a string and leave not buffer behind. For example,
  126. a Lisp program could call this function in the following way:
  127. (setq html (org-lparse-region \"html\" beg end t 'string))
  128. When called interactively, the output buffer is selected, and shown
  129. in a window. A non-interactive call will only return the buffer."
  130. (interactive "Mbackend: \nr\nP")
  131. (when (org-called-interactively-p 'any)
  132. (setq buffer (format "*Org %s Export*" (upcase backend))))
  133. (let ((transient-mark-mode t) (zmacs-regions t)
  134. ext-plist rtn)
  135. (setq ext-plist (plist-put ext-plist :ignore-subtree-p t))
  136. (goto-char end)
  137. (set-mark (point)) ;; to activate the region
  138. (goto-char beg)
  139. (setq rtn (org-lparse backend backend nil nil ext-plist buffer body-only))
  140. (if (fboundp 'deactivate-mark) (deactivate-mark))
  141. (if (and (org-called-interactively-p 'any) (bufferp rtn))
  142. (switch-to-buffer-other-window rtn)
  143. rtn)))
  144. (defvar org-lparse-par-open nil)
  145. (defun org-lparse-should-inline-p (filename descp)
  146. "Return non-nil if link FILENAME should be inlined.
  147. The decision to inline the FILENAME link is based on the current
  148. settings. DESCP is the boolean of whether there was a link
  149. description. See variables `org-export-html-inline-images' and
  150. `org-export-html-inline-image-extensions'."
  151. (let ((inline-images (org-lparse-get 'INLINE-IMAGES))
  152. (inline-image-extensions
  153. (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
  154. (and (or (eq t inline-images) (and inline-images (not descp)))
  155. (org-file-image-p filename inline-image-extensions))))
  156. (defun org-lparse-format-org-link (line opt-plist)
  157. "Return LINE with markup of Org mode links.
  158. OPT-PLIST is the export options list."
  159. (let ((start 0)
  160. (current-dir (if buffer-file-name
  161. (file-name-directory buffer-file-name)
  162. default-directory))
  163. (link-validate (plist-get opt-plist :link-validation-function))
  164. type id-file fnc
  165. rpl path attr desc descp desc1 desc2 link
  166. org-lparse-link-description-is-image)
  167. (while (string-match org-bracket-link-analytic-regexp++ line start)
  168. (setq org-lparse-link-description-is-image nil)
  169. (setq start (match-beginning 0))
  170. (setq path (save-match-data (org-link-unescape
  171. (match-string 3 line))))
  172. (setq type (cond
  173. ((match-end 2) (match-string 2 line))
  174. ((save-match-data
  175. (or (file-name-absolute-p path)
  176. (string-match "^\\.\\.?/" path)))
  177. "file")
  178. (t "internal")))
  179. (setq path (org-extract-attributes (org-link-unescape path)))
  180. (setq attr (get-text-property 0 'org-attributes path))
  181. (setq desc1 (if (match-end 5) (match-string 5 line))
  182. desc2 (if (match-end 2) (concat type ":" path) path)
  183. descp (and desc1 (not (equal desc1 desc2)))
  184. desc (or desc1 desc2))
  185. ;; Make an image out of the description if that is so wanted
  186. (when (and descp (org-file-image-p
  187. desc (org-lparse-get 'INLINE-IMAGE-EXTENSIONS)))
  188. (setq org-lparse-link-description-is-image t)
  189. (save-match-data
  190. (if (string-match "^file:" desc)
  191. (setq desc (substring desc (match-end 0)))))
  192. (save-match-data
  193. (setq desc (org-add-props
  194. (org-lparse-format 'INLINE-IMAGE desc)
  195. '(org-protected t)))))
  196. (cond
  197. ((equal type "internal")
  198. (let
  199. ((frag-0
  200. (if (= (string-to-char path) ?#)
  201. (substring path 1)
  202. path)))
  203. (setq rpl
  204. (org-lparse-format
  205. 'ORG-LINK opt-plist "" "" (org-solidify-link-text
  206. (save-match-data
  207. (org-link-unescape frag-0))
  208. nil) desc attr descp))))
  209. ((and (equal type "id")
  210. (setq id-file (org-id-find-id-file path)))
  211. ;; This is an id: link to another file (if it was the same file,
  212. ;; it would have become an internal link...)
  213. (save-match-data
  214. (setq id-file (file-relative-name
  215. id-file
  216. (file-name-directory org-current-export-file)))
  217. (setq rpl
  218. (org-lparse-format
  219. 'ORG-LINK opt-plist type id-file
  220. (concat (if (org-uuidgen-p path) "ID-") path)
  221. desc attr descp))))
  222. ((member type '("http" "https"))
  223. ;; standard URL, can inline as image
  224. (setq rpl
  225. (org-lparse-format
  226. 'ORG-LINK opt-plist type path nil desc attr descp)))
  227. ((member type '("ftp" "mailto" "news"))
  228. ;; standard URL, can't inline as image
  229. (setq rpl
  230. (org-lparse-format
  231. 'ORG-LINK opt-plist type path nil desc attr descp)))
  232. ((string= type "coderef")
  233. (setq rpl
  234. (org-lparse-format
  235. 'ORG-LINK opt-plist type "" (format "coderef-%s" path)
  236. (format
  237. (org-export-get-coderef-format
  238. path
  239. (and descp desc))
  240. (cdr (assoc path org-export-code-refs))) nil descp)))
  241. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  242. ;; The link protocol has a function for format the link
  243. (setq rpl
  244. (save-match-data
  245. (funcall fnc (org-link-unescape path) desc1 'html))))
  246. ((string= type "file")
  247. ;; FILE link
  248. (save-match-data
  249. (let*
  250. ((components
  251. (if
  252. (string-match "::\\(.*\\)" path)
  253. (list
  254. (replace-match "" t nil path)
  255. (match-string 1 path))
  256. (list path nil)))
  257. ;;The proper path, without a fragment
  258. (path-1
  259. (first components))
  260. ;;The raw fragment
  261. (fragment-0
  262. (second components))
  263. ;;Check the fragment. If it can't be used as
  264. ;;target fragment we'll pass nil instead.
  265. (fragment-1
  266. (if
  267. (and fragment-0
  268. (not (string-match "^[0-9]*$" fragment-0))
  269. (not (string-match "^\\*" fragment-0))
  270. (not (string-match "^/.*/$" fragment-0)))
  271. (org-solidify-link-text
  272. (org-link-unescape fragment-0))
  273. nil))
  274. (desc-2
  275. ;;Description minus "file:" and ".org"
  276. (if (string-match "^file:" desc)
  277. (let
  278. ((desc-1 (replace-match "" t t desc)))
  279. (if (string-match "\\.org$" desc-1)
  280. (replace-match "" t t desc-1)
  281. desc-1))
  282. desc)))
  283. (setq rpl
  284. (if
  285. (and
  286. (functionp link-validate)
  287. (not (funcall link-validate path-1 current-dir)))
  288. desc
  289. (org-lparse-format
  290. 'ORG-LINK opt-plist "file" path-1 fragment-1
  291. desc-2 attr descp))))))
  292. (t
  293. ;; just publish the path, as default
  294. (setq rpl (concat "<i>&lt;" type ":"
  295. (save-match-data (org-link-unescape path))
  296. "&gt;</i>"))))
  297. (setq line (replace-match rpl t t line)
  298. start (+ start (length rpl))))
  299. line))
  300. (defmacro with-org-lparse-preserve-paragraph-state (&rest body)
  301. `(let ((org-lparse-do-open-par org-lparse-par-open))
  302. (org-lparse-end-paragraph)
  303. ,@body
  304. (when org-lparse-do-open-par
  305. (org-lparse-begin-paragraph))))
  306. (defvar org-lparse-native-backends nil
  307. "List of native backends registered with `org-lparse'.
  308. A backend can use `org-lparse-register-backend' to add itself to
  309. this list.
  310. All native backends must implement a get routine and a mandatory
  311. set of callback routines.
  312. The get routine must be named as org-<backend>-get where backend
  313. is the name of the backend. The exporter uses `org-lparse-get'
  314. and retrieves the backend-specific callback by querying for
  315. ENTITY-CONTROL and ENTITY-FORMAT variables.
  316. For the sake of illustration, the html backend implements
  317. `org-xhtml-get'. It returns
  318. `org-xhtml-entity-control-callbacks-alist' and
  319. `org-xhtml-entity-format-callbacks-alist' as the values of
  320. ENTITY-CONTROL and ENTITY-FORMAT settings.")
  321. (defun org-lparse-register-backend (backend)
  322. "Make BACKEND known to `org-lparse' library.
  323. Add BACKEND to `org-lparse-native-backends'."
  324. (when backend
  325. (setq backend (cond
  326. ((symbolp backend) (symbol-name backend))
  327. ((stringp backend) backend)
  328. (t (error "Error while registering backend: %S" backend))))
  329. (add-to-list 'org-lparse-native-backends backend)))
  330. (defun org-lparse-unregister-backend (backend)
  331. (setq org-lparse-native-backends
  332. (remove (cond
  333. ((symbolp backend) (symbol-name backend))
  334. ((stringp backend) backend))
  335. org-lparse-native-backends))
  336. (message "Unregistered backend %S" backend))
  337. (defun org-lparse-get-other-backends (in-fmt)
  338. "Return OUTPUT-FMT-ALIST corresponding to IN-FMT.
  339. See `org-lparse-convert-capabilities' for definition of
  340. OUTPUT-FMT-ALIST."
  341. (when (org-lparse-get-converter in-fmt)
  342. (or (ignore-errors (org-lparse-backend-get in-fmt 'OTHER-BACKENDS) )
  343. (catch 'out-fmts
  344. (dolist (c org-lparse-convert-capabilities)
  345. (when (member in-fmt (nth 1 c))
  346. (throw 'out-fmts (nth 2 c))))))))
  347. (defun org-lparse-get-converter (in-fmt)
  348. "Return converter associated with IN-FMT.
  349. See `org-lparse-convert-capabilities' for further information."
  350. (or (ignore-errors (org-lparse-backend-get in-fmt 'CONVERT-METHOD))
  351. org-lparse-convert-process))
  352. (defun org-lparse-all-backends ()
  353. "Return all formats to which `org-lparse' could export to.
  354. The return value is an alist of the form (TARGET-BACKEND
  355. NATIVE-BACKEND-1 NATIVE-BACKEND-2 ...) with the condition that
  356. the org file can be exported to TARGET-BACKEND via any one of
  357. NATIVE-BACKEND-1, NATIVE-BACKEND-2 etc.
  358. For example, an entry of the form \"(\"pdf\" \"odt\" \"xhtml\")\"
  359. would mean that the org file could be exported to \"pdf\" format
  360. by exporting natively either to \"xhtml\" or \"odt\" backends."
  361. (let (all-backends)
  362. (flet ((add (other native)
  363. (let ((val (assoc-string other all-backends t)))
  364. (if val (setcdr val (nconc (list native) (cdr val)))
  365. (push (cons other (list native)) all-backends)))))
  366. (loop for backend in org-lparse-native-backends
  367. do (loop for other in
  368. (mapcar #'car (org-lparse-get-other-backends backend))
  369. do (add other backend))))
  370. all-backends))
  371. (defun org-lparse-backend-is-native-p (backend)
  372. (member backend org-lparse-native-backends))
  373. (defun org-lparse (target-backend native-backend arg
  374. &optional hidden ext-plist
  375. to-buffer body-only pub-dir)
  376. "Export the outline to various formats.
  377. If there is an active region, export only the region. The outline
  378. is first exported to NATIVE-BACKEND and optionally converted to
  379. TARGET-BACKEND. See `org-lparse-native-backends' for list of
  380. known native backends. Each native backend can specify a
  381. converter and list of target backends it exports to using the
  382. CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
  383. method. See `org-xhtml-get' for an illustrative example.
  384. ARG is a prefix argument that specifies how many levels of
  385. outline should become headlines. The default is 3. Lower levels
  386. will become bulleted lists.
  387. HIDDEN is obsolete and does nothing.
  388. EXT-PLIST is a property list that controls various aspects of
  389. export. The settings here override org-mode's default settings
  390. and but are inferior to file-local settings.
  391. TO-BUFFER dumps the exported lines to a buffer or a string
  392. instead of a file. If TO-BUFFER is the symbol `string' return the
  393. exported lines as a string. If TO-BUFFER is non-nil, create a
  394. buffer with that name and export to that buffer.
  395. BODY-ONLY controls the presence of header and footer lines in
  396. exported text. If BODY-ONLY is non-nil, don't produce the file
  397. header and footer, simply return the content of <body>...</body>,
  398. without even the body tags themselves.
  399. PUB-DIR specifies the publishing directory."
  400. (interactive
  401. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  402. (all-backends (org-lparse-all-backends))
  403. (target-backend
  404. (and all-backends
  405. (funcall input "Export to: " all-backends nil t nil)))
  406. (native-backend
  407. (let ((choices (if target-backend
  408. (cdr (assoc target-backend all-backends))
  409. (or org-lparse-native-backends
  410. (error "No registered backends")))))
  411. (funcall input "Use Native backend: " choices nil t nil))))
  412. (list target-backend native-backend current-prefix-arg)))
  413. (let* ((org-lparse-backend (intern native-backend))
  414. (org-lparse-other-backend (and target-backend
  415. (intern target-backend))))
  416. (unless (org-lparse-backend-is-native-p native-backend)
  417. (error "Don't know how to export natively to backend %s" native-backend))
  418. (unless (or (not target-backend)
  419. (equal target-backend native-backend)
  420. (assoc target-backend (org-lparse-get-other-backends
  421. native-backend)))
  422. (error "Don't know how to export to backend %s %s" target-backend
  423. (format "via %s" native-backend)))
  424. (run-hooks 'org-export-first-hook)
  425. (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir)))
  426. (defcustom org-lparse-convert-processes
  427. '(("BasicODConverter"
  428. ("soffice" "-norestore" "-invisible" "-headless"
  429. "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
  430. ("unoconv"
  431. ("unoconv" "-f" "%f" "-o" "%d" "%i")))
  432. "Specify a list of document converters and their usage.
  433. The converters in this list are offered as choices while
  434. customizing `org-lparse-convert-process'.
  435. This variable is an alist where each element is of the
  436. form (CONVERTER-NAME (CONVERTER-PROGRAM ARG1 ARG2 ...)).
  437. CONVERTER-NAME is name of the converter. CONVERTER-PROGRAM is
  438. the name of the executable. ARG1, ARG2 etc are command line
  439. options that are passed to CONVERTER-PROGRAM. Format specifiers
  440. can be used in the ARGs and they are interpreted as below:
  441. %i input file name in full
  442. %I input file name as a URL
  443. %f format of the output file
  444. %o output file name in full
  445. %O output file name as a URL
  446. %d output dir in full
  447. %D output dir as a URL."
  448. :group 'org-lparse
  449. :type
  450. '(choice
  451. (const :tag "None" nil)
  452. (alist :tag "Converters"
  453. :key-type (string :tag "Converter Name")
  454. :value-type (group (cons (string :tag "Executable")
  455. (repeat (string :tag "Command line args")))))))
  456. (defcustom org-lparse-convert-process nil
  457. "Command to convert from an Org exported format to other formats.
  458. During customization, the list of choices are populated from
  459. `org-lparse-convert-processes'. Refer afore-mentioned variable
  460. for further information."
  461. :group 'org-lparse
  462. :type '(choice :convert-widget
  463. (lambda (w)
  464. (apply 'widget-convert (widget-type w)
  465. (eval (car (widget-get w :args)))))
  466. `((const :tag "None" nil)
  467. ,@(mapcar (lambda (c)
  468. `(const :tag ,(car c) ,(cadr c)))
  469. org-lparse-convert-processes))))
  470. (defcustom org-lparse-convert-capabilities
  471. '(("Text"
  472. ("odt" "ott" "doc" "rtf")
  473. (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
  474. ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
  475. ("Web"
  476. ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
  477. ("Spreadsheet"
  478. ("ods" "ots" "xls" "csv")
  479. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
  480. ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
  481. ("Presentation"
  482. ("odp" "otp" "ppt")
  483. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
  484. ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
  485. "Specify input and output formats of `org-lparse-convert-process'.
  486. More correctly, specify the set of input and output formats that
  487. the user is actually interested in.
  488. This variable is an alist where each element is of the
  489. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  490. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  491. alist where each element is of the form (OUTPUT-FMT
  492. OUTPUT-FILE-EXTENSION).
  493. The variable is interpreted as follows:
  494. `org-lparse-convert-process' can take any document that is in
  495. INPUT-FMT-LIST and produce any document that is in the
  496. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  497. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  498. serves dual purposes:
  499. - It is used for populating completion candidates during
  500. `org-lparse' and `org-lparse-convert' commands.
  501. - It is used as the value of \"%f\" specifier in
  502. `org-lparse-convert-process'.
  503. DOCUMENT-CLASS is used to group a set of file formats in
  504. INPUT-FMT-LIST in to a single class.
  505. Note that this variable inherently captures how LibreOffice based
  506. converters work. LibreOffice maps documents of various formats
  507. to classes like Text, Web, Spreadsheet, Presentation etc and
  508. allow document of a given class (irrespective of it's source
  509. format) to be converted to any of the export formats associated
  510. with that class.
  511. See default setting of this variable for an typical
  512. configuration."
  513. :group 'org-lparse
  514. :type
  515. '(choice
  516. (const :tag "None" nil)
  517. (alist :key-type (string :tag "Document Class")
  518. :value-type
  519. (group (repeat :tag "Input formats" (string :tag "Input format"))
  520. (alist :tag "Output formats"
  521. :key-type (string :tag "Output format")
  522. :value-type
  523. (group (string :tag "Output file extension")))))))
  524. (defcustom org-lparse-use-flashy-warning nil
  525. "Control flashing of messages logged with `org-lparse-warn'.
  526. When non-nil, messages are fontified with warning face and the
  527. exporter lingers for a while to catch user's attention."
  528. :type 'boolean
  529. :group 'org-lparse)
  530. (defun org-lparse-convert (&optional in-file out-fmt prefix-arg)
  531. "Convert IN-FILE to format OUT-FMT using a command line converter.
  532. IN-FILE is the file to be converted. If unspecified, it defaults
  533. to variable `buffer-file-name'. OUT-FMT is the desired output
  534. format. If the backend has registered a CONVERT-METHOD as part
  535. of it's get function then that converter is used. Otherwise
  536. `org-lparse-convert-process' is used. If PREFIX-ARG is non-nil
  537. then the newly converted file is opened using `org-open-file'."
  538. (interactive
  539. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  540. (in-file (read-file-name "File to be converted: "
  541. nil buffer-file-name t))
  542. (in-fmt (file-name-extension in-file))
  543. (out-fmt-choices (org-lparse-get-other-backends in-fmt))
  544. (out-fmt
  545. (or (and out-fmt-choices
  546. (funcall input "Output format: "
  547. out-fmt-choices nil nil nil))
  548. (error
  549. "No known converter or no known output formats for %s files"
  550. in-fmt))))
  551. (list in-file out-fmt current-prefix-arg)))
  552. (require 'browse-url)
  553. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  554. (dummy (or (file-readable-p in-file)
  555. (error "Cannot read %s" in-file)))
  556. (in-fmt (file-name-extension in-file))
  557. (out-fmt (or out-fmt (error "Output format unspecified")))
  558. (convert-process (org-lparse-get-converter in-fmt))
  559. (program (car convert-process))
  560. (dummy (and (or program (error "Converter not configured"))
  561. (or (executable-find program)
  562. (error "Cannot find converter %s" program))))
  563. (out-fmt-alist
  564. (or (assoc out-fmt (org-lparse-get-other-backends in-fmt))
  565. (error "Cannot convert from %s to %s format" in-fmt out-fmt)))
  566. (out-file (concat (file-name-sans-extension in-file) "."
  567. (nth 1 out-fmt-alist)))
  568. (out-dir (file-name-directory in-file))
  569. (arglist (mapcar (lambda (arg)
  570. (format-spec
  571. arg `((?i . ,in-file)
  572. (?I . ,(browse-url-file-url in-file))
  573. (?f . ,out-fmt)
  574. (?o . ,out-file)
  575. (?O . ,(browse-url-file-url out-file))
  576. (?d . ,out-dir)
  577. (?D . ,(browse-url-file-url out-dir)))))
  578. (cdr convert-process))))
  579. (when (file-exists-p out-file)
  580. (delete-file out-file))
  581. (message "Executing %s %s" program (mapconcat 'identity arglist " "))
  582. (apply 'call-process program nil nil nil arglist)
  583. (cond
  584. ((file-exists-p out-file)
  585. (message "Exported to %s using %s" out-file program)
  586. (when prefix-arg
  587. (message "Opening %s..." out-file)
  588. (org-open-file out-file))
  589. out-file
  590. ;; (set-buffer (find-file-noselect out-file))
  591. )
  592. (t
  593. (message "Export to %s failed" out-file)
  594. nil))))
  595. (defvar org-lparse-insert-tag-with-newlines 'both)
  596. ;; Following variables are let-bound during `org-lparse'
  597. (defvar org-lparse-dyn-first-heading-pos)
  598. (defvar org-lparse-toc)
  599. (defvar org-lparse-entity-control-callbacks-alist)
  600. (defvar org-lparse-entity-format-callbacks-alist)
  601. (defvar org-lparse-backend nil
  602. "The native backend to which the document is currently exported.
  603. This variable is let bound during `org-lparse'. Valid values are
  604. one of the symbols corresponding to `org-lparse-native-backends'.
  605. Compare this variable with `org-export-current-backend' which is
  606. bound only during `org-export-preprocess-string' stage of the
  607. export process.
  608. See also `org-lparse-other-backend'.")
  609. (defvar org-lparse-other-backend nil
  610. "The target backend to which the document is currently exported.
  611. This variable is let bound during `org-lparse'. This variable is
  612. set to either `org-lparse-backend' or one of the symbols
  613. corresponding to OTHER-BACKENDS specification of the
  614. org-lparse-backend.
  615. For example, if a document is exported to \"odt\" then both
  616. org-lparse-backend and org-lparse-other-backend are bound to
  617. 'odt. On the other hand, if a document is exported to \"odt\"
  618. and then converted to \"doc\" then org-lparse-backend is set to
  619. 'odt and org-lparse-other-backend is set to 'doc.")
  620. (defvar org-lparse-body-only nil
  621. "Bind this to BODY-ONLY arg of `org-lparse'.")
  622. (defvar org-lparse-to-buffer nil
  623. "Bind this to TO-BUFFER arg of `org-lparse'.")
  624. (defun org-do-lparse (arg &optional hidden ext-plist
  625. to-buffer body-only pub-dir)
  626. "Export the outline to various formats.
  627. See `org-lparse' for more information. This function is a
  628. html-agnostic version of the `org-export-as-html' function in 7.5
  629. version."
  630. ;; Make sure we have a file name when we need it.
  631. (when (and (not (or to-buffer body-only))
  632. (not buffer-file-name))
  633. (if (buffer-base-buffer)
  634. (org-set-local 'buffer-file-name
  635. (with-current-buffer (buffer-base-buffer)
  636. buffer-file-name))
  637. (error "Need a file name to be able to export")))
  638. (org-lparse-warn
  639. (format "Exporting to %s using org-lparse..."
  640. (upcase (symbol-name
  641. (or org-lparse-backend org-lparse-other-backend)))))
  642. (setq-default org-todo-line-regexp org-todo-line-regexp)
  643. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  644. (setq-default org-done-keywords org-done-keywords)
  645. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  646. (let* (org-lparse-encode-pending
  647. org-lparse-par-open
  648. (org-lparse-list-level 0) ; list level starts at 1. A
  649. ; value of 0 implies we are
  650. ; outside of any list
  651. org-lparse-outline-text-open
  652. (org-lparse-latex-fragment-fallback ; currently used only by
  653. ; odt exporter
  654. (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK))
  655. (if (and (org-check-external-command "latex" "" t)
  656. (org-check-external-command "dvipng" "" t))
  657. 'dvipng
  658. 'verbatim)))
  659. (org-lparse-insert-tag-with-newlines 'both)
  660. (org-lparse-to-buffer to-buffer)
  661. (org-lparse-body-only body-only)
  662. (org-lparse-entity-control-callbacks-alist
  663. (org-lparse-get 'ENTITY-CONTROL))
  664. (org-lparse-entity-format-callbacks-alist
  665. (org-lparse-get 'ENTITY-FORMAT))
  666. (opt-plist
  667. (org-export-process-option-filters
  668. (org-combine-plists (org-default-export-plist)
  669. ext-plist
  670. (org-infile-export-plist))))
  671. (body-only (or body-only (plist-get opt-plist :body-only)))
  672. valid org-lparse-dyn-first-heading-pos
  673. (odd org-odd-levels-only)
  674. (region-p (org-region-active-p))
  675. (rbeg (and region-p (region-beginning)))
  676. (rend (and region-p (region-end)))
  677. (subtree-p
  678. (if (plist-get opt-plist :ignore-subtree-p)
  679. nil
  680. (when region-p
  681. (save-excursion
  682. (goto-char rbeg)
  683. (and (org-at-heading-p)
  684. (>= (org-end-of-subtree t t) rend))))))
  685. (level-offset (if subtree-p
  686. (save-excursion
  687. (goto-char rbeg)
  688. (+ (funcall outline-level)
  689. (if org-odd-levels-only 1 0)))
  690. 0))
  691. (opt-plist (setq org-export-opt-plist
  692. (if subtree-p
  693. (org-export-add-subtree-options opt-plist rbeg)
  694. opt-plist)))
  695. ;; The following two are dynamically scoped into other
  696. ;; routines below.
  697. (org-current-export-dir
  698. (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist)))
  699. (org-current-export-file buffer-file-name)
  700. (level 0) (line "") (origline "") txt todo
  701. (umax nil)
  702. (umax-toc nil)
  703. (filename (if to-buffer nil
  704. (expand-file-name
  705. (concat
  706. (file-name-sans-extension
  707. (or (and subtree-p
  708. (org-entry-get (region-beginning)
  709. "EXPORT_FILE_NAME" t))
  710. (file-name-nondirectory buffer-file-name)))
  711. "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist))
  712. (file-name-as-directory
  713. (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist))))))
  714. (current-dir (if buffer-file-name
  715. (file-name-directory buffer-file-name)
  716. default-directory))
  717. (buffer (if to-buffer
  718. (cond
  719. ((eq to-buffer 'string)
  720. (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME)))
  721. (t (get-buffer-create to-buffer)))
  722. (find-file-noselect
  723. (or (let ((f (org-lparse-get 'INIT-METHOD)))
  724. (and f (functionp f) (funcall f filename)))
  725. filename))))
  726. (org-levels-open (make-vector org-level-max nil))
  727. (date (plist-get opt-plist :date))
  728. (date (cond
  729. ((and date (string-match "%" date))
  730. (format-time-string date))
  731. (date date)
  732. (t (format-time-string "%Y-%m-%d %T %Z"))))
  733. (dummy (setq opt-plist (plist-put opt-plist :effective-date date)))
  734. (title (org-xml-encode-org-text-skip-links
  735. (or (and subtree-p (org-export-get-title-from-subtree))
  736. (plist-get opt-plist :title)
  737. (and (not body-only)
  738. (not
  739. (plist-get opt-plist :skip-before-1st-heading))
  740. (org-export-grab-title-from-buffer))
  741. (and buffer-file-name
  742. (file-name-sans-extension
  743. (file-name-nondirectory buffer-file-name)))
  744. "UNTITLED")))
  745. (dummy (setq opt-plist (plist-put opt-plist :title title)))
  746. (html-table-tag (plist-get opt-plist :html-table-tag))
  747. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  748. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  749. (org-lparse-dyn-current-environment nil)
  750. ;; Get the language-dependent settings
  751. (lang-words (or (assoc (plist-get opt-plist :language)
  752. org-export-language-setup)
  753. (assoc "en" org-export-language-setup)))
  754. (dummy (setq opt-plist (plist-put opt-plist :lang-words lang-words)))
  755. (head-count 0) cnt
  756. (start 0)
  757. (coding-system-for-write
  758. (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE))
  759. (and (boundp 'buffer-file-coding-system)
  760. buffer-file-coding-system)))
  761. (save-buffer-coding-system
  762. (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE))
  763. (and (boundp 'buffer-file-coding-system)
  764. buffer-file-coding-system)))
  765. (region
  766. (buffer-substring
  767. (if region-p (region-beginning) (point-min))
  768. (if region-p (region-end) (point-max))))
  769. (org-export-have-math nil)
  770. (org-export-footnotes-seen nil)
  771. (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
  772. (org-footnote-insert-pos-for-preprocessor 'point-min)
  773. (lines
  774. (org-split-string
  775. (org-export-preprocess-string
  776. region
  777. :emph-multiline t
  778. :for-backend (if (equal org-lparse-backend 'xhtml) ; hack
  779. 'html
  780. org-lparse-backend)
  781. :skip-before-1st-heading
  782. (plist-get opt-plist :skip-before-1st-heading)
  783. :drawers (plist-get opt-plist :drawers)
  784. :todo-keywords (plist-get opt-plist :todo-keywords)
  785. :tasks (plist-get opt-plist :tasks)
  786. :tags (plist-get opt-plist :tags)
  787. :priority (plist-get opt-plist :priority)
  788. :footnotes (plist-get opt-plist :footnotes)
  789. :timestamps (plist-get opt-plist :timestamps)
  790. :archived-trees
  791. (plist-get opt-plist :archived-trees)
  792. :select-tags (plist-get opt-plist :select-tags)
  793. :exclude-tags (plist-get opt-plist :exclude-tags)
  794. :add-text
  795. (plist-get opt-plist :text)
  796. :LaTeX-fragments
  797. (plist-get opt-plist :LaTeX-fragments))
  798. "[\r\n]"))
  799. table-open
  800. table-buffer table-orig-buffer
  801. ind
  802. rpl path attr desc descp desc1 desc2 link
  803. snumber fnc
  804. footnotes footref-seen
  805. org-lparse-output-buffer
  806. org-lparse-footnote-definitions
  807. org-lparse-footnote-number
  808. org-lparse-footnote-buffer
  809. org-lparse-toc
  810. href
  811. )
  812. (let ((inhibit-read-only t))
  813. (org-unmodified
  814. (remove-text-properties (point-min) (point-max)
  815. '(:org-license-to-kill t))))
  816. (message "Exporting...")
  817. (org-init-section-numbers)
  818. ;; Switch to the output buffer
  819. (setq org-lparse-output-buffer buffer)
  820. (set-buffer org-lparse-output-buffer)
  821. (let ((inhibit-read-only t)) (erase-buffer))
  822. (fundamental-mode)
  823. (org-install-letbind)
  824. (and (fboundp 'set-buffer-file-coding-system)
  825. (set-buffer-file-coding-system coding-system-for-write))
  826. (let ((case-fold-search nil)
  827. (org-odd-levels-only odd))
  828. ;; create local variables for all options, to make sure all called
  829. ;; functions get the correct information
  830. (mapc (lambda (x)
  831. (set (make-local-variable (nth 2 x))
  832. (plist-get opt-plist (car x))))
  833. org-export-plist-vars)
  834. (setq umax (if arg (prefix-numeric-value arg)
  835. org-export-headline-levels))
  836. (setq umax-toc (if (integerp org-export-with-toc)
  837. (min org-export-with-toc umax)
  838. umax))
  839. (when (and org-export-with-toc (not body-only))
  840. (setq lines (org-lparse-prepare-toc
  841. lines level-offset opt-plist umax-toc)))
  842. (unless body-only
  843. (org-lparse-begin 'DOCUMENT-CONTENT opt-plist)
  844. (org-lparse-begin 'DOCUMENT-BODY opt-plist))
  845. (setq head-count 0)
  846. (org-init-section-numbers)
  847. (org-lparse-begin-paragraph)
  848. (while (setq line (pop lines) origline line)
  849. (catch 'nextline
  850. (when (and (org-lparse-current-environment-p 'quote)
  851. (string-match org-outline-regexp-bol line))
  852. (org-lparse-end-environment 'quote))
  853. (when (org-lparse-current-environment-p 'quote)
  854. (org-lparse-insert 'LINE line)
  855. (throw 'nextline nil))
  856. ;; Fixed-width, verbatim lines (examples)
  857. (when (and org-export-with-fixed-width
  858. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  859. (when (not (org-lparse-current-environment-p 'fixedwidth))
  860. (org-lparse-begin-environment 'fixedwidth))
  861. (org-lparse-insert 'LINE (match-string 3 line))
  862. (when (or (not lines)
  863. (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
  864. (car lines))))
  865. (org-lparse-end-environment 'fixedwidth))
  866. (throw 'nextline nil))
  867. ;; Notes: The baseline version of org-html.el (git commit
  868. ;; 3d802e), while encoutering a *line-long* protected text,
  869. ;; does one of the following two things based on the state
  870. ;; of the export buffer.
  871. ;; 1. If a paragraph element has just been opened and
  872. ;; contains only whitespace as content, insert the
  873. ;; protected text as part of the previous paragraph.
  874. ;; 2. If the paragraph element has already been opened and
  875. ;; contains some valid content insert the protected text
  876. ;; as part of the current paragraph.
  877. ;; I think --->
  878. ;; Scenario 1 mentioned above kicks in when a block of
  879. ;; protected text has to be inserted enbloc. For example,
  880. ;; this happens, when inserting an source or example block
  881. ;; or preformatted content enclosed in #+backend,
  882. ;; #+begin_bakend ... #+end_backend)
  883. ;; Scenario 2 mentioned above kicks in when the protected
  884. ;; text is part of a running sentence. For example this
  885. ;; happens in the case of an *multiline* LaTeX equation that
  886. ;; needs to be inserted verbatim.
  887. ;; org-html.el in the master branch seems to do some
  888. ;; jugglery by moving paragraphs around. Inorder to make
  889. ;; these changes backend-agnostic introduce a new text
  890. ;; property org-native-text and impose the added semantics
  891. ;; that these protected blocks appear outside of a
  892. ;; conventional paragraph element.
  893. ;;
  894. ;; Extra Note: Check whether org-example and org-native-text
  895. ;; are entirely equivalent.
  896. ;; Fixes bug reported by Christian Moe concerning verbatim
  897. ;; LaTeX fragments.
  898. ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
  899. ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
  900. ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
  901. ;; Native Text
  902. (when (and (get-text-property 0 'org-native-text line)
  903. ;; Make sure it is the entire line that is protected
  904. (not (< (or (next-single-property-change
  905. 0 'org-native-text line) 10000)
  906. (length line))))
  907. (let ((ind (get-text-property 0 'original-indentation line)))
  908. (org-lparse-begin-environment 'native)
  909. (org-lparse-insert 'LINE line)
  910. (while (and lines
  911. (or (= (length (car lines)) 0)
  912. (not ind)
  913. (equal ind (get-text-property
  914. 0 'original-indentation (car lines))))
  915. (or (= (length (car lines)) 0)
  916. (get-text-property 0 'org-native-text (car lines))))
  917. (org-lparse-insert 'LINE (pop lines)))
  918. (org-lparse-end-environment 'native))
  919. (throw 'nextline nil))
  920. ;; Protected HTML
  921. (when (and (get-text-property 0 'org-protected line)
  922. ;; Make sure it is the entire line that is protected
  923. (not (< (or (next-single-property-change
  924. 0 'org-protected line) 10000)
  925. (length line))))
  926. (let ((ind (get-text-property 0 'original-indentation line)))
  927. (org-lparse-insert 'LINE line)
  928. (while (and lines
  929. (or (= (length (car lines)) 0)
  930. (not ind)
  931. (equal ind (get-text-property
  932. 0 'original-indentation (car lines))))
  933. (or (= (length (car lines)) 0)
  934. (get-text-property 0 'org-protected (car lines))))
  935. (org-lparse-insert 'LINE (pop lines))))
  936. (throw 'nextline nil))
  937. ;; Blockquotes, verse, and center
  938. (when (string-match "^ORG-\\(.+\\)-\\(START\\|END\\)$" line)
  939. (let* ((style (intern (downcase (match-string 1 line))))
  940. (f (cdr (assoc (match-string 2 line)
  941. '(("START" . org-lparse-begin-environment)
  942. ("END" . org-lparse-end-environment))))))
  943. (when (memq style '(blockquote verse center))
  944. (funcall f style)
  945. (throw 'nextline nil))))
  946. (run-hooks 'org-export-html-after-blockquotes-hook)
  947. (when (org-lparse-current-environment-p 'verse)
  948. (let ((i (org-get-string-indentation line)))
  949. (if (> i 0)
  950. (setq line (concat
  951. (let ((org-lparse-encode-pending t))
  952. (org-lparse-format 'SPACES (* 2 i)))
  953. " " (org-trim line))))
  954. (unless (string-match "\\\\\\\\[ \t]*$" line)
  955. (setq line (concat line "\\\\")))))
  956. ;; make targets to anchors
  957. (setq start 0)
  958. (while (string-match
  959. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
  960. (cond
  961. ((get-text-property (match-beginning 1) 'org-protected line)
  962. (setq start (match-end 1)))
  963. ((match-end 2)
  964. (setq line (replace-match
  965. (let ((org-lparse-encode-pending t))
  966. (org-lparse-format
  967. 'ANCHOR "" (org-solidify-link-text
  968. (match-string 1 line))))
  969. t t line)))
  970. ((and org-export-with-toc (equal (string-to-char line) ?*))
  971. ;; FIXME: NOT DEPENDENT on TOC?????????????????????
  972. (setq line (replace-match
  973. (let ((org-lparse-encode-pending t))
  974. (org-lparse-format
  975. 'FONTIFY (match-string 1 line) "target"))
  976. ;; (concat "@<i>" (match-string 1 line) "@</i> ")
  977. t t line)))
  978. (t
  979. (setq line (replace-match
  980. (concat
  981. (let ((org-lparse-encode-pending t))
  982. (org-lparse-format
  983. 'ANCHOR (match-string 1 line)
  984. (org-solidify-link-text (match-string 1 line))
  985. "target")) " ")
  986. t t line)))))
  987. (let ((org-lparse-encode-pending t))
  988. (setq line (org-lparse-handle-time-stamps line)))
  989. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  990. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  991. ;; Also handle sub_superscripts and checkboxes
  992. (or (string-match org-table-hline-regexp line)
  993. (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
  994. (setq line (org-xml-encode-org-text-skip-links line)))
  995. (setq line (org-lparse-format-org-link line opt-plist))
  996. ;; TODO items
  997. (if (and (string-match org-todo-line-regexp line)
  998. (match-beginning 2))
  999. (setq line (concat
  1000. (substring line 0 (match-beginning 2))
  1001. (org-lparse-format 'TODO (match-string 2 line))
  1002. (substring line (match-end 2)))))
  1003. ;; Does this contain a reference to a footnote?
  1004. (when org-export-with-footnotes
  1005. (setq start 0)
  1006. (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start)
  1007. ;; Discard protected matches not clearly identified as
  1008. ;; footnote markers.
  1009. (if (or (get-text-property (match-beginning 2) 'org-protected line)
  1010. (not (get-text-property (match-beginning 2) 'org-footnote line)))
  1011. (setq start (match-end 2))
  1012. (let ((n (match-string 2 line)) refcnt a)
  1013. (if (setq a (assoc n footref-seen))
  1014. (progn
  1015. (setcdr a (1+ (cdr a)))
  1016. (setq refcnt (cdr a)))
  1017. (setq refcnt 1)
  1018. (push (cons n 1) footref-seen))
  1019. (setq line
  1020. (replace-match
  1021. (concat
  1022. (or (match-string 1 line) "")
  1023. (org-lparse-format
  1024. 'FOOTNOTE-REFERENCE
  1025. n (cdr (assoc n org-lparse-footnote-definitions))
  1026. refcnt)
  1027. ;; If another footnote is following the
  1028. ;; current one, add a separator.
  1029. (if (save-match-data
  1030. (string-match "\\`\\[[0-9]+\\]"
  1031. (substring line (match-end 0))))
  1032. (ignore-errors
  1033. (org-lparse-get 'FOOTNOTE-SEPARATOR))
  1034. ""))
  1035. t t line))))))
  1036. (cond
  1037. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  1038. ;; This is a headline
  1039. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  1040. level-offset))
  1041. txt (match-string 2 line))
  1042. (if (string-match quote-re0 txt)
  1043. (setq txt (replace-match "" t t txt)))
  1044. (if (<= level (max umax umax-toc))
  1045. (setq head-count (+ head-count 1)))
  1046. (unless org-lparse-dyn-first-heading-pos
  1047. (setq org-lparse-dyn-first-heading-pos (point)))
  1048. (org-lparse-begin-level level txt umax head-count)
  1049. ;; QUOTES
  1050. (when (string-match quote-re line)
  1051. (org-lparse-begin-environment 'quote)))
  1052. ((and org-export-with-tables
  1053. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  1054. (when (not table-open)
  1055. ;; New table starts
  1056. (setq table-open t table-buffer nil table-orig-buffer nil))
  1057. ;; Accumulate lines
  1058. (setq table-buffer (cons line table-buffer)
  1059. table-orig-buffer (cons origline table-orig-buffer))
  1060. (when (or (not lines)
  1061. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  1062. (car lines))))
  1063. (setq table-open nil
  1064. table-buffer (nreverse table-buffer)
  1065. table-orig-buffer (nreverse table-orig-buffer))
  1066. (org-lparse-end-paragraph)
  1067. (org-lparse-insert 'TABLE table-buffer table-orig-buffer)))
  1068. ;; Normal lines
  1069. (t
  1070. ;; This line either is list item or end a list.
  1071. (when (get-text-property 0 'list-item line)
  1072. (setq line (org-lparse-export-list-line
  1073. line
  1074. (get-text-property 0 'list-item line)
  1075. (get-text-property 0 'list-struct line)
  1076. (get-text-property 0 'list-prevs line))))
  1077. ;; Horizontal line
  1078. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  1079. (with-org-lparse-preserve-paragraph-state
  1080. (org-lparse-insert 'HORIZONTAL-LINE))
  1081. (throw 'nextline nil))
  1082. ;; Empty lines start a new paragraph. If hand-formatted lists
  1083. ;; are not fully interpreted, lines starting with "-", "+", "*"
  1084. ;; also start a new paragraph.
  1085. (when (string-match "^ [-+*]-\\|^[ \t]*$" line)
  1086. (when org-lparse-footnote-number
  1087. (org-lparse-end-footnote-definition org-lparse-footnote-number)
  1088. (setq org-lparse-footnote-number nil))
  1089. (org-lparse-begin-paragraph))
  1090. ;; Is this the start of a footnote?
  1091. (when org-export-with-footnotes
  1092. (when (and (boundp 'footnote-section-tag-regexp)
  1093. (string-match (concat "^" footnote-section-tag-regexp)
  1094. line))
  1095. ;; ignore this line
  1096. (throw 'nextline nil))
  1097. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  1098. (org-lparse-end-paragraph)
  1099. (setq org-lparse-footnote-number (match-string 1 line))
  1100. (setq line (replace-match "" t t line))
  1101. (org-lparse-begin-footnote-definition org-lparse-footnote-number)))
  1102. ;; Check if the line break needs to be conserved
  1103. (cond
  1104. ((string-match "\\\\\\\\[ \t]*$" line)
  1105. (setq line (replace-match
  1106. (org-lparse-format 'LINE-BREAK)
  1107. t t line)))
  1108. (org-export-preserve-breaks
  1109. (setq line (concat line (org-lparse-format 'LINE-BREAK)))))
  1110. ;; Check if a paragraph should be started
  1111. (let ((start 0))
  1112. (while (and org-lparse-par-open
  1113. (string-match "\\\\par\\>" line start))
  1114. (error "FIXME")
  1115. ;; Leave a space in the </p> so that the footnote matcher
  1116. ;; does not see this.
  1117. (if (not (get-text-property (match-beginning 0)
  1118. 'org-protected line))
  1119. (setq line (replace-match "</p ><p >" t t line)))
  1120. (setq start (match-end 0))))
  1121. (org-lparse-insert 'LINE line)))))
  1122. ;; Properly close all local lists and other lists
  1123. (when (org-lparse-current-environment-p 'quote)
  1124. (org-lparse-end-environment 'quote))
  1125. (org-lparse-end-level 1 umax)
  1126. ;; the </div> to close the last text-... div.
  1127. (when (and (> umax 0) org-lparse-dyn-first-heading-pos)
  1128. (org-lparse-end-outline-text-or-outline))
  1129. (org-lparse-end 'DOCUMENT-BODY opt-plist)
  1130. (unless body-only
  1131. (org-lparse-end 'DOCUMENT-CONTENT))
  1132. (unless (plist-get opt-plist :buffer-will-be-killed)
  1133. (set-auto-mode t))
  1134. (org-lparse-end 'EXPORT)
  1135. (goto-char (point-min))
  1136. (or (org-export-push-to-kill-ring
  1137. (upcase (symbol-name org-lparse-backend)))
  1138. (message "Exporting... done"))
  1139. (cond
  1140. ((not to-buffer)
  1141. (let ((f (org-lparse-get 'SAVE-METHOD)))
  1142. (or (and f (functionp f) (funcall f filename opt-plist))
  1143. (save-buffer)))
  1144. (or (when (and (boundp 'org-lparse-other-backend)
  1145. org-lparse-other-backend
  1146. (not (equal org-lparse-backend org-lparse-other-backend)))
  1147. (let ((org-lparse-convert-process
  1148. (org-lparse-get-converter org-lparse-backend)))
  1149. (when org-lparse-convert-process
  1150. (org-lparse-convert buffer-file-name
  1151. (symbol-name org-lparse-other-backend)))))
  1152. (current-buffer)))
  1153. ((eq to-buffer 'string)
  1154. (prog1 (buffer-substring (point-min) (point-max))
  1155. (kill-buffer (current-buffer))))
  1156. (t (current-buffer))))))
  1157. (defun org-lparse-format-table (lines olines)
  1158. "Retuns backend-specific code for org-type and table-type tables."
  1159. (if (stringp lines)
  1160. (setq lines (org-split-string lines "\n")))
  1161. (if (string-match "^[ \t]*|" (car lines))
  1162. ;; A normal org table
  1163. (org-lparse-format-org-table lines nil)
  1164. ;; Table made by table.el
  1165. (or (org-lparse-format-table-table-using-table-generate-source
  1166. org-lparse-backend olines
  1167. (not org-export-prefer-native-exporter-for-tables))
  1168. ;; We are here only when table.el table has NO col or row
  1169. ;; spanning and the user prefers using org's own converter for
  1170. ;; exporting of such simple table.el tables.
  1171. (org-lparse-format-table-table lines))))
  1172. (defun org-lparse-table-get-colalign-info (lines)
  1173. (let ((forced-aligns (org-find-text-property-in-string
  1174. 'org-forced-aligns (car lines))))
  1175. (when (and forced-aligns org-table-clean-did-remove-column)
  1176. (setq forced-aligns
  1177. (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
  1178. forced-aligns))
  1179. (defvar org-lparse-table-style)
  1180. (defvar org-lparse-table-ncols)
  1181. (defvar org-lparse-table-rownum)
  1182. (defvar org-lparse-table-is-styled)
  1183. (defvar org-lparse-table-begin-marker)
  1184. (defvar org-lparse-table-num-numeric-items-per-column)
  1185. (defvar org-lparse-table-colalign-info)
  1186. (defvar org-lparse-table-colalign-vector)
  1187. ;; Following variables are defined in org-table.el
  1188. (defvar org-table-number-fraction)
  1189. (defvar org-table-number-regexp)
  1190. (defun org-lparse-do-format-org-table (lines &optional splice)
  1191. "Format a org-type table into backend-specific code.
  1192. LINES is a list of lines. Optional argument SPLICE means, do not
  1193. insert header and surrounding <table> tags, just format the lines.
  1194. Optional argument NO-CSS means use XHTML attributes instead of CSS
  1195. for formatting. This is required for the DocBook exporter."
  1196. (require 'org-table)
  1197. ;; Get rid of hlines at beginning and end
  1198. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1199. (setq lines (nreverse lines))
  1200. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1201. (setq lines (nreverse lines))
  1202. (when org-export-table-remove-special-lines
  1203. ;; Check if the table has a marking column. If yes remove the
  1204. ;; column and the special lines
  1205. (setq lines (org-table-clean-before-export lines)))
  1206. (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
  1207. (caption (and caption (org-xml-encode-org-text caption)))
  1208. (label (org-find-text-property-in-string 'org-label (car lines)))
  1209. (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines))
  1210. (attributes (org-find-text-property-in-string 'org-attributes
  1211. (car lines)))
  1212. (head (and org-export-highlight-first-table-line
  1213. (delq nil (mapcar
  1214. (lambda (x) (string-match "^[ \t]*|-" x))
  1215. (cdr lines)))))
  1216. (org-lparse-table-rownum -1) org-lparse-table-ncols i (cnt 0)
  1217. tbopen line fields
  1218. org-lparse-table-cur-rowgrp-is-hdr
  1219. org-lparse-table-rowgrp-open
  1220. org-lparse-table-num-numeric-items-per-column
  1221. org-lparse-table-colalign-vector n
  1222. org-lparse-table-rowgrp-info
  1223. org-lparse-table-begin-marker
  1224. (org-lparse-table-style 'org-table)
  1225. org-lparse-table-is-styled)
  1226. (cond
  1227. (splice
  1228. (setq org-lparse-table-is-styled nil)
  1229. (while (setq line (pop lines))
  1230. (unless (string-match "^[ \t]*|-" line)
  1231. (insert
  1232. (org-lparse-format-table-row
  1233. (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
  1234. (t
  1235. (setq org-lparse-table-is-styled t)
  1236. (org-lparse-begin 'TABLE caption label attributes)
  1237. (setq org-lparse-table-begin-marker (point))
  1238. (org-lparse-begin-table-rowgroup head)
  1239. (while (setq line (pop lines))
  1240. (cond
  1241. ((string-match "^[ \t]*|-" line)
  1242. (when lines (org-lparse-begin-table-rowgroup)))
  1243. (t
  1244. (insert
  1245. (org-lparse-format-table-row
  1246. (org-split-string line "[ \t]*|[ \t]*")) "\n"))))
  1247. (org-lparse-end 'TABLE-ROWGROUP)
  1248. (org-lparse-end-table)))))
  1249. (defun org-lparse-format-org-table (lines &optional splice)
  1250. (with-temp-buffer
  1251. (org-lparse-do-format-org-table lines splice)
  1252. (buffer-substring-no-properties (point-min) (point-max))))
  1253. (defun org-lparse-do-format-table-table (lines)
  1254. "Format a table generated by table.el into backend-specific code.
  1255. This conversion does *not* use `table-generate-source' from table.el.
  1256. This has the advantage that Org-mode's HTML conversions can be used.
  1257. But it has the disadvantage, that no cell- or row-spanning is allowed."
  1258. (let (line field-buffer
  1259. (org-lparse-table-cur-rowgrp-is-hdr
  1260. org-export-highlight-first-table-line)
  1261. (caption nil)
  1262. (attributes nil)
  1263. (label nil)
  1264. (org-lparse-table-style 'table-table)
  1265. (org-lparse-table-is-styled nil)
  1266. fields org-lparse-table-ncols i (org-lparse-table-rownum -1)
  1267. (empty (org-lparse-format 'SPACES 1)))
  1268. (org-lparse-begin 'TABLE caption label attributes)
  1269. (while (setq line (pop lines))
  1270. (cond
  1271. ((string-match "^[ \t]*\\+-" line)
  1272. (when field-buffer
  1273. (let ((org-export-table-row-tags '("<tr>" . "</tr>"))
  1274. ;; (org-export-html-table-use-header-tags-for-first-column nil)
  1275. )
  1276. (insert (org-lparse-format-table-row field-buffer empty)))
  1277. (setq org-lparse-table-cur-rowgrp-is-hdr nil)
  1278. (setq field-buffer nil)))
  1279. (t
  1280. ;; Break the line into fields and store the fields
  1281. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1282. (if field-buffer
  1283. (setq field-buffer (mapcar
  1284. (lambda (x)
  1285. (concat x (org-lparse-format 'LINE-BREAK)
  1286. (pop fields)))
  1287. field-buffer))
  1288. (setq field-buffer fields)))))
  1289. (org-lparse-end-table)))
  1290. (defun org-lparse-format-table-table (lines)
  1291. (with-temp-buffer
  1292. (org-lparse-do-format-table-table lines)
  1293. (buffer-substring-no-properties (point-min) (point-max))))
  1294. (defvar table-source-languages) ; defined in table.el
  1295. (defun org-lparse-format-table-table-using-table-generate-source (backend
  1296. lines
  1297. &optional
  1298. spanned-only)
  1299. "Format a table into BACKEND, using `table-generate-source' from table.el.
  1300. Use SPANNED-ONLY to suppress exporting of simple table.el tables.
  1301. When SPANNED-ONLY is nil, all table.el tables are exported. When
  1302. SPANNED-ONLY is non-nil, only tables with either row or column
  1303. spans are exported.
  1304. This routine returns the generated source or nil as appropriate.
  1305. Refer docstring of `org-export-prefer-native-exporter-for-tables'
  1306. for further information."
  1307. (require 'table)
  1308. (with-current-buffer (get-buffer-create " org-tmp1 ")
  1309. (erase-buffer)
  1310. (insert (mapconcat 'identity lines "\n"))
  1311. (goto-char (point-min))
  1312. (if (not (re-search-forward "|[^+]" nil t))
  1313. (error "Error processing table"))
  1314. (table-recognize-table)
  1315. (when (or (not spanned-only)
  1316. (let* ((dim (table-query-dimension))
  1317. (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
  1318. (not (= (* c r) cells))))
  1319. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  1320. (cond
  1321. ((member backend table-source-languages)
  1322. (table-generate-source backend " org-tmp2 ")
  1323. (set-buffer " org-tmp2 ")
  1324. (buffer-substring (point-min) (point-max)))
  1325. (t
  1326. ;; table.el doesn't support the given backend. Currently this
  1327. ;; happens in case of odt export. Strip the table from the
  1328. ;; generated document. A better alternative would be to embed
  1329. ;; the table as ascii text in the output document.
  1330. (org-lparse-warn
  1331. (concat
  1332. "Found table.el-type table in the source org file. "
  1333. (format "table.el doesn't support %s backend. "
  1334. (upcase (symbol-name backend)))
  1335. "Skipping ahead ..."))
  1336. "")))))
  1337. (defun org-lparse-handle-time-stamps (s)
  1338. "Format time stamps in string S, or remove them."
  1339. (catch 'exit
  1340. (let (r b)
  1341. (while (string-match org-maybe-keyword-time-regexp s)
  1342. (or b (setq b (substring s 0 (match-beginning 0))))
  1343. (setq r (concat
  1344. r (substring s 0 (match-beginning 0)) " "
  1345. (org-lparse-format
  1346. 'FONTIFY
  1347. (concat
  1348. (if (match-end 1)
  1349. (org-lparse-format
  1350. 'FONTIFY
  1351. (match-string 1 s) "timestamp-kwd"))
  1352. " "
  1353. (org-lparse-format
  1354. 'FONTIFY
  1355. (substring (org-translate-time (match-string 3 s)) 1 -1)
  1356. "timestamp"))
  1357. "timestamp-wrapper"))
  1358. s (substring s (match-end 0))))
  1359. ;; Line break if line started and ended with time stamp stuff
  1360. (if (not r)
  1361. s
  1362. (setq r (concat r s))
  1363. (unless (string-match "\\S-" (concat b s))
  1364. (setq r (concat r (org-lparse-format 'LINE-BREAK))))
  1365. r))))
  1366. (defun org-xml-encode-plain-text (s)
  1367. "Convert plain text characters to HTML equivalent.
  1368. Possible conversions are set in `org-export-html-protect-char-alist'."
  1369. (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP)) c)
  1370. (while (setq c (pop cl))
  1371. (let ((start 0))
  1372. (while (string-match (car c) s start)
  1373. (setq s (replace-match (cdr c) t t s)
  1374. start (1+ (match-beginning 0))))))
  1375. s))
  1376. (defun org-xml-encode-org-text-skip-links (string)
  1377. "Prepare STRING for HTML export. Apply all active conversions.
  1378. If there are links in the string, don't modify these."
  1379. (let* ((re (concat org-bracket-link-regexp "\\|"
  1380. (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
  1381. m s l res)
  1382. (while (setq m (string-match re string))
  1383. (setq s (substring string 0 m)
  1384. l (match-string 0 string)
  1385. string (substring string (match-end 0)))
  1386. (push (org-xml-encode-org-text s) res)
  1387. (push l res))
  1388. (push (org-xml-encode-org-text string) res)
  1389. (apply 'concat (nreverse res))))
  1390. (defun org-xml-encode-org-text (s)
  1391. "Apply all active conversions to translate special ASCII to HTML."
  1392. (setq s (org-xml-encode-plain-text s))
  1393. (if org-export-html-expand
  1394. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1395. (setq s (replace-match "<\\1>" t nil s))))
  1396. (if org-export-with-emphasize
  1397. (setq s (org-lparse-apply-char-styles s)))
  1398. (if org-export-with-special-strings
  1399. (setq s (org-lparse-convert-special-strings s)))
  1400. (if org-export-with-sub-superscripts
  1401. (setq s (org-lparse-apply-sub-superscript-styles s)))
  1402. (if org-export-with-TeX-macros
  1403. (let ((start 0) wd rep)
  1404. (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
  1405. s start))
  1406. (if (get-text-property (match-beginning 0) 'org-protected s)
  1407. (setq start (match-end 0))
  1408. (setq wd (match-string 1 s))
  1409. (if (setq rep (org-lparse-format 'ORG-ENTITY wd))
  1410. (setq s (replace-match rep t t s))
  1411. (setq start (+ start (length wd))))))))
  1412. s)
  1413. (defun org-lparse-convert-special-strings (string)
  1414. "Convert special characters in STRING to HTML."
  1415. (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS))
  1416. e a re rpl start)
  1417. (while (setq a (pop all))
  1418. (setq re (car a) rpl (cdr a) start 0)
  1419. (while (string-match re string start)
  1420. (if (get-text-property (match-beginning 0) 'org-protected string)
  1421. (setq start (match-end 0))
  1422. (setq string (replace-match rpl t nil string)))))
  1423. string))
  1424. (defun org-lparse-apply-sub-superscript-styles (string)
  1425. "Apply subscript and superscript styles to STRING.
  1426. Use `org-export-with-sub-superscripts' to control application of
  1427. sub and superscript styles."
  1428. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1429. (while (string-match org-match-substring-regexp string s)
  1430. (cond
  1431. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1432. ((get-text-property (match-beginning 2) 'org-protected string)
  1433. (setq s (match-end 2)))
  1434. (t
  1435. (setq s (match-end 1)
  1436. key (if (string= (match-string 2 string) "_")
  1437. 'subscript 'superscript)
  1438. c (or (match-string 8 string)
  1439. (match-string 6 string)
  1440. (match-string 5 string))
  1441. string (replace-match
  1442. (concat (match-string 1 string)
  1443. (org-lparse-format 'FONTIFY c key))
  1444. t t string)))))
  1445. (while (string-match "\\\\\\([_^]\\)" string)
  1446. (setq string (replace-match (match-string 1 string) t t string)))
  1447. string))
  1448. (defvar org-lparse-char-styles
  1449. `(("*" bold)
  1450. ("/" emphasis)
  1451. ("_" underline)
  1452. ("=" code)
  1453. ("~" verbatim)
  1454. ("+" strike))
  1455. "Map Org emphasis markers to char styles.
  1456. This is an alist where each element is of the
  1457. form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
  1458. (defun org-lparse-apply-char-styles (string)
  1459. "Apply char styles to STRING.
  1460. The variable `org-lparse-char-styles' controls how the Org
  1461. emphasis markers are interpreted."
  1462. (let ((s 0) rpl)
  1463. (while (string-match org-emph-re string s)
  1464. (if (not (equal
  1465. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1466. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1467. (setq s (match-beginning 0)
  1468. rpl
  1469. (concat
  1470. (match-string 1 string)
  1471. (org-lparse-format
  1472. 'FONTIFY (match-string 4 string)
  1473. (nth 1 (assoc (match-string 3 string)
  1474. org-lparse-char-styles)))
  1475. (match-string 5 string))
  1476. string (replace-match rpl t t string)
  1477. s (+ s (- (length rpl) 2)))
  1478. (setq s (1+ s))))
  1479. string))
  1480. (defun org-lparse-export-list-line (line pos struct prevs)
  1481. "Insert list syntax in export buffer. Return LINE, maybe modified.
  1482. POS is the item position or line position the line had before
  1483. modifications to buffer. STRUCT is the list structure. PREVS is
  1484. the alist of previous items."
  1485. (let* ((get-type
  1486. (function
  1487. ;; Translate type of list containing POS to "d", "o" or
  1488. ;; "u".
  1489. (lambda (pos struct prevs)
  1490. (let ((type (org-list-get-list-type pos struct prevs)))
  1491. (cond
  1492. ((eq 'ordered type) "o")
  1493. ((eq 'descriptive type) "d")
  1494. (t "u"))))))
  1495. (get-closings
  1496. (function
  1497. ;; Return list of all items and sublists ending at POS, in
  1498. ;; reverse order.
  1499. (lambda (pos)
  1500. (let (out)
  1501. (catch 'exit
  1502. (mapc (lambda (e)
  1503. (let ((end (nth 6 e))
  1504. (item (car e)))
  1505. (cond
  1506. ((= end pos) (push item out))
  1507. ((>= item pos) (throw 'exit nil)))))
  1508. struct))
  1509. out)))))
  1510. ;; First close any previous item, or list, ending at POS.
  1511. (mapc (lambda (e)
  1512. (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
  1513. (first-item (org-list-get-list-begin e struct prevs))
  1514. (type (funcall get-type first-item struct prevs)))
  1515. (org-lparse-end-paragraph)
  1516. ;; Ending for every item
  1517. (org-lparse-end-list-item type)
  1518. ;; We're ending last item of the list: end list.
  1519. (when lastp
  1520. (org-lparse-end-list type)
  1521. (org-lparse-begin-paragraph))))
  1522. (funcall get-closings pos))
  1523. (cond
  1524. ;; At an item: insert appropriate tags in export buffer.
  1525. ((assq pos struct)
  1526. (string-match
  1527. (concat "[ \t]*\\(\\S-+[ \t]*\\)"
  1528. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  1529. "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
  1530. "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
  1531. "\\(.*\\)") line)
  1532. (let* ((checkbox (match-string 3 line))
  1533. (desc-tag (or (match-string 4 line) "???"))
  1534. (body (or (match-string 5 line) ""))
  1535. (list-beg (org-list-get-list-begin pos struct prevs))
  1536. (firstp (= list-beg pos))
  1537. ;; Always refer to first item to determine list type, in
  1538. ;; case list is ill-formed.
  1539. (type (funcall get-type list-beg struct prevs))
  1540. (counter (let ((count-tmp (org-list-get-counter pos struct)))
  1541. (cond
  1542. ((not count-tmp) nil)
  1543. ((string-match "[A-Za-z]" count-tmp)
  1544. (- (string-to-char (upcase count-tmp)) 64))
  1545. ((string-match "[0-9]+" count-tmp)
  1546. count-tmp)))))
  1547. (when firstp
  1548. (org-lparse-end-paragraph)
  1549. (org-lparse-begin-list type))
  1550. (let ((arg (cond ((equal type "d") desc-tag)
  1551. ((equal type "o") counter))))
  1552. (org-lparse-begin 'LIST-ITEM type arg))
  1553. ;; If line had a checkbox, some additional modification is required.
  1554. (when checkbox
  1555. (setq body
  1556. (concat
  1557. (org-lparse-format
  1558. 'FONTIFY (concat
  1559. "["
  1560. (cond
  1561. ((string-match "X" checkbox) "X")
  1562. ((string-match " " checkbox)
  1563. (org-lparse-format 'SPACES 1))
  1564. (t "-"))
  1565. "]")
  1566. 'code)
  1567. " "
  1568. body)))
  1569. ;; Return modified line
  1570. body))
  1571. ;; At a list ender: go to next line (side-effects only).
  1572. ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
  1573. ;; Not at an item: return line unchanged (side-effects only).
  1574. (t line))))
  1575. (defun org-lparse-bind-local-variables (opt-plist)
  1576. (mapc (lambda (x)
  1577. (set (make-local-variable (nth 2 x))
  1578. (plist-get opt-plist (car x))))
  1579. org-export-plist-vars))
  1580. (defvar org-lparse-table-rowgrp-open)
  1581. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  1582. (defvar org-lparse-footnote-number)
  1583. (defvar org-lparse-footnote-definitions)
  1584. (defvar org-lparse-footnote-buffer)
  1585. (defvar org-lparse-output-buffer)
  1586. (defcustom org-lparse-debug nil
  1587. "Enable or Disable logging of `org-lparse' callbacks.
  1588. The parameters passed to the backend-registered ENTITY-CONTROL
  1589. and ENTITY-FORMAT callbacks are logged as comment strings in the
  1590. exported buffer. (org-lparse-format 'COMMENT fmt args) is used
  1591. for logging. Customize this variable only if you are an expert
  1592. user. Valid values of this variable are:
  1593. nil : Disable logging
  1594. control : Log all invocations of `org-lparse-begin' and
  1595. `org-lparse-end' callbacks.
  1596. format : Log invocations of `org-lparse-format' callbacks.
  1597. t : Log all invocations of `org-lparse-begin', `org-lparse-end'
  1598. and `org-lparse-format' callbacks,"
  1599. :group 'org-lparse
  1600. :type '(choice
  1601. (const :tag "Disable" nil)
  1602. (const :tag "Format callbacks" format)
  1603. (const :tag "Control callbacks" control)
  1604. (const :tag "Format and Control callbacks" t)))
  1605. (defun org-lparse-begin (entity &rest args)
  1606. "Begin ENTITY in current buffer. ARGS is entity specific.
  1607. ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
  1608. Use (org-lparse-begin 'LIST \"o\") to begin a list in current
  1609. buffer.
  1610. See `org-xhtml-entity-control-callbacks-alist' for more
  1611. information."
  1612. (when (and (member org-lparse-debug '(t control))
  1613. (not (eq entity 'DOCUMENT-CONTENT)))
  1614. (insert (org-lparse-format 'COMMENT "%s BEGIN %S" entity args)))
  1615. (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist))))
  1616. (unless f (error "Unknown entity: %s" entity))
  1617. (apply f args)))
  1618. (defun org-lparse-end (entity &rest args)
  1619. "Close ENTITY in current buffer. ARGS is entity specific.
  1620. ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
  1621. etc.
  1622. Use (org-lparse-end 'LIST \"o\") to close a list in current
  1623. buffer.
  1624. See `org-xhtml-entity-control-callbacks-alist' for more
  1625. information."
  1626. (when (and (member org-lparse-debug '(t control))
  1627. (not (eq entity 'DOCUMENT-CONTENT)))
  1628. (insert (org-lparse-format 'COMMENT "%s END %S" entity args)))
  1629. (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist))))
  1630. (unless f (error "Unknown entity: %s" entity))
  1631. (apply f args)))
  1632. (defun org-lparse-begin-paragraph (&optional style)
  1633. "Insert <p>, but first close previous paragraph if any."
  1634. (org-lparse-end-paragraph)
  1635. (org-lparse-begin 'PARAGRAPH style)
  1636. (setq org-lparse-par-open t))
  1637. (defun org-lparse-end-paragraph ()
  1638. "Close paragraph if there is one open."
  1639. (when org-lparse-par-open
  1640. (org-lparse-end 'PARAGRAPH)
  1641. (setq org-lparse-par-open nil)))
  1642. (defun org-lparse-end-list-item (&optional type)
  1643. "Close <li> if necessary."
  1644. (org-lparse-end-paragraph)
  1645. (org-lparse-end 'LIST-ITEM (or type "u")))
  1646. (defvar org-lparse-dyn-current-environment nil)
  1647. (defun org-lparse-begin-environment (style)
  1648. (assert (not org-lparse-dyn-current-environment) t)
  1649. (setq org-lparse-dyn-current-environment style)
  1650. (org-lparse-begin 'ENVIRONMENT style))
  1651. (defun org-lparse-end-environment (style)
  1652. (org-lparse-end 'ENVIRONMENT style)
  1653. (assert (eq org-lparse-dyn-current-environment style) t)
  1654. (setq org-lparse-dyn-current-environment nil))
  1655. (defun org-lparse-current-environment-p (style)
  1656. (eq org-lparse-dyn-current-environment style))
  1657. (defun org-lparse-begin-footnote-definition (n)
  1658. (unless org-lparse-footnote-buffer
  1659. (setq org-lparse-footnote-buffer
  1660. (get-buffer-create "*Org HTML Export Footnotes*")))
  1661. (set-buffer org-lparse-footnote-buffer)
  1662. (erase-buffer)
  1663. (setq org-lparse-insert-tag-with-newlines nil)
  1664. (org-lparse-begin 'FOOTNOTE-DEFINITION n))
  1665. (defun org-lparse-end-footnote-definition (n)
  1666. (org-lparse-end 'FOOTNOTE-DEFINITION n)
  1667. (setq org-lparse-insert-tag-with-newlines 'both)
  1668. (push (cons n (buffer-string)) org-lparse-footnote-definitions)
  1669. (set-buffer org-lparse-output-buffer))
  1670. (defun org-lparse-format (entity &rest args)
  1671. "Format ENTITY in backend-specific way and return it.
  1672. ARGS is specific to entity being formatted.
  1673. Use (org-lparse-format 'HEADING \"text\" 1) to format text as
  1674. level 1 heading.
  1675. See `org-xhtml-entity-format-callbacks-alist' for more information."
  1676. (when (and (member org-lparse-debug '(t format))
  1677. (not (equal entity 'COMMENT)))
  1678. (insert (org-lparse-format 'COMMENT "%s: %S" entity args)))
  1679. (cond
  1680. ((consp entity)
  1681. (let ((text (pop args)))
  1682. (apply 'org-lparse-format 'TAGS entity text args)))
  1683. (t
  1684. (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist))))
  1685. (unless f (error "Unknown entity: %s" entity))
  1686. (apply f args)))))
  1687. (defun org-lparse-insert (entity &rest args)
  1688. (insert (apply 'org-lparse-format entity args)))
  1689. (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc)
  1690. (let* ((quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  1691. (org-min-level (org-get-min-level lines level-offset))
  1692. (org-last-level org-min-level)
  1693. level)
  1694. (with-temp-buffer
  1695. (org-lparse-bind-local-variables opt-plist)
  1696. (erase-buffer)
  1697. (org-lparse-begin 'TOC (nth 3 (plist-get opt-plist :lang-words)))
  1698. (setq
  1699. lines
  1700. (mapcar
  1701. #'(lambda (line)
  1702. (when (and (string-match org-todo-line-regexp line)
  1703. (not (get-text-property 0 'org-protected line))
  1704. (<= (setq level (org-tr-level
  1705. (- (match-end 1) (match-beginning 1)
  1706. level-offset)))
  1707. umax-toc))
  1708. (let ((txt (save-match-data
  1709. (org-xml-encode-org-text-skip-links
  1710. (org-export-cleanup-toc-line
  1711. (match-string 3 line)))))
  1712. (todo (and
  1713. org-export-mark-todo-in-toc
  1714. (or (and (match-beginning 2)
  1715. (not (member (match-string 2 line)
  1716. org-done-keywords)))
  1717. (and (= level umax-toc)
  1718. (org-search-todo-below
  1719. line lines level)))))
  1720. tags)
  1721. ;; Check for targets
  1722. (while (string-match org-any-target-regexp line)
  1723. (setq line
  1724. (replace-match
  1725. (let ((org-lparse-encode-pending t))
  1726. (org-lparse-format 'FONTIFY
  1727. (match-string 1 line) "target"))
  1728. t t line)))
  1729. (when (string-match
  1730. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  1731. (setq tags (match-string 1 txt)
  1732. txt (replace-match "" t nil txt)))
  1733. (when (string-match quote-re0 txt)
  1734. (setq txt (replace-match "" t t txt)))
  1735. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  1736. (setq txt (replace-match "" t t txt)))
  1737. (org-lparse-format
  1738. 'TOC-ITEM
  1739. (let* ((snumber (org-section-number level))
  1740. (href (replace-regexp-in-string
  1741. "\\." "-" (format "sec-%s" snumber)))
  1742. (href
  1743. (or
  1744. (cdr (assoc
  1745. href org-export-preferred-target-alist))
  1746. href))
  1747. (href (org-solidify-link-text href)))
  1748. (org-lparse-format 'TOC-ENTRY snumber todo txt tags href))
  1749. level org-last-level)
  1750. (setq org-last-level level)))
  1751. line)
  1752. lines))
  1753. (org-lparse-end 'TOC)
  1754. (setq org-lparse-toc (buffer-string))))
  1755. lines)
  1756. (defun org-lparse-format-table-row (fields &optional text-for-empty-fields)
  1757. (unless org-lparse-table-ncols
  1758. ;; first row of the table
  1759. (setq org-lparse-table-ncols (length fields))
  1760. (when org-lparse-table-is-styled
  1761. (setq org-lparse-table-num-numeric-items-per-column
  1762. (make-vector org-lparse-table-ncols 0))
  1763. (setq org-lparse-table-colalign-vector
  1764. (make-vector org-lparse-table-ncols nil))
  1765. (let ((c -1))
  1766. (while (< (incf c) org-lparse-table-ncols)
  1767. (let ((cookie (cdr (assoc (1+ c) org-lparse-table-colalign-info))))
  1768. (setf (aref org-lparse-table-colalign-vector c)
  1769. (cond
  1770. ((string= cookie "l") "left")
  1771. ((string= cookie "r") "right")
  1772. ((string= cookie "c") "center")
  1773. (t nil))))))))
  1774. (incf org-lparse-table-rownum)
  1775. (let ((i -1))
  1776. (org-lparse-format
  1777. 'TABLE-ROW
  1778. (mapconcat
  1779. (lambda (x)
  1780. (when (and (string= x "") text-for-empty-fields)
  1781. (setq x text-for-empty-fields))
  1782. (incf i)
  1783. (and org-lparse-table-is-styled
  1784. (< i org-lparse-table-ncols)
  1785. (string-match org-table-number-regexp x)
  1786. (incf (aref org-lparse-table-num-numeric-items-per-column i)))
  1787. (org-lparse-format 'TABLE-CELL x org-lparse-table-rownum i))
  1788. fields "\n"))))
  1789. (defun org-lparse-get (what &optional opt-plist)
  1790. "Query for value of WHAT for the current backend `org-lparse-backend'.
  1791. See also `org-lparse-backend-get'."
  1792. (if (boundp 'org-lparse-backend)
  1793. (org-lparse-backend-get (symbol-name org-lparse-backend) what opt-plist)
  1794. (error "org-lparse-backend is not bound yet")))
  1795. (defun org-lparse-backend-get (backend what &optional opt-plist)
  1796. "Query BACKEND for value of WHAT.
  1797. Dispatch the call to `org-<backend>-user-get'. If that throws an
  1798. error, dispatch the call to `org-<backend>-get'. See
  1799. `org-xhtml-get' for all known settings queried for by
  1800. `org-lparse' during the course of export."
  1801. (assert (stringp backend) t)
  1802. (unless (org-lparse-backend-is-native-p backend)
  1803. (error "Unknown native backend %s" backend))
  1804. (let ((backend-get-method (intern (format "org-%s-get" backend)))
  1805. (backend-user-get-method (intern (format "org-%s-user-get" backend))))
  1806. (cond
  1807. ((functionp backend-get-method)
  1808. (condition-case nil
  1809. (funcall backend-user-get-method what opt-plist)
  1810. (error (funcall backend-get-method what opt-plist))))
  1811. (t
  1812. (error "Native backend %s doesn't define %s" backend backend-get-method)))))
  1813. (defun org-lparse-insert-tag (tag &rest args)
  1814. (when (member org-lparse-insert-tag-with-newlines '(lead both))
  1815. (insert "\n"))
  1816. (insert (apply 'format tag args))
  1817. (when (member org-lparse-insert-tag-with-newlines '(trail both))
  1818. (insert "\n")))
  1819. (defun org-lparse-get-targets-from-title (title)
  1820. (let* ((target (org-get-text-property-any 0 'target title))
  1821. (extra-targets (assoc target org-export-target-aliases))
  1822. (target (or (cdr (assoc target org-export-preferred-target-alist))
  1823. target)))
  1824. (cons target (remove target extra-targets))))
  1825. (defun org-lparse-suffix-from-snumber (snumber)
  1826. (let* ((snu (replace-regexp-in-string "\\." "-" snumber))
  1827. (href (cdr (assoc (concat "sec-" snu)
  1828. org-export-preferred-target-alist))))
  1829. (org-solidify-link-text (or href snu))))
  1830. (defun org-lparse-begin-level (level title umax head-count)
  1831. "Insert a new LEVEL in HTML export.
  1832. When TITLE is nil, just close all open levels."
  1833. (org-lparse-end-level level umax)
  1834. (unless title (error "Why is heading nil"))
  1835. (let* ((targets (org-lparse-get-targets-from-title title))
  1836. (target (car targets)) (extra-targets (cdr targets))
  1837. (target (and target (org-solidify-link-text target)))
  1838. (extra-class (org-get-text-property-any 0 'html-container-class title))
  1839. snumber tags level1 class)
  1840. (when (string-match (org-re "\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$") title)
  1841. (setq tags (and org-export-with-tags (match-string 1 title)))
  1842. (setq title (replace-match "" t t title)))
  1843. (if (> level umax)
  1844. (progn
  1845. (if (aref org-levels-open (1- level))
  1846. (org-lparse-end-list-item)
  1847. (aset org-levels-open (1- level) t)
  1848. (org-lparse-end-paragraph)
  1849. (org-lparse-begin-list 'unordered))
  1850. (org-lparse-begin
  1851. 'LIST-ITEM 'unordered target
  1852. (org-lparse-format 'HEADLINE title extra-targets tags)))
  1853. (aset org-levels-open (1- level) t)
  1854. (setq snumber (org-section-number level))
  1855. (setq level1 (+ level (or (org-lparse-get 'TOPLEVEL-HLEVEL) 1) -1))
  1856. (unless (= head-count 1)
  1857. (org-lparse-end-outline-text-or-outline))
  1858. (org-lparse-begin-outline-and-outline-text
  1859. level1 snumber title tags target extra-targets extra-class)
  1860. (org-lparse-begin-paragraph))))
  1861. (defun org-lparse-end-level (level umax)
  1862. (org-lparse-end-paragraph)
  1863. (loop for l from org-level-max downto level
  1864. do (when (aref org-levels-open (1- l))
  1865. ;; Terminate one level in HTML export
  1866. (if (<= l umax)
  1867. (org-lparse-end-outline-text-or-outline)
  1868. (org-lparse-end-list-item)
  1869. (org-lparse-end-list 'unordered))
  1870. (aset org-levels-open (1- l) nil))))
  1871. (defvar org-lparse-outline-text-open)
  1872. (defun org-lparse-begin-outline-and-outline-text (level1 snumber title tags
  1873. target extra-targets
  1874. extra-class)
  1875. (org-lparse-begin
  1876. 'OUTLINE level1 snumber title tags target extra-targets extra-class)
  1877. (org-lparse-begin-outline-text level1 snumber extra-class))
  1878. (defun org-lparse-end-outline-text-or-outline ()
  1879. (cond
  1880. (org-lparse-outline-text-open
  1881. (org-lparse-end 'OUTLINE-TEXT)
  1882. (setq org-lparse-outline-text-open nil))
  1883. (t (org-lparse-end 'OUTLINE))))
  1884. (defun org-lparse-begin-outline-text (level1 snumber extra-class)
  1885. (assert (not org-lparse-outline-text-open) t)
  1886. (setq org-lparse-outline-text-open t)
  1887. (org-lparse-begin 'OUTLINE-TEXT level1 snumber extra-class))
  1888. (defun org-lparse-html-list-type-to-canonical-list-type (ltype)
  1889. (cdr (assoc ltype '(("o" . ordered)
  1890. ("u" . unordered)
  1891. ("d" . description)))))
  1892. (defvar org-lparse-list-level) ; dynamically bound in org-do-lparse
  1893. (defun org-lparse-begin-list (ltype)
  1894. (incf org-lparse-list-level)
  1895. (org-lparse-begin 'LIST ltype))
  1896. (defun org-lparse-end-list (ltype)
  1897. (decf org-lparse-list-level)
  1898. (org-lparse-end 'LIST ltype))
  1899. (defvar org-lparse-table-rowgrp-info)
  1900. (defun org-lparse-begin-table-rowgroup (&optional is-header-row)
  1901. (push (cons (1+ org-lparse-table-rownum) :start) org-lparse-table-rowgrp-info)
  1902. (org-lparse-begin 'TABLE-ROWGROUP is-header-row))
  1903. (defun org-lparse-end-table ()
  1904. (when org-lparse-table-is-styled
  1905. ;; column groups
  1906. (unless (car org-table-colgroup-info)
  1907. (setq org-table-colgroup-info
  1908. (cons :start (cdr org-table-colgroup-info))))
  1909. ;; column alignment
  1910. (let ((c -1))
  1911. (mapc
  1912. (lambda (x)
  1913. (incf c)
  1914. (setf (aref org-lparse-table-colalign-vector c)
  1915. (or (aref org-lparse-table-colalign-vector c)
  1916. (if (> (/ (float x) (1+ org-lparse-table-rownum))
  1917. org-table-number-fraction)
  1918. "right" "left"))))
  1919. org-lparse-table-num-numeric-items-per-column)))
  1920. (org-lparse-end 'TABLE))
  1921. (defvar org-lparse-encode-pending nil)
  1922. (defun org-lparse-format-tags (tag text prefix suffix &rest args)
  1923. (cond
  1924. ((consp tag)
  1925. (concat prefix (apply 'format (car tag) args) text suffix
  1926. (format (cdr tag))))
  1927. ((stringp tag) ; singleton tag
  1928. (concat prefix (apply 'format tag args) text))))
  1929. (defun org-xml-fix-class-name (kwd) ; audit callers of this function
  1930. "Turn todo keyword into a valid class name.
  1931. Replaces invalid characters with \"_\"."
  1932. (save-match-data
  1933. (while (string-match "[^a-zA-Z0-9_]" kwd)
  1934. (setq kwd (replace-match "_" t t kwd))))
  1935. kwd)
  1936. (defun org-lparse-format-todo (todo)
  1937. (org-lparse-format 'FONTIFY
  1938. (concat
  1939. (ignore-errors (org-lparse-get 'TODO-KWD-CLASS-PREFIX))
  1940. (org-xml-fix-class-name todo))
  1941. (list (if (member todo org-done-keywords) "done" "todo")
  1942. todo)))
  1943. (defun org-lparse-format-extra-targets (extra-targets)
  1944. (if (not extra-targets) ""
  1945. (mapconcat (lambda (x)
  1946. (setq x (org-solidify-link-text
  1947. (if (org-uuidgen-p x) (concat "ID-" x) x)))
  1948. (org-lparse-format 'ANCHOR "" x))
  1949. extra-targets "")))
  1950. (defun org-lparse-format-org-tags (tags)
  1951. (if (not tags) ""
  1952. (org-lparse-format
  1953. 'FONTIFY (mapconcat
  1954. (lambda (x)
  1955. (org-lparse-format
  1956. 'FONTIFY x
  1957. (concat
  1958. (ignore-errors (org-lparse-get 'TAG-CLASS-PREFIX))
  1959. (org-xml-fix-class-name x))))
  1960. (org-split-string tags ":")
  1961. (org-lparse-format 'SPACES 1)) "tag")))
  1962. (defun org-lparse-format-section-number (&optional snumber level)
  1963. (and org-export-with-section-numbers
  1964. (not org-lparse-body-only) snumber level
  1965. (org-lparse-format 'FONTIFY snumber (format "section-number-%d" level))))
  1966. (defun org-lparse-warn (msg)
  1967. (if (not org-lparse-use-flashy-warning)
  1968. (message msg)
  1969. (put-text-property 0 (length msg) 'face 'font-lock-warning-face msg)
  1970. (message msg)
  1971. (sleep-for 3)))
  1972. (defun org-xml-format-href (s)
  1973. "Make sure the S is valid as a href reference in an XHTML document."
  1974. (save-match-data
  1975. (let ((start 0))
  1976. (while (string-match "&" s start)
  1977. (setq start (+ (match-beginning 0) 3)
  1978. s (replace-match "&amp;" t t s)))))
  1979. s)
  1980. (defun org-xml-format-desc (s)
  1981. "Make sure the S is valid as a description in a link."
  1982. (if (and s (not (get-text-property 1 'org-protected s)))
  1983. (save-match-data
  1984. (org-xml-encode-org-text s))
  1985. s))
  1986. (provide 'org-lparse)
  1987. ;;; org-lparse.el ends here