org-lparse.el 83 KB

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