org-lparse.el 79 KB

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