org-lparse.el 83 KB

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