org-lparse.el 82 KB

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