org-lparse.el 82 KB

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