org-lparse.el 83 KB

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