org-lparse.el 87 KB

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