org-lparse.el 70 KB

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