org-lparse.el 82 KB

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