org-lparse.el 82 KB

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