org-lparse.el 83 KB

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