org-lparse.el 72 KB

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