org-lparse.el 81 KB

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