org-lparse.el 83 KB

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