org-lparse.el 78 KB

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