org-lparse.el 87 KB

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