org-lparse.el 72 KB

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