org-lparse.el 83 KB

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