org-lparse.el 82 KB

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