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
  233. (org-lparse-format
  234. 'ORG-LINK opt-plist type "" (format "coderef-%s" path)
  235. (format
  236. (org-export-get-coderef-format
  237. path
  238. (and descp desc))
  239. (cdr (assoc path org-export-code-refs))) nil descp)))
  240. ((functionp (setq fnc (nth 2 (assoc type org-link-protocols))))
  241. ;; The link protocol has a function for format the link
  242. (setq rpl
  243. (save-match-data
  244. (funcall fnc (org-link-unescape path) desc1 'html))))
  245. ((string= type "file")
  246. ;; FILE link
  247. (save-match-data
  248. (let*
  249. ((components
  250. (if
  251. (string-match "::\\(.*\\)" path)
  252. (list
  253. (replace-match "" t nil path)
  254. (match-string 1 path))
  255. (list path nil)))
  256. ;;The proper path, without a fragment
  257. (path-1
  258. (first components))
  259. ;;The raw fragment
  260. (fragment-0
  261. (second components))
  262. ;;Check the fragment. If it can't be used as
  263. ;;target fragment we'll pass nil instead.
  264. (fragment-1
  265. (if
  266. (and fragment-0
  267. (not (string-match "^[0-9]*$" fragment-0))
  268. (not (string-match "^\\*" fragment-0))
  269. (not (string-match "^/.*/$" fragment-0)))
  270. (org-solidify-link-text
  271. (org-link-unescape fragment-0))
  272. nil))
  273. (desc-2
  274. ;;Description minus "file:" and ".org"
  275. (if (string-match "^file:" desc)
  276. (let
  277. ((desc-1 (replace-match "" t t desc)))
  278. (if (string-match "\\.org$" desc-1)
  279. (replace-match "" t t desc-1)
  280. desc-1))
  281. desc)))
  282. (setq rpl
  283. (if
  284. (and
  285. (functionp link-validate)
  286. (not (funcall link-validate path-1 current-dir)))
  287. desc
  288. (org-lparse-format
  289. 'ORG-LINK opt-plist "file" path-1 fragment-1
  290. desc-2 attr descp))))))
  291. (t
  292. ;; just publish the path, as default
  293. (setq rpl (concat "<i>&lt;" type ":"
  294. (save-match-data (org-link-unescape path))
  295. "&gt;</i>"))))
  296. (setq line (replace-match rpl t t line)
  297. start (+ start (length rpl))))
  298. line))
  299. (defmacro with-org-lparse-preserve-paragraph-state (&rest body)
  300. `(let ((org-lparse-do-open-par org-lparse-par-open))
  301. (org-lparse-end-paragraph)
  302. ,@body
  303. (when org-lparse-do-open-par
  304. (org-lparse-begin-paragraph))))
  305. (defvar org-lparse-native-backends nil
  306. "List of native backends registered with `org-lparse'.
  307. A backend can use `org-lparse-register-backend' to add itself to
  308. this list.
  309. All native backends must implement a get routine and a mandatory
  310. set of callback routines.
  311. The get routine must be named as org-<backend>-get where backend
  312. is the name of the backend. The exporter uses `org-lparse-get'
  313. and retrieves the backend-specific callback by querying for
  314. ENTITY-CONTROL and ENTITY-FORMAT variables.
  315. For the sake of illustration, the html backend implements
  316. `org-xhtml-get'. It returns
  317. `org-xhtml-entity-control-callbacks-alist' and
  318. `org-xhtml-entity-format-callbacks-alist' as the values of
  319. ENTITY-CONTROL and ENTITY-FORMAT settings.")
  320. (defun org-lparse-register-backend (backend)
  321. "Make BACKEND known to `org-lparse' library.
  322. Add BACKEND to `org-lparse-native-backends'."
  323. (when backend
  324. (setq backend (cond
  325. ((symbolp backend) (symbol-name backend))
  326. ((stringp backend) backend)
  327. (t (error "Error while registering backend: %S" backend))))
  328. (add-to-list 'org-lparse-native-backends backend)))
  329. (defun org-lparse-unregister-backend (backend)
  330. (setq org-lparse-native-backends
  331. (remove (cond
  332. ((symbolp backend) (symbol-name backend))
  333. ((stringp backend) backend))
  334. org-lparse-native-backends))
  335. (message "Unregistered backend %S" backend))
  336. (defun org-lparse-get-other-backends (in-fmt)
  337. "Return OUTPUT-FMT-ALIST corresponding to IN-FMT.
  338. See `org-lparse-convert-capabilities' for definition of
  339. OUTPUT-FMT-ALIST."
  340. (when (org-lparse-get-converter in-fmt)
  341. (or (ignore-errors (org-lparse-backend-get in-fmt 'OTHER-BACKENDS) )
  342. (catch 'out-fmts
  343. (dolist (c org-lparse-convert-capabilities)
  344. (when (member in-fmt (nth 1 c))
  345. (throw 'out-fmts (nth 2 c))))))))
  346. (defun org-lparse-get-converter (in-fmt)
  347. "Return converter associated with IN-FMT.
  348. See `org-lparse-convert-capabilities' for further information."
  349. (or (ignore-errors (org-lparse-backend-get in-fmt 'CONVERT-METHOD))
  350. org-lparse-convert-process))
  351. (defun org-lparse-all-backends ()
  352. "Return all formats to which `org-lparse' could export to.
  353. The return value is an alist of the form (TARGET-BACKEND
  354. NATIVE-BACKEND-1 NATIVE-BACKEND-2 ...) with the condition that
  355. the org file can be exported to TARGET-BACKEND via any one of
  356. NATIVE-BACKEND-1, NATIVE-BACKEND-2 etc.
  357. For example, an entry of the form \"(\"pdf\" \"odt\" \"xhtml\")\"
  358. would mean that the org file could be exported to \"pdf\" format
  359. by exporting natively either to \"xhtml\" or \"odt\" backends."
  360. (let (all-backends)
  361. (flet ((add (other native)
  362. (let ((val (assoc-string other all-backends t)))
  363. (if val (setcdr val (nconc (list native) (cdr val)))
  364. (push (cons other (list native)) all-backends)))))
  365. (loop for backend in org-lparse-native-backends
  366. do (loop for other in
  367. (mapcar #'car (org-lparse-get-other-backends backend))
  368. do (add other backend))))
  369. all-backends))
  370. (defun org-lparse-backend-is-native-p (backend)
  371. (member backend org-lparse-native-backends))
  372. (defun org-lparse (target-backend native-backend arg
  373. &optional hidden ext-plist
  374. to-buffer body-only pub-dir)
  375. "Export the outline to various formats.
  376. If there is an active region, export only the region. The
  377. outline is first exported to NATIVE-BACKEND and optionally
  378. converted to TARGET-BACKEND. See `org-lparse-native-backends'
  379. for list of known native backends. Each native backend can
  380. specify a converter and list of target backends it exports to
  381. using the CONVERT-PROCESS and OTHER-BACKENDS settings of it's get
  382. method. See `org-xhtml-get' for an illustrative example.
  383. ARG is a prefix argument that specifies how many levels of
  384. outline should become headlines. The default is 3. Lower levels
  385. will become bulleted lists.
  386. HIDDEN is obsolete and does nothing.
  387. EXT-PLIST is a property list that controls various aspects of
  388. export. The settings here override org-mode's default settings
  389. and but are inferior to file-local settings.
  390. TO-BUFFER dumps the exported lines to a buffer or a string
  391. instead of a file. If TO-BUFFER is the symbol `string' return the
  392. exported lines as a string. If TO-BUFFER is non-nil, create a
  393. buffer with that name and export to that buffer.
  394. BODY-ONLY controls the presence of header and footer lines in
  395. exported text. If BODY-ONLY is non-nil, don't produce the file
  396. header and footer, simply return the content of <body>...</body>,
  397. without even the body tags themselves.
  398. PUB-DIR specifies the publishing directory."
  399. (interactive
  400. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  401. (all-backends (org-lparse-all-backends))
  402. (target-backend
  403. (and all-backends
  404. (funcall input "Export to: " all-backends nil t nil)))
  405. (native-backend
  406. (let ((choices (if target-backend
  407. (cdr (assoc target-backend all-backends))
  408. (or org-lparse-native-backends
  409. (error "No registered backends")))))
  410. (funcall input "Use Native backend: " choices nil t nil))))
  411. (list target-backend native-backend current-prefix-arg)))
  412. (let* ((org-lparse-backend (intern native-backend))
  413. (org-lparse-other-backend (and target-backend
  414. (intern target-backend))))
  415. (unless (org-lparse-backend-is-native-p native-backend)
  416. (error "Don't know how to export natively to backend %s" native-backend))
  417. (unless (or (not target-backend)
  418. (equal target-backend native-backend)
  419. (assoc target-backend (org-lparse-get-other-backends
  420. native-backend)))
  421. (error "Don't know how to export to backend %s %s" target-backend
  422. (format "via %s" native-backend)))
  423. (run-hooks 'org-export-first-hook)
  424. (org-do-lparse arg hidden ext-plist to-buffer body-only pub-dir)))
  425. (defcustom org-lparse-convert-processes
  426. '(("BasicODConverter"
  427. ("soffice" "-norestore" "-invisible" "-headless"
  428. "\"macro:///BasicODConverter.Main.Convert(%I,%f,%O)\""))
  429. ("unoconv"
  430. ("unoconv" "-f" "%f" "-o" "%d" "%i")))
  431. "Specify a list of document converters and their usage.
  432. The converters in this list are offered as choices while
  433. customizing `org-lparse-convert-process'.
  434. This variable is an alist where each element is of the
  435. form (CONVERTER-NAME (CONVERTER-PROGRAM ARG1 ARG2 ...)).
  436. CONVERTER-NAME is name of the converter. CONVERTER-PROGRAM is
  437. the name of the executable. ARG1, ARG2 etc are command line
  438. options that are passed to CONVERTER-PROGRAM. Format specifiers
  439. can be used in the ARGs and they are interpreted as below:
  440. %i input file name in full
  441. %I input file name as a URL
  442. %f format of the output file
  443. %o output file name in full
  444. %O output file name as a URL
  445. %d output dir in full
  446. %D output dir as a URL."
  447. :group 'org-lparse
  448. :type
  449. '(choice
  450. (const :tag "None" nil)
  451. (alist :tag "Converters"
  452. :key-type (string :tag "Converter Name")
  453. :value-type (group (cons (string :tag "Executable")
  454. (repeat (string :tag "Command line args")))))))
  455. (defcustom org-lparse-convert-process nil
  456. "Command to convert from an Org exported format to other formats.
  457. During customization, the list of choices are populated from
  458. `org-lparse-convert-processes'. Refer afore-mentioned variable
  459. for further information."
  460. :group 'org-lparse
  461. :type '(choice :convert-widget
  462. (lambda (w)
  463. (apply 'widget-convert (widget-type w)
  464. (eval (car (widget-get w :args)))))
  465. `((const :tag "None" nil)
  466. ,@(mapcar (lambda (c)
  467. `(const :tag ,(car c) ,(cadr c)))
  468. org-lparse-convert-processes))))
  469. (defcustom org-lparse-convert-capabilities
  470. '(("Text"
  471. ("odt" "ott" "doc" "rtf")
  472. (("pdf" "pdf") ("odt" "odt") ("xhtml" "html") ("rtf" "rtf")
  473. ("ott" "ott") ("doc" "doc") ("ooxml" "xml") ("html" "html")))
  474. ("Web"
  475. ("html" "xhtml") (("pdf" "pdf") ("odt" "txt") ("html" "html")))
  476. ("Spreadsheet"
  477. ("ods" "ots" "xls" "csv")
  478. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv")
  479. ("ods" "ods") ("xls" "xls") ("xhtml" "xhtml") ("ooxml" "xml")))
  480. ("Presentation"
  481. ("odp" "otp" "ppt")
  482. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("xhtml" "xml")
  483. ("otp" "otp") ("ppt" "ppt") ("odg" "odg") ("html" "html"))))
  484. "Specify input and output formats of `org-lparse-convert-process'.
  485. More correctly, specify the set of input and output formats that
  486. the user is actually interested in.
  487. This variable is an alist where each element is of the
  488. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  489. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  490. alist where each element is of the form (OUTPUT-FMT
  491. OUTPUT-FILE-EXTENSION).
  492. The variable is interpreted as follows:
  493. `org-lparse-convert-process' can take any document that is in
  494. INPUT-FMT-LIST and produce any document that is in the
  495. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  496. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  497. serves dual purposes:
  498. - It is used for populating completion candidates during
  499. `org-lparse' and `org-lparse-convert' commands.
  500. - It is used as the value of \"%f\" specifier in
  501. `org-lparse-convert-process'.
  502. DOCUMENT-CLASS is used to group a set of file formats in
  503. INPUT-FMT-LIST in to a single class.
  504. Note that this variable inherently captures how LibreOffice based
  505. converters work. LibreOffice maps documents of various formats
  506. to classes like Text, Web, Spreadsheet, Presentation etc and
  507. allow document of a given class (irrespective of it's source
  508. format) to be converted to any of the export formats associated
  509. with that class.
  510. See default setting of this variable for an typical
  511. configuration."
  512. :group 'org-lparse
  513. :type
  514. '(choice
  515. (const :tag "None" nil)
  516. (alist :key-type (string :tag "Document Class")
  517. :value-type
  518. (group (repeat :tag "Input formats" (string :tag "Input format"))
  519. (alist :tag "Output formats"
  520. :key-type (string :tag "Output format")
  521. :value-type
  522. (group (string :tag "Output file extension")))))))
  523. (defcustom org-lparse-use-flashy-warning nil
  524. "Control flashing of messages logged with `org-lparse-warn'.
  525. When non-nil, messages are fontified with warning face and the
  526. exporter lingers for a while to catch user's attention."
  527. :type 'boolean
  528. :group 'org-lparse)
  529. (defun org-lparse-convert (&optional in-file out-fmt prefix-arg)
  530. "Convert IN-FILE to format OUT-FMT using a command line converter.
  531. IN-FILE is the file to be converted. If unspecified, it defaults
  532. to variable `buffer-file-name'. OUT-FMT is the desired output
  533. format. If the backend has registered a CONVERT-METHOD as part
  534. of it's get function then that converter is used. Otherwise
  535. `org-lparse-convert-process' is used. If PREFIX-ARG is non-nil
  536. then the newly converted file is opened using `org-open-file'."
  537. (interactive
  538. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  539. (in-file (read-file-name "File to be converted: "
  540. nil buffer-file-name t))
  541. (in-fmt (file-name-extension in-file))
  542. (out-fmt-choices (org-lparse-get-other-backends in-fmt))
  543. (out-fmt
  544. (or (and out-fmt-choices
  545. (funcall input "Output format: "
  546. out-fmt-choices nil nil nil))
  547. (error
  548. "No known converter or no known output formats for %s files"
  549. in-fmt))))
  550. (list in-file out-fmt current-prefix-arg)))
  551. (require 'browse-url)
  552. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  553. (dummy (or (file-readable-p in-file)
  554. (error "Cannot read %s" in-file)))
  555. (in-fmt (file-name-extension in-file))
  556. (out-fmt (or out-fmt (error "Output format unspecified")))
  557. (convert-process (org-lparse-get-converter in-fmt))
  558. (program (car convert-process))
  559. (dummy (and (or program (error "Converter not configured"))
  560. (or (executable-find program)
  561. (error "Cannot find converter %s" program))))
  562. (out-fmt-alist
  563. (or (assoc out-fmt (org-lparse-get-other-backends in-fmt))
  564. (error "Cannot convert from %s to %s format" in-fmt out-fmt)))
  565. (out-file (concat (file-name-sans-extension in-file) "."
  566. (nth 1 out-fmt-alist)))
  567. (out-dir (file-name-directory in-file))
  568. (arglist (mapcar (lambda (arg)
  569. (format-spec
  570. arg `((?i . ,in-file)
  571. (?I . ,(browse-url-file-url in-file))
  572. (?f . ,out-fmt)
  573. (?o . ,out-file)
  574. (?O . ,(browse-url-file-url out-file))
  575. (?d . ,out-dir)
  576. (?D . ,(browse-url-file-url out-dir)))))
  577. (cdr convert-process))))
  578. (when (file-exists-p out-file)
  579. (delete-file out-file))
  580. (message "Executing %s %s" program (mapconcat 'identity arglist " "))
  581. (apply 'call-process program nil nil nil arglist)
  582. (cond
  583. ((file-exists-p out-file)
  584. (message "Exported to %s using %s" out-file program)
  585. (when prefix-arg
  586. (message "Opening %s..." out-file)
  587. (org-open-file out-file))
  588. out-file
  589. ;; (set-buffer (find-file-noselect out-file))
  590. )
  591. (t
  592. (message "Export to %s failed" out-file)
  593. nil))))
  594. (defvar org-lparse-insert-tag-with-newlines 'both)
  595. ;; Following variables are let-bound during `org-lparse'
  596. (defvar org-lparse-dyn-first-heading-pos)
  597. (defvar org-lparse-toc)
  598. (defvar org-lparse-entity-control-callbacks-alist)
  599. (defvar org-lparse-entity-format-callbacks-alist)
  600. (defvar org-lparse-backend nil
  601. "The native backend to which the document is currently exported.
  602. This variable is let bound during `org-lparse'. Valid values are
  603. one of the symbols corresponding to `org-lparse-native-backends'.
  604. Compare this variable with `org-export-current-backend' which is
  605. bound only during `org-export-preprocess-string' stage of the
  606. export process.
  607. See also `org-lparse-other-backend'.")
  608. (defvar org-lparse-other-backend nil
  609. "The target backend to which the document is currently exported.
  610. This variable is let bound during `org-lparse'. This variable is
  611. set to either `org-lparse-backend' or one of the symbols
  612. corresponding to OTHER-BACKENDS specification of the
  613. org-lparse-backend.
  614. For example, if a document is exported to \"odt\" then both
  615. org-lparse-backend and org-lparse-other-backend are bound to
  616. 'odt. On the other hand, if a document is exported to \"odt\"
  617. and then converted to \"doc\" then org-lparse-backend is set to
  618. 'odt and org-lparse-other-backend is set to 'doc.")
  619. (defvar org-lparse-body-only nil
  620. "Bind this to BODY-ONLY arg of `org-lparse'.")
  621. (defvar org-lparse-to-buffer nil
  622. "Bind this to TO-BUFFER arg of `org-lparse'.")
  623. (defun org-do-lparse (arg &optional hidden ext-plist
  624. to-buffer body-only pub-dir)
  625. "Export the outline to various formats.
  626. See `org-lparse' for more information. This function is a
  627. html-agnostic version of the `org-export-as-html' function in 7.5
  628. version."
  629. ;; Make sure we have a file name when we need it.
  630. (when (and (not (or to-buffer body-only))
  631. (not buffer-file-name))
  632. (if (buffer-base-buffer)
  633. (org-set-local 'buffer-file-name
  634. (with-current-buffer (buffer-base-buffer)
  635. buffer-file-name))
  636. (error "Need a file name to be able to export")))
  637. (org-lparse-warn
  638. (format "Exporting to %s using org-lparse..."
  639. (upcase (symbol-name
  640. (or org-lparse-backend org-lparse-other-backend)))))
  641. (setq-default org-todo-line-regexp org-todo-line-regexp)
  642. (setq-default org-deadline-line-regexp org-deadline-line-regexp)
  643. (setq-default org-done-keywords org-done-keywords)
  644. (setq-default org-maybe-keyword-time-regexp org-maybe-keyword-time-regexp)
  645. (let* (hfy-user-sheet-assoc ; let `htmlfontify' know that
  646. ; we are interested in
  647. ; collecting styles
  648. org-lparse-encode-pending
  649. org-lparse-par-open
  650. ;; list related vars
  651. (org-lparse-list-level 0) ; list level starts at 1. A
  652. ; value of 0 implies we are
  653. ; outside of any list
  654. (org-lparse-list-item-count 0)
  655. org-lparse-list-stack
  656. ;; list-table related vars
  657. org-lparse-list-table-p
  658. org-lparse-list-table:table-cell-open
  659. org-lparse-list-table:table-row
  660. org-lparse-list-table:lines
  661. org-lparse-outline-text-open
  662. (org-lparse-latex-fragment-fallback ; currently used only by
  663. ; odt exporter
  664. (or (ignore-errors (org-lparse-get 'LATEX-FRAGMENT-FALLBACK))
  665. (if (and (org-check-external-command "latex" "" t)
  666. (org-check-external-command "dvipng" "" t))
  667. 'dvipng
  668. 'verbatim)))
  669. (org-lparse-insert-tag-with-newlines 'both)
  670. (org-lparse-to-buffer to-buffer)
  671. (org-lparse-body-only body-only)
  672. (org-lparse-entity-control-callbacks-alist
  673. (org-lparse-get 'ENTITY-CONTROL))
  674. (org-lparse-entity-format-callbacks-alist
  675. (org-lparse-get 'ENTITY-FORMAT))
  676. (opt-plist
  677. (org-export-process-option-filters
  678. (org-combine-plists (org-default-export-plist)
  679. ext-plist
  680. (org-infile-export-plist))))
  681. (body-only (or body-only (plist-get opt-plist :body-only)))
  682. valid org-lparse-dyn-first-heading-pos
  683. (odd org-odd-levels-only)
  684. (region-p (org-region-active-p))
  685. (rbeg (and region-p (region-beginning)))
  686. (rend (and region-p (region-end)))
  687. (subtree-p
  688. (if (plist-get opt-plist :ignore-subtree-p)
  689. nil
  690. (when region-p
  691. (save-excursion
  692. (goto-char rbeg)
  693. (and (org-at-heading-p)
  694. (>= (org-end-of-subtree t t) rend))))))
  695. (level-offset (if subtree-p
  696. (save-excursion
  697. (goto-char rbeg)
  698. (+ (funcall outline-level)
  699. (if org-odd-levels-only 1 0)))
  700. 0))
  701. (opt-plist (setq org-export-opt-plist
  702. (if subtree-p
  703. (org-export-add-subtree-options opt-plist rbeg)
  704. opt-plist)))
  705. ;; The following two are dynamically scoped into other
  706. ;; routines below.
  707. (org-current-export-dir
  708. (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist)))
  709. (org-current-export-file buffer-file-name)
  710. (level 0) (line "") (origline "") txt todo
  711. (umax nil)
  712. (umax-toc nil)
  713. (filename (if to-buffer nil
  714. (expand-file-name
  715. (concat
  716. (file-name-sans-extension
  717. (or (and subtree-p
  718. (org-entry-get (region-beginning)
  719. "EXPORT_FILE_NAME" t))
  720. (file-name-nondirectory buffer-file-name)))
  721. "." (org-lparse-get 'FILE-NAME-EXTENSION opt-plist))
  722. (file-name-as-directory
  723. (or pub-dir (org-lparse-get 'EXPORT-DIR opt-plist))))))
  724. (current-dir (if buffer-file-name
  725. (file-name-directory buffer-file-name)
  726. default-directory))
  727. (buffer (if to-buffer
  728. (cond
  729. ((eq to-buffer 'string)
  730. (get-buffer-create (org-lparse-get 'EXPORT-BUFFER-NAME)))
  731. (t (get-buffer-create to-buffer)))
  732. (find-file-noselect
  733. (or (let ((f (org-lparse-get 'INIT-METHOD)))
  734. (and f (functionp f) (funcall f filename)))
  735. filename))))
  736. (org-levels-open (make-vector org-level-max nil))
  737. (date (plist-get opt-plist :date))
  738. (date (cond
  739. ((and date (string-match "%" date))
  740. (format-time-string date))
  741. (date date)
  742. (t (format-time-string "%Y-%m-%d %T %Z"))))
  743. (dummy (setq opt-plist (plist-put opt-plist :effective-date date)))
  744. (title (org-xml-encode-org-text-skip-links
  745. (or (and subtree-p (org-export-get-title-from-subtree))
  746. (plist-get opt-plist :title)
  747. (and (not body-only)
  748. (not
  749. (plist-get opt-plist :skip-before-1st-heading))
  750. (org-export-grab-title-from-buffer))
  751. (and buffer-file-name
  752. (file-name-sans-extension
  753. (file-name-nondirectory buffer-file-name)))
  754. "UNTITLED")))
  755. (dummy (setq opt-plist (plist-put opt-plist :title title)))
  756. (html-table-tag (plist-get opt-plist :html-table-tag))
  757. (quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  758. (quote-re (concat "^\\(\\*+\\)\\([ \t]+" org-quote-string "\\>\\)"))
  759. (org-lparse-dyn-current-environment nil)
  760. ;; Get the language-dependent settings
  761. (lang-words (or (assoc (plist-get opt-plist :language)
  762. org-export-language-setup)
  763. (assoc "en" org-export-language-setup)))
  764. (dummy (setq opt-plist (plist-put opt-plist :lang-words lang-words)))
  765. (head-count 0) cnt
  766. (start 0)
  767. (coding-system-for-write
  768. (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-WRITE))
  769. (and (boundp 'buffer-file-coding-system)
  770. buffer-file-coding-system)))
  771. (save-buffer-coding-system
  772. (or (ignore-errors (org-lparse-get 'CODING-SYSTEM-FOR-SAVE))
  773. (and (boundp 'buffer-file-coding-system)
  774. buffer-file-coding-system)))
  775. (region
  776. (buffer-substring
  777. (if region-p (region-beginning) (point-min))
  778. (if region-p (region-end) (point-max))))
  779. (org-export-have-math nil)
  780. (org-export-footnotes-seen nil)
  781. (org-export-footnotes-data (org-footnote-all-labels 'with-defs))
  782. (org-footnote-insert-pos-for-preprocessor 'point-min)
  783. (org-lparse-opt-plist opt-plist)
  784. (lines
  785. (org-split-string
  786. (org-export-preprocess-string
  787. region
  788. :emph-multiline t
  789. :for-backend (if (equal org-lparse-backend 'xhtml) ; hack
  790. 'html
  791. org-lparse-backend)
  792. :skip-before-1st-heading
  793. (plist-get opt-plist :skip-before-1st-heading)
  794. :drawers (plist-get opt-plist :drawers)
  795. :todo-keywords (plist-get opt-plist :todo-keywords)
  796. :tasks (plist-get opt-plist :tasks)
  797. :tags (plist-get opt-plist :tags)
  798. :priority (plist-get opt-plist :priority)
  799. :footnotes (plist-get opt-plist :footnotes)
  800. :timestamps (plist-get opt-plist :timestamps)
  801. :archived-trees
  802. (plist-get opt-plist :archived-trees)
  803. :select-tags (plist-get opt-plist :select-tags)
  804. :exclude-tags (plist-get opt-plist :exclude-tags)
  805. :add-text
  806. (plist-get opt-plist :text)
  807. :LaTeX-fragments
  808. (plist-get opt-plist :LaTeX-fragments))
  809. "[\r\n]"))
  810. table-open
  811. table-buffer table-orig-buffer
  812. ind
  813. rpl path attr desc descp desc1 desc2 link
  814. snumber fnc
  815. footnotes footref-seen
  816. org-lparse-output-buffer
  817. org-lparse-footnote-definitions
  818. org-lparse-footnote-number
  819. ;; collection
  820. org-lparse-collect-buffer
  821. (org-lparse-collect-count 0) ; things will get haywire if
  822. ; collections are chained. Use
  823. ; this variable to assert this
  824. ; pre-requisite
  825. org-lparse-toc
  826. href
  827. )
  828. (let ((inhibit-read-only t))
  829. (org-unmodified
  830. (remove-text-properties (point-min) (point-max)
  831. '(:org-license-to-kill t))))
  832. (message "Exporting...")
  833. (org-init-section-numbers)
  834. ;; Switch to the output buffer
  835. (setq org-lparse-output-buffer buffer)
  836. (set-buffer org-lparse-output-buffer)
  837. (let ((inhibit-read-only t)) (erase-buffer))
  838. (fundamental-mode)
  839. (org-install-letbind)
  840. (and (fboundp 'set-buffer-file-coding-system)
  841. (set-buffer-file-coding-system coding-system-for-write))
  842. (let ((case-fold-search nil)
  843. (org-odd-levels-only odd))
  844. ;; create local variables for all options, to make sure all called
  845. ;; functions get the correct information
  846. (mapc (lambda (x)
  847. (set (make-local-variable (nth 2 x))
  848. (plist-get opt-plist (car x))))
  849. org-export-plist-vars)
  850. (setq umax (if arg (prefix-numeric-value arg)
  851. org-export-headline-levels))
  852. (setq umax-toc (if (integerp org-export-with-toc)
  853. (min org-export-with-toc umax)
  854. umax))
  855. (when (and org-export-with-toc (not body-only))
  856. (setq lines (org-lparse-prepare-toc
  857. lines level-offset opt-plist umax-toc)))
  858. (unless body-only
  859. (org-lparse-begin 'DOCUMENT-CONTENT opt-plist)
  860. (org-lparse-begin 'DOCUMENT-BODY opt-plist))
  861. (setq head-count 0)
  862. (org-init-section-numbers)
  863. (org-lparse-begin-paragraph)
  864. (while (setq line (pop lines) origline line)
  865. (catch 'nextline
  866. (when (and (org-lparse-current-environment-p 'quote)
  867. (string-match org-outline-regexp-bol line))
  868. (org-lparse-end-environment 'quote))
  869. (when (org-lparse-current-environment-p 'quote)
  870. (org-lparse-insert 'LINE line)
  871. (throw 'nextline nil))
  872. ;; Fixed-width, verbatim lines (examples)
  873. (when (and org-export-with-fixed-width
  874. (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)" line))
  875. (when (not (org-lparse-current-environment-p 'fixedwidth))
  876. (org-lparse-begin-environment 'fixedwidth))
  877. (org-lparse-insert 'LINE (match-string 3 line))
  878. (when (or (not lines)
  879. (not (string-match "^[ \t]*:\\(\\([ \t]\\|$\\)\\(.*\\)\\)"
  880. (car lines))))
  881. (org-lparse-end-environment 'fixedwidth))
  882. (throw 'nextline nil))
  883. ;; Notes: The baseline version of org-html.el (git commit
  884. ;; 3d802e), while encoutering a *line-long* protected text,
  885. ;; does one of the following two things based on the state
  886. ;; of the export buffer.
  887. ;; 1. If a paragraph element has just been opened and
  888. ;; contains only whitespace as content, insert the
  889. ;; protected text as part of the previous paragraph.
  890. ;; 2. If the paragraph element has already been opened and
  891. ;; contains some valid content insert the protected text
  892. ;; as part of the current paragraph.
  893. ;; I think --->
  894. ;; Scenario 1 mentioned above kicks in when a block of
  895. ;; protected text has to be inserted enbloc. For example,
  896. ;; this happens, when inserting an source or example block
  897. ;; or preformatted content enclosed in #+backend,
  898. ;; #+begin_bakend ... #+end_backend)
  899. ;; Scenario 2 mentioned above kicks in when the protected
  900. ;; text is part of a running sentence. For example this
  901. ;; happens in the case of an *multiline* LaTeX equation that
  902. ;; needs to be inserted verbatim.
  903. ;; org-html.el in the master branch seems to do some
  904. ;; jugglery by moving paragraphs around. Inorder to make
  905. ;; these changes backend-agnostic introduce a new text
  906. ;; property org-native-text and impose the added semantics
  907. ;; that these protected blocks appear outside of a
  908. ;; conventional paragraph element.
  909. ;;
  910. ;; Extra Note: Check whether org-example and org-native-text
  911. ;; are entirely equivalent.
  912. ;; Fixes bug reported by Christian Moe concerning verbatim
  913. ;; LaTeX fragments.
  914. ;; on git commit 533ba3f90250a1f25f494c390d639ea6274f235c
  915. ;; http://repo.or.cz/w/org-mode/org-jambu.git/shortlog/refs/heads/staging
  916. ;; See http://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01379.html
  917. ;; Native Text
  918. (when (and (get-text-property 0 'org-native-text line)
  919. ;; Make sure it is the entire line that is protected
  920. (not (< (or (next-single-property-change
  921. 0 'org-native-text line) 10000)
  922. (length line))))
  923. (let ((ind (get-text-property 0 'original-indentation line)))
  924. (org-lparse-begin-environment 'native)
  925. (org-lparse-insert 'LINE line)
  926. (while (and lines
  927. (or (= (length (car lines)) 0)
  928. (not ind)
  929. (equal ind (get-text-property
  930. 0 'original-indentation (car lines))))
  931. (or (= (length (car lines)) 0)
  932. (get-text-property 0 'org-native-text (car lines))))
  933. (org-lparse-insert 'LINE (pop lines)))
  934. (org-lparse-end-environment 'native))
  935. (throw 'nextline nil))
  936. ;; Protected HTML
  937. (when (and (get-text-property 0 'org-protected line)
  938. ;; Make sure it is the entire line that is protected
  939. (not (< (or (next-single-property-change
  940. 0 'org-protected line) 10000)
  941. (length line))))
  942. (let ((ind (get-text-property 0 'original-indentation line)))
  943. (org-lparse-insert 'LINE line)
  944. (while (and lines
  945. (or (= (length (car lines)) 0)
  946. (not ind)
  947. (equal ind (get-text-property
  948. 0 'original-indentation (car lines))))
  949. (or (= (length (car lines)) 0)
  950. (get-text-property 0 'org-protected (car lines))))
  951. (org-lparse-insert 'LINE (pop lines))))
  952. (throw 'nextline nil))
  953. ;; Blockquotes, verse, and center
  954. (when (string-match "^ORG-\\(.+\\)-\\(START\\|END\\)$" line)
  955. (let* ((style (intern (downcase (match-string 1 line))))
  956. (f (cdr (assoc (match-string 2 line)
  957. '(("START" . org-lparse-begin-environment)
  958. ("END" . org-lparse-end-environment))))))
  959. (when (memq style '(blockquote verse center list-table))
  960. (funcall f style)
  961. (throw 'nextline nil))))
  962. (run-hooks 'org-export-html-after-blockquotes-hook)
  963. (when (org-lparse-current-environment-p 'verse)
  964. (let ((i (org-get-string-indentation line)))
  965. (if (> i 0)
  966. (setq line (concat
  967. (let ((org-lparse-encode-pending t))
  968. (org-lparse-format 'SPACES (* 2 i)))
  969. " " (org-trim line))))
  970. (unless (string-match "\\\\\\\\[ \t]*$" line)
  971. (setq line (concat line "\\\\")))))
  972. ;; make targets to anchors
  973. (setq start 0)
  974. (while (string-match
  975. "<<<?\\([^<>]*\\)>>>?\\((INVISIBLE)\\)?[ \t]*\n?" line start)
  976. (cond
  977. ((get-text-property (match-beginning 1) 'org-protected line)
  978. (setq start (match-end 1)))
  979. ((match-end 2)
  980. (setq line (replace-match
  981. (let ((org-lparse-encode-pending t))
  982. (org-lparse-format
  983. 'ANCHOR "" (org-solidify-link-text
  984. (match-string 1 line))))
  985. t t line)))
  986. ((and org-export-with-toc (equal (string-to-char line) ?*))
  987. ;; FIXME: NOT DEPENDENT on TOC?????????????????????
  988. (setq line (replace-match
  989. (let ((org-lparse-encode-pending t))
  990. (org-lparse-format
  991. 'FONTIFY (match-string 1 line) "target"))
  992. ;; (concat "@<i>" (match-string 1 line) "@</i> ")
  993. t t line)))
  994. (t
  995. (setq line (replace-match
  996. (concat
  997. (let ((org-lparse-encode-pending t))
  998. (org-lparse-format
  999. 'ANCHOR (match-string 1 line)
  1000. (org-solidify-link-text (match-string 1 line))
  1001. "target")) " ")
  1002. t t line)))))
  1003. (let ((org-lparse-encode-pending t))
  1004. (setq line (org-lparse-handle-time-stamps line)))
  1005. ;; replace "&" by "&amp;", "<" and ">" by "&lt;" and "&gt;"
  1006. ;; handle @<..> HTML tags (replace "@&gt;..&lt;" by "<..>")
  1007. ;; Also handle sub_superscripts and checkboxes
  1008. (or (string-match org-table-hline-regexp line)
  1009. (string-match "^[ \t]*\\([+]-\\||[ ]\\)[-+ |]*[+|][ \t]*$" line)
  1010. (setq line (org-xml-encode-org-text-skip-links line)))
  1011. (setq line (org-lparse-format-org-link line opt-plist))
  1012. ;; TODO items
  1013. (if (and (string-match org-todo-line-regexp line)
  1014. (match-beginning 2))
  1015. (setq line (concat
  1016. (substring line 0 (match-beginning 2))
  1017. (org-lparse-format 'TODO (match-string 2 line))
  1018. (substring line (match-end 2)))))
  1019. ;; Does this contain a reference to a footnote?
  1020. (when org-export-with-footnotes
  1021. (setq start 0)
  1022. (while (string-match "\\([^* \t].*?\\)[ \t]*\\[\\([0-9]+\\)\\]" line start)
  1023. ;; Discard protected matches not clearly identified as
  1024. ;; footnote markers.
  1025. (if (or (get-text-property (match-beginning 2) 'org-protected line)
  1026. (not (get-text-property (match-beginning 2) 'org-footnote line)))
  1027. (setq start (match-end 2))
  1028. (let ((n (match-string 2 line)) refcnt a)
  1029. (if (setq a (assoc n footref-seen))
  1030. (progn
  1031. (setcdr a (1+ (cdr a)))
  1032. (setq refcnt (cdr a)))
  1033. (setq refcnt 1)
  1034. (push (cons n 1) footref-seen))
  1035. (setq line
  1036. (replace-match
  1037. (concat
  1038. (or (match-string 1 line) "")
  1039. (org-lparse-format
  1040. 'FOOTNOTE-REFERENCE
  1041. n (cdr (assoc n org-lparse-footnote-definitions))
  1042. refcnt)
  1043. ;; If another footnote is following the
  1044. ;; current one, add a separator.
  1045. (if (save-match-data
  1046. (string-match "\\`\\[[0-9]+\\]"
  1047. (substring line (match-end 0))))
  1048. (ignore-errors
  1049. (org-lparse-get 'FOOTNOTE-SEPARATOR))
  1050. ""))
  1051. t t line))))))
  1052. (cond
  1053. ((string-match "^\\(\\*+\\)[ \t]+\\(.*\\)" line)
  1054. ;; This is a headline
  1055. (setq level (org-tr-level (- (match-end 1) (match-beginning 1)
  1056. level-offset))
  1057. txt (match-string 2 line))
  1058. (if (string-match quote-re0 txt)
  1059. (setq txt (replace-match "" t t txt)))
  1060. (if (<= level (max umax umax-toc))
  1061. (setq head-count (+ head-count 1)))
  1062. (unless org-lparse-dyn-first-heading-pos
  1063. (setq org-lparse-dyn-first-heading-pos (point)))
  1064. (org-lparse-begin-level level txt umax head-count)
  1065. ;; QUOTES
  1066. (when (string-match quote-re line)
  1067. (org-lparse-begin-environment 'quote)))
  1068. ((and org-export-with-tables
  1069. (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)" line))
  1070. (when (not table-open)
  1071. ;; New table starts
  1072. (setq table-open t table-buffer nil table-orig-buffer nil))
  1073. ;; Accumulate lines
  1074. (setq table-buffer (cons line table-buffer)
  1075. table-orig-buffer (cons origline table-orig-buffer))
  1076. (when (or (not lines)
  1077. (not (string-match "^\\([ \t]*\\)\\(|\\|\\+-+\\+\\)"
  1078. (car lines))))
  1079. (setq table-open nil
  1080. table-buffer (nreverse table-buffer)
  1081. table-orig-buffer (nreverse table-orig-buffer))
  1082. (org-lparse-end-paragraph)
  1083. (org-lparse-insert 'TABLE table-buffer table-orig-buffer)))
  1084. ;; Normal lines
  1085. (t
  1086. ;; This line either is list item or end a list.
  1087. (when (get-text-property 0 'list-item line)
  1088. (setq line (org-lparse-export-list-line
  1089. line
  1090. (get-text-property 0 'list-item line)
  1091. (get-text-property 0 'list-struct line)
  1092. (get-text-property 0 'list-prevs line))))
  1093. ;; Horizontal line
  1094. (when (string-match "^[ \t]*-\\{5,\\}[ \t]*$" line)
  1095. (with-org-lparse-preserve-paragraph-state
  1096. (org-lparse-insert 'HORIZONTAL-LINE))
  1097. (throw 'nextline nil))
  1098. ;; Empty lines start a new paragraph. If hand-formatted lists
  1099. ;; are not fully interpreted, lines starting with "-", "+", "*"
  1100. ;; also start a new paragraph.
  1101. (when (string-match "^ [-+*]-\\|^[ \t]*$" line)
  1102. (when org-lparse-footnote-number
  1103. (org-lparse-end-footnote-definition org-lparse-footnote-number)
  1104. (setq org-lparse-footnote-number nil))
  1105. (org-lparse-begin-paragraph))
  1106. ;; Is this the start of a footnote?
  1107. (when org-export-with-footnotes
  1108. (when (and (boundp 'footnote-section-tag-regexp)
  1109. (string-match (concat "^" footnote-section-tag-regexp)
  1110. line))
  1111. ;; ignore this line
  1112. (throw 'nextline nil))
  1113. (when (string-match "^[ \t]*\\[\\([0-9]+\\)\\]" line)
  1114. (org-lparse-end-paragraph)
  1115. (setq org-lparse-footnote-number (match-string 1 line))
  1116. (setq line (replace-match "" t t line))
  1117. (org-lparse-begin-footnote-definition org-lparse-footnote-number)))
  1118. ;; Check if the line break needs to be conserved
  1119. (cond
  1120. ((string-match "\\\\\\\\[ \t]*$" line)
  1121. (setq line (replace-match
  1122. (org-lparse-format 'LINE-BREAK)
  1123. t t line)))
  1124. (org-export-preserve-breaks
  1125. (setq line (concat line (org-lparse-format 'LINE-BREAK)))))
  1126. ;; Check if a paragraph should be started
  1127. (let ((start 0))
  1128. (while (and org-lparse-par-open
  1129. (string-match "\\\\par\\>" line start))
  1130. (error "FIXME")
  1131. ;; Leave a space in the </p> so that the footnote matcher
  1132. ;; does not see this.
  1133. (if (not (get-text-property (match-beginning 0)
  1134. 'org-protected line))
  1135. (setq line (replace-match "</p ><p >" t t line)))
  1136. (setq start (match-end 0))))
  1137. (org-lparse-insert 'LINE line)))))
  1138. ;; Properly close all local lists and other lists
  1139. (when (org-lparse-current-environment-p 'quote)
  1140. (org-lparse-end-environment 'quote))
  1141. (org-lparse-end-level 1 umax)
  1142. ;; the </div> to close the last text-... div.
  1143. (when (and (> umax 0) org-lparse-dyn-first-heading-pos)
  1144. (org-lparse-end-outline-text-or-outline))
  1145. (org-lparse-end 'DOCUMENT-BODY opt-plist)
  1146. (unless body-only
  1147. (org-lparse-end 'DOCUMENT-CONTENT))
  1148. (unless (plist-get opt-plist :buffer-will-be-killed)
  1149. (set-auto-mode t))
  1150. (org-lparse-end 'EXPORT)
  1151. ;; kill collection buffer
  1152. (when org-lparse-collect-buffer
  1153. (kill-buffer org-lparse-collect-buffer))
  1154. (goto-char (point-min))
  1155. (or (org-export-push-to-kill-ring
  1156. (upcase (symbol-name org-lparse-backend)))
  1157. (message "Exporting... done"))
  1158. (cond
  1159. ((not to-buffer)
  1160. (let ((f (org-lparse-get 'SAVE-METHOD)))
  1161. (or (and f (functionp f) (funcall f filename opt-plist))
  1162. (save-buffer)))
  1163. (or (when (and (boundp 'org-lparse-other-backend)
  1164. org-lparse-other-backend
  1165. (not (equal org-lparse-backend org-lparse-other-backend)))
  1166. (let ((org-lparse-convert-process
  1167. (org-lparse-get-converter org-lparse-backend)))
  1168. (when org-lparse-convert-process
  1169. (org-lparse-convert buffer-file-name
  1170. (symbol-name org-lparse-other-backend)))))
  1171. (current-buffer)))
  1172. ((eq to-buffer 'string)
  1173. (prog1 (buffer-substring (point-min) (point-max))
  1174. (kill-buffer (current-buffer))))
  1175. (t (current-buffer))))))
  1176. (defun org-lparse-format-table (lines olines)
  1177. "Retuns backend-specific code for org-type and table-type tables."
  1178. (if (stringp lines)
  1179. (setq lines (org-split-string lines "\n")))
  1180. (if (string-match "^[ \t]*|" (car lines))
  1181. ;; A normal org table
  1182. (org-lparse-format-org-table lines nil)
  1183. ;; Table made by table.el
  1184. (or (org-lparse-format-table-table-using-table-generate-source
  1185. ;; FIXME: Need to take care of this during merge
  1186. (if (eq org-lparse-backend 'xhtml) 'html org-lparse-backend)
  1187. olines
  1188. (not org-export-prefer-native-exporter-for-tables))
  1189. ;; We are here only when table.el table has NO col or row
  1190. ;; spanning and the user prefers using org's own converter for
  1191. ;; exporting of such simple table.el tables.
  1192. (org-lparse-format-table-table lines))))
  1193. (defun org-lparse-table-get-colalign-info (lines)
  1194. (let ((forced-aligns (org-find-text-property-in-string
  1195. 'org-forced-aligns (car lines))))
  1196. (when (and forced-aligns org-table-clean-did-remove-column)
  1197. (setq forced-aligns
  1198. (mapcar (lambda (x) (cons (1- (car x)) (cdr x))) forced-aligns)))
  1199. forced-aligns))
  1200. (defvar org-lparse-table-style)
  1201. (defvar org-lparse-table-ncols)
  1202. (defvar org-lparse-table-rownum)
  1203. (defvar org-lparse-table-is-styled)
  1204. (defvar org-lparse-table-begin-marker)
  1205. (defvar org-lparse-table-num-numeric-items-per-column)
  1206. (defvar org-lparse-table-colalign-info)
  1207. (defvar org-lparse-table-colalign-vector)
  1208. ;; Following variables are defined in org-table.el
  1209. (defvar org-table-number-fraction)
  1210. (defvar org-table-number-regexp)
  1211. (defun org-lparse-org-table-to-list-table (lines &optional splice)
  1212. "Convert org-table to list-table.
  1213. LINES is a list of the form (ROW1 ROW2 ROW3 ...) where each
  1214. element is a `string' representing a single row of org-table.
  1215. Thus each ROW has vertical separators \"|\" separating the table
  1216. fields. A ROW could also be a row-group separator of the form
  1217. \"|---...|\". Return a list of the form (ROW1 ROW2 ROW3
  1218. ...). ROW could either be symbol `:hrule' or a list of the
  1219. form (FIELD1 FIELD2 FIELD3 ...) as appropriate."
  1220. (let (line lines-1)
  1221. (cond
  1222. (splice
  1223. (while (setq line (pop lines))
  1224. (unless (string-match "^[ \t]*|-" line)
  1225. (push (org-split-string line "[ \t]*|[ \t]*") lines-1))))
  1226. (t
  1227. (while (setq line (pop lines))
  1228. (cond
  1229. ((string-match "^[ \t]*|-" line)
  1230. (when lines
  1231. (push :hrule lines-1)))
  1232. (t
  1233. (push (org-split-string line "[ \t]*|[ \t]*") lines-1))))))
  1234. (nreverse lines-1)))
  1235. (defun org-lparse-do-format-org-table (lines &optional splice)
  1236. "Format a org-type table into backend-specific code.
  1237. LINES is a list of lines. Optional argument SPLICE means, do not
  1238. insert header and surrounding <table> tags, just format the lines.
  1239. Optional argument NO-CSS means use XHTML attributes instead of CSS
  1240. for formatting. This is required for the DocBook exporter."
  1241. (require 'org-table)
  1242. ;; Get rid of hlines at beginning and end
  1243. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1244. (setq lines (nreverse lines))
  1245. (if (string-match "^[ \t]*|-" (car lines)) (setq lines (cdr lines)))
  1246. (setq lines (nreverse lines))
  1247. (when org-export-table-remove-special-lines
  1248. ;; Check if the table has a marking column. If yes remove the
  1249. ;; column and the special lines
  1250. (setq lines (org-table-clean-before-export lines)))
  1251. (let* ((caption (org-find-text-property-in-string 'org-caption (car lines)))
  1252. (caption (and caption (org-xml-encode-org-text caption)))
  1253. (label (org-find-text-property-in-string 'org-label (car lines)))
  1254. (org-lparse-table-colalign-info (org-lparse-table-get-colalign-info lines))
  1255. (attributes (org-find-text-property-in-string 'org-attributes
  1256. (car lines)))
  1257. (head (and org-export-highlight-first-table-line
  1258. (delq nil (mapcar
  1259. (lambda (x) (string-match "^[ \t]*|-" x))
  1260. (cdr lines))))))
  1261. (setq lines (org-lparse-org-table-to-list-table lines splice))
  1262. (org-lparse-do-format-list-table
  1263. lines splice caption label attributes head org-lparse-table-colalign-info)))
  1264. (defun org-lparse-do-format-list-table (lines &optional splice
  1265. caption label attributes head
  1266. org-lparse-table-colalign-info)
  1267. (or (featurep 'org-table) ; required for
  1268. (require 'org-table)) ; `org-table-number-regexp'
  1269. (let* ((org-lparse-table-rownum -1) org-lparse-table-ncols i (cnt 0)
  1270. tbopen fields line
  1271. org-lparse-table-cur-rowgrp-is-hdr
  1272. org-lparse-table-rowgrp-open
  1273. org-lparse-table-num-numeric-items-per-column
  1274. org-lparse-table-colalign-vector n
  1275. org-lparse-table-rowgrp-info
  1276. org-lparse-table-begin-marker
  1277. (org-lparse-table-style 'org-table)
  1278. org-lparse-table-is-styled)
  1279. (cond
  1280. (splice
  1281. (setq org-lparse-table-is-styled nil)
  1282. (while (setq line (pop lines))
  1283. (insert (org-lparse-format-table-row line) "\n")))
  1284. (t
  1285. (setq org-lparse-table-is-styled t)
  1286. (org-lparse-begin 'TABLE caption label attributes)
  1287. (setq org-lparse-table-begin-marker (point))
  1288. (org-lparse-begin-table-rowgroup head)
  1289. (while (setq line (pop lines))
  1290. (cond
  1291. ((equal line :hrule)
  1292. (org-lparse-begin-table-rowgroup))
  1293. (t
  1294. (insert (org-lparse-format-table-row line) "\n"))))
  1295. (org-lparse-end 'TABLE-ROWGROUP)
  1296. (org-lparse-end-table)))))
  1297. (defun org-lparse-format-org-table (lines &optional splice)
  1298. (with-temp-buffer
  1299. (org-lparse-do-format-org-table lines splice)
  1300. (buffer-substring-no-properties (point-min) (point-max))))
  1301. (defun org-lparse-format-list-table (lines &optional splice)
  1302. (with-temp-buffer
  1303. (org-lparse-do-format-list-table lines splice)
  1304. (buffer-substring-no-properties (point-min) (point-max))))
  1305. (defun org-lparse-do-format-table-table (lines)
  1306. "Format a table generated by table.el into backend-specific code.
  1307. This conversion does *not* use `table-generate-source' from table.el.
  1308. This has the advantage that Org-mode's HTML conversions can be used.
  1309. But it has the disadvantage, that no cell- or row-spanning is allowed."
  1310. (let (line field-buffer
  1311. (org-lparse-table-cur-rowgrp-is-hdr
  1312. org-export-highlight-first-table-line)
  1313. (caption nil)
  1314. (attributes nil)
  1315. (label nil)
  1316. (org-lparse-table-style 'table-table)
  1317. (org-lparse-table-is-styled nil)
  1318. fields org-lparse-table-ncols i (org-lparse-table-rownum -1)
  1319. (empty (org-lparse-format 'SPACES 1)))
  1320. (org-lparse-begin 'TABLE caption label attributes)
  1321. (while (setq line (pop lines))
  1322. (cond
  1323. ((string-match "^[ \t]*\\+-" line)
  1324. (when field-buffer
  1325. (let ((org-export-table-row-tags '("<tr>" . "</tr>"))
  1326. ;; (org-export-html-table-use-header-tags-for-first-column nil)
  1327. )
  1328. (insert (org-lparse-format-table-row field-buffer empty)))
  1329. (setq org-lparse-table-cur-rowgrp-is-hdr nil)
  1330. (setq field-buffer nil)))
  1331. (t
  1332. ;; Break the line into fields and store the fields
  1333. (setq fields (org-split-string line "[ \t]*|[ \t]*"))
  1334. (if field-buffer
  1335. (setq field-buffer (mapcar
  1336. (lambda (x)
  1337. (concat x (org-lparse-format 'LINE-BREAK)
  1338. (pop fields)))
  1339. field-buffer))
  1340. (setq field-buffer fields)))))
  1341. (org-lparse-end-table)))
  1342. (defun org-lparse-format-table-table (lines)
  1343. (with-temp-buffer
  1344. (org-lparse-do-format-table-table lines)
  1345. (buffer-substring-no-properties (point-min) (point-max))))
  1346. (defvar table-source-languages) ; defined in table.el
  1347. (defun org-lparse-format-table-table-using-table-generate-source (backend
  1348. lines
  1349. &optional
  1350. spanned-only)
  1351. "Format a table into BACKEND, using `table-generate-source' from table.el.
  1352. Use SPANNED-ONLY to suppress exporting of simple table.el tables.
  1353. When SPANNED-ONLY is nil, all table.el tables are exported. When
  1354. SPANNED-ONLY is non-nil, only tables with either row or column
  1355. spans are exported.
  1356. This routine returns the generated source or nil as appropriate.
  1357. Refer docstring of `org-export-prefer-native-exporter-for-tables'
  1358. for further information."
  1359. (require 'table)
  1360. (with-current-buffer (get-buffer-create " org-tmp1 ")
  1361. (erase-buffer)
  1362. (insert (mapconcat 'identity lines "\n"))
  1363. (goto-char (point-min))
  1364. (if (not (re-search-forward "|[^+]" nil t))
  1365. (error "Error processing table"))
  1366. (table-recognize-table)
  1367. (when (or (not spanned-only)
  1368. (let* ((dim (table-query-dimension))
  1369. (c (nth 4 dim)) (r (nth 5 dim)) (cells (nth 6 dim)))
  1370. (not (= (* c r) cells))))
  1371. (with-current-buffer (get-buffer-create " org-tmp2 ") (erase-buffer))
  1372. (cond
  1373. ((member backend table-source-languages)
  1374. (table-generate-source backend " org-tmp2 ")
  1375. (set-buffer " org-tmp2 ")
  1376. (buffer-substring (point-min) (point-max)))
  1377. (t
  1378. ;; table.el doesn't support the given backend. Currently this
  1379. ;; happens in case of odt export. Strip the table from the
  1380. ;; generated document. A better alternative would be to embed
  1381. ;; the table as ascii text in the output document.
  1382. (org-lparse-warn
  1383. (concat
  1384. "Found table.el-type table in the source org file. "
  1385. (format "table.el doesn't support %s backend. "
  1386. (upcase (symbol-name backend)))
  1387. "Skipping ahead ..."))
  1388. "")))))
  1389. (defun org-lparse-handle-time-stamps (s)
  1390. "Format time stamps in string S, or remove them."
  1391. (catch 'exit
  1392. (let (r b)
  1393. (while (string-match org-maybe-keyword-time-regexp s)
  1394. (or b (setq b (substring s 0 (match-beginning 0))))
  1395. (setq r (concat
  1396. r (substring s 0 (match-beginning 0)) " "
  1397. (org-lparse-format
  1398. 'FONTIFY
  1399. (concat
  1400. (if (match-end 1)
  1401. (org-lparse-format
  1402. 'FONTIFY
  1403. (match-string 1 s) "timestamp-kwd"))
  1404. " "
  1405. (org-lparse-format
  1406. 'FONTIFY
  1407. (substring (org-translate-time (match-string 3 s)) 1 -1)
  1408. "timestamp"))
  1409. "timestamp-wrapper"))
  1410. s (substring s (match-end 0))))
  1411. ;; Line break if line started and ended with time stamp stuff
  1412. (if (not r)
  1413. s
  1414. (setq r (concat r s))
  1415. (unless (string-match "\\S-" (concat b s))
  1416. (setq r (concat r (org-lparse-format 'LINE-BREAK))))
  1417. r))))
  1418. (defun org-xml-encode-plain-text (s)
  1419. "Convert plain text characters to HTML equivalent.
  1420. Possible conversions are set in `org-export-html-protect-char-alist'."
  1421. (let ((cl (org-lparse-get 'PLAIN-TEXT-MAP)) c)
  1422. (while (setq c (pop cl))
  1423. (let ((start 0))
  1424. (while (string-match (car c) s start)
  1425. (setq s (replace-match (cdr c) t t s)
  1426. start (1+ (match-beginning 0))))))
  1427. s))
  1428. (defun org-xml-encode-org-text-skip-links (string)
  1429. "Prepare STRING for HTML export. Apply all active conversions.
  1430. If there are links in the string, don't modify these."
  1431. (let* ((re (concat org-bracket-link-regexp "\\|"
  1432. (org-re "[ \t]+\\(:[[:alnum:]_@#%:]+:\\)[ \t]*$")))
  1433. m s l res)
  1434. (while (setq m (string-match re string))
  1435. (setq s (substring string 0 m)
  1436. l (match-string 0 string)
  1437. string (substring string (match-end 0)))
  1438. (push (org-xml-encode-org-text s) res)
  1439. (push l res))
  1440. (push (org-xml-encode-org-text string) res)
  1441. (apply 'concat (nreverse res))))
  1442. (defun org-xml-encode-org-text (s)
  1443. "Apply all active conversions to translate special ASCII to HTML."
  1444. (setq s (org-xml-encode-plain-text s))
  1445. (if org-export-html-expand
  1446. (while (string-match "@&lt;\\([^&]*\\)&gt;" s)
  1447. (setq s (replace-match "<\\1>" t nil s))))
  1448. (if org-export-with-emphasize
  1449. (setq s (org-lparse-apply-char-styles s)))
  1450. (if org-export-with-special-strings
  1451. (setq s (org-lparse-convert-special-strings s)))
  1452. (if org-export-with-sub-superscripts
  1453. (setq s (org-lparse-apply-sub-superscript-styles s)))
  1454. (if org-export-with-TeX-macros
  1455. (let ((start 0) wd rep)
  1456. (while (setq start (string-match "\\\\\\([a-zA-Z]+[0-9]*\\)\\({}\\)?"
  1457. s start))
  1458. (if (get-text-property (match-beginning 0) 'org-protected s)
  1459. (setq start (match-end 0))
  1460. (setq wd (match-string 1 s))
  1461. (if (setq rep (org-lparse-format 'ORG-ENTITY wd))
  1462. (setq s (replace-match rep t t s))
  1463. (setq start (+ start (length wd))))))))
  1464. s)
  1465. (defun org-lparse-convert-special-strings (string)
  1466. "Convert special characters in STRING to HTML."
  1467. (let ((all (org-lparse-get 'SPECIAL-STRING-REGEXPS))
  1468. e a re rpl start)
  1469. (while (setq a (pop all))
  1470. (setq re (car a) rpl (cdr a) start 0)
  1471. (while (string-match re string start)
  1472. (if (get-text-property (match-beginning 0) 'org-protected string)
  1473. (setq start (match-end 0))
  1474. (setq string (replace-match rpl t nil string)))))
  1475. string))
  1476. (defun org-lparse-apply-sub-superscript-styles (string)
  1477. "Apply subscript and superscript styles to STRING.
  1478. Use `org-export-with-sub-superscripts' to control application of
  1479. sub and superscript styles."
  1480. (let (key c (s 0) (requireb (eq org-export-with-sub-superscripts '{})))
  1481. (while (string-match org-match-substring-regexp string s)
  1482. (cond
  1483. ((and requireb (match-end 8)) (setq s (match-end 2)))
  1484. ((get-text-property (match-beginning 2) 'org-protected string)
  1485. (setq s (match-end 2)))
  1486. (t
  1487. (setq s (match-end 1)
  1488. key (if (string= (match-string 2 string) "_")
  1489. 'subscript 'superscript)
  1490. c (or (match-string 8 string)
  1491. (match-string 6 string)
  1492. (match-string 5 string))
  1493. string (replace-match
  1494. (concat (match-string 1 string)
  1495. (org-lparse-format 'FONTIFY c key))
  1496. t t string)))))
  1497. (while (string-match "\\\\\\([_^]\\)" string)
  1498. (setq string (replace-match (match-string 1 string) t t string)))
  1499. string))
  1500. (defvar org-lparse-char-styles
  1501. `(("*" bold)
  1502. ("/" emphasis)
  1503. ("_" underline)
  1504. ("=" code)
  1505. ("~" verbatim)
  1506. ("+" strike))
  1507. "Map Org emphasis markers to char styles.
  1508. This is an alist where each element is of the
  1509. form (ORG-EMPHASIS-CHAR . CHAR-STYLE).")
  1510. (defun org-lparse-apply-char-styles (string)
  1511. "Apply char styles to STRING.
  1512. The variable `org-lparse-char-styles' controls how the Org
  1513. emphasis markers are interpreted."
  1514. (let ((s 0) rpl)
  1515. (while (string-match org-emph-re string s)
  1516. (if (not (equal
  1517. (substring string (match-beginning 3) (1+ (match-beginning 3)))
  1518. (substring string (match-beginning 4) (1+ (match-beginning 4)))))
  1519. (setq s (match-beginning 0)
  1520. rpl
  1521. (concat
  1522. (match-string 1 string)
  1523. (org-lparse-format
  1524. 'FONTIFY (match-string 4 string)
  1525. (nth 1 (assoc (match-string 3 string)
  1526. org-lparse-char-styles)))
  1527. (match-string 5 string))
  1528. string (replace-match rpl t t string)
  1529. s (+ s (- (length rpl) 2)))
  1530. (setq s (1+ s))))
  1531. string))
  1532. (defun org-lparse-export-list-line (line pos struct prevs)
  1533. "Insert list syntax in export buffer. Return LINE, maybe modified.
  1534. POS is the item position or line position the line had before
  1535. modifications to buffer. STRUCT is the list structure. PREVS is
  1536. the alist of previous items."
  1537. (let* ((get-type
  1538. (function
  1539. ;; Translate type of list containing POS to "d", "o" or
  1540. ;; "u".
  1541. (lambda (pos struct prevs)
  1542. (let ((type (org-list-get-list-type pos struct prevs)))
  1543. (cond
  1544. ((eq 'ordered type) "o")
  1545. ((eq 'descriptive type) "d")
  1546. (t "u"))))))
  1547. (get-closings
  1548. (function
  1549. ;; Return list of all items and sublists ending at POS, in
  1550. ;; reverse order.
  1551. (lambda (pos)
  1552. (let (out)
  1553. (catch 'exit
  1554. (mapc (lambda (e)
  1555. (let ((end (nth 6 e))
  1556. (item (car e)))
  1557. (cond
  1558. ((= end pos) (push item out))
  1559. ((>= item pos) (throw 'exit nil)))))
  1560. struct))
  1561. out)))))
  1562. ;; First close any previous item, or list, ending at POS.
  1563. (mapc (lambda (e)
  1564. (let* ((lastp (= (org-list-get-last-item e struct prevs) e))
  1565. (first-item (org-list-get-list-begin e struct prevs))
  1566. (type (funcall get-type first-item struct prevs)))
  1567. (org-lparse-end-paragraph)
  1568. ;; Ending for every item
  1569. (org-lparse-end-list-item-1 type)
  1570. ;; We're ending last item of the list: end list.
  1571. (when lastp
  1572. (org-lparse-end-list type)
  1573. (org-lparse-begin-paragraph))))
  1574. (funcall get-closings pos))
  1575. (cond
  1576. ;; At an item: insert appropriate tags in export buffer.
  1577. ((assq pos struct)
  1578. (string-match
  1579. (concat "[ \t]*\\(\\S-+[ \t]*\\)"
  1580. "\\(?:\\[@\\(?:start:\\)?\\([0-9]+\\|[A-Za-z]\\)\\][ \t]*\\)?"
  1581. "\\(?:\\(\\[[ X-]\\]\\)[ \t]+\\)?"
  1582. "\\(?:\\(.*\\)[ \t]+::\\(?:[ \t]+\\|$\\)\\)?"
  1583. "\\(.*\\)") line)
  1584. (let* ((checkbox (match-string 3 line))
  1585. (desc-tag (or (match-string 4 line) "???"))
  1586. (body (or (match-string 5 line) ""))
  1587. (list-beg (org-list-get-list-begin pos struct prevs))
  1588. (firstp (= list-beg pos))
  1589. ;; Always refer to first item to determine list type, in
  1590. ;; case list is ill-formed.
  1591. (type (funcall get-type list-beg struct prevs))
  1592. (counter (let ((count-tmp (org-list-get-counter pos struct)))
  1593. (cond
  1594. ((not count-tmp) nil)
  1595. ((string-match "[A-Za-z]" count-tmp)
  1596. (- (string-to-char (upcase count-tmp)) 64))
  1597. ((string-match "[0-9]+" count-tmp)
  1598. count-tmp)))))
  1599. (when firstp
  1600. (org-lparse-end-paragraph)
  1601. (org-lparse-begin-list type))
  1602. (let ((arg (cond ((equal type "d") desc-tag)
  1603. ((equal type "o") counter))))
  1604. (org-lparse-begin-list-item type arg))
  1605. ;; If line had a checkbox, some additional modification is required.
  1606. (when checkbox
  1607. (setq body
  1608. (concat
  1609. (org-lparse-format
  1610. 'FONTIFY (concat
  1611. "["
  1612. (cond
  1613. ((string-match "X" checkbox) "X")
  1614. ((string-match " " checkbox)
  1615. (org-lparse-format 'SPACES 1))
  1616. (t "-"))
  1617. "]")
  1618. 'code)
  1619. " "
  1620. body)))
  1621. ;; Return modified line
  1622. body))
  1623. ;; At a list ender: go to next line (side-effects only).
  1624. ((equal "ORG-LIST-END-MARKER" line) (throw 'nextline nil))
  1625. ;; Not at an item: return line unchanged (side-effects only).
  1626. (t line))))
  1627. (defun org-lparse-bind-local-variables (opt-plist)
  1628. (mapc (lambda (x)
  1629. (set (make-local-variable (nth 2 x))
  1630. (plist-get opt-plist (car x))))
  1631. org-export-plist-vars))
  1632. (defvar org-lparse-table-rowgrp-open)
  1633. (defvar org-lparse-table-cur-rowgrp-is-hdr)
  1634. (defvar org-lparse-footnote-number)
  1635. (defvar org-lparse-footnote-definitions)
  1636. (defvar org-lparse-output-buffer nil
  1637. "Buffer to which `org-do-lparse' writes to.
  1638. This buffer contains the contents of the to-be-created exported
  1639. document.")
  1640. (defcustom org-lparse-debug nil
  1641. "Enable or Disable logging of `org-lparse' callbacks.
  1642. The parameters passed to the backend-registered ENTITY-CONTROL
  1643. and ENTITY-FORMAT callbacks are logged as comment strings in the
  1644. exported buffer. (org-lparse-format 'COMMENT fmt args) is used
  1645. for logging. Customize this variable only if you are an expert
  1646. user. Valid values of this variable are:
  1647. nil : Disable logging
  1648. control : Log all invocations of `org-lparse-begin' and
  1649. `org-lparse-end' callbacks.
  1650. format : Log invocations of `org-lparse-format' callbacks.
  1651. t : Log all invocations of `org-lparse-begin', `org-lparse-end'
  1652. and `org-lparse-format' callbacks,"
  1653. :group 'org-lparse
  1654. :type '(choice
  1655. (const :tag "Disable" nil)
  1656. (const :tag "Format callbacks" format)
  1657. (const :tag "Control callbacks" control)
  1658. (const :tag "Format and Control callbacks" t)))
  1659. (defun org-lparse-begin (entity &rest args)
  1660. "Begin ENTITY in current buffer. ARGS is entity specific.
  1661. ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM etc.
  1662. Use (org-lparse-begin 'LIST \"o\") to begin a list in current
  1663. buffer.
  1664. See `org-xhtml-entity-control-callbacks-alist' for more
  1665. information."
  1666. (when (and (member org-lparse-debug '(t control))
  1667. (not (eq entity 'DOCUMENT-CONTENT)))
  1668. (insert (org-lparse-format 'COMMENT "%s BEGIN %S" entity args)))
  1669. (let ((f (cadr (assoc entity org-lparse-entity-control-callbacks-alist))))
  1670. (unless f (error "Unknown entity: %s" entity))
  1671. (apply f args)))
  1672. (defun org-lparse-end (entity &rest args)
  1673. "Close ENTITY in current buffer. ARGS is entity specific.
  1674. ENTITY can be one of PARAGRAPH, LIST, LIST-ITEM
  1675. etc.
  1676. Use (org-lparse-end 'LIST \"o\") to close a list in current
  1677. buffer.
  1678. See `org-xhtml-entity-control-callbacks-alist' for more
  1679. information."
  1680. (when (and (member org-lparse-debug '(t control))
  1681. (not (eq entity 'DOCUMENT-CONTENT)))
  1682. (insert (org-lparse-format 'COMMENT "%s END %S" entity args)))
  1683. (let ((f (caddr (assoc entity org-lparse-entity-control-callbacks-alist))))
  1684. (unless f (error "Unknown entity: %s" entity))
  1685. (apply f args)))
  1686. (defun org-lparse-begin-paragraph (&optional style)
  1687. "Insert <p>, but first close previous paragraph if any."
  1688. (org-lparse-end-paragraph)
  1689. (org-lparse-begin 'PARAGRAPH style)
  1690. (setq org-lparse-par-open t))
  1691. (defun org-lparse-end-paragraph ()
  1692. "Close paragraph if there is one open."
  1693. (when org-lparse-par-open
  1694. (org-lparse-end 'PARAGRAPH)
  1695. (setq org-lparse-par-open nil)))
  1696. (defun org-lparse-end-list-item-1 (&optional type)
  1697. "Close <li> if necessary."
  1698. (org-lparse-end-paragraph)
  1699. (org-lparse-end-list-item (or type "u")))
  1700. (defcustom org-lparse-list-table-enable nil
  1701. "Specify whether a list be exported as a table.
  1702. When this option is enabled, lists that are enclosed in
  1703. \"#+begin_list-table...#+end_list-table\" are exported as
  1704. tables. Otherwise they are exported normally."
  1705. :type 'boolean
  1706. :group 'org-lparse)
  1707. (defun org-lparse-preprocess-after-blockquote-hook ()
  1708. "Treat #+begin_list-table...#+end_list-table blocks specially.
  1709. When `org-lparse-list-table-enable' is non-nil, enclose these
  1710. blocks within ORG-LIST-TABLE-START...ORG-LIST-TABLE-END."
  1711. (when org-lparse-list-table-enable
  1712. (goto-char (point-min))
  1713. (while (re-search-forward "^[ \t]*#\\+\\(begin\\|end\\)_\\(.*\\)$" nil t)
  1714. (when (string= (downcase (match-string 2)) "list-table")
  1715. (replace-match (if (equal (downcase (match-string 1)) "begin")
  1716. "ORG-LIST-TABLE-START"
  1717. "ORG-LIST-TABLE-END") t t)))))
  1718. (add-hook 'org-export-preprocess-after-blockquote-hook
  1719. 'org-lparse-preprocess-after-blockquote-hook)
  1720. (defvar org-lparse-list-table-p nil
  1721. "Non-nil if `org-do-lparse' is within a list-table.
  1722. See `org-lparse-list-table-enable'.")
  1723. (defvar org-lparse-dyn-current-environment nil)
  1724. (defun org-lparse-begin-environment (style)
  1725. (case style
  1726. (list-table
  1727. (setq org-lparse-list-table-p org-lparse-list-table-enable))
  1728. (t
  1729. (setq org-lparse-dyn-current-environment style)
  1730. (org-lparse-begin 'ENVIRONMENT style))))
  1731. (defun org-lparse-end-environment (style)
  1732. (case style
  1733. (list-table
  1734. (setq org-lparse-list-table-p nil))
  1735. (t
  1736. (org-lparse-end 'ENVIRONMENT style)
  1737. (setq org-lparse-dyn-current-environment nil))))
  1738. (defun org-lparse-current-environment-p (style)
  1739. (eq org-lparse-dyn-current-environment style))
  1740. (defun org-lparse-begin-footnote-definition (n)
  1741. (org-lparse-begin-collect)
  1742. (setq org-lparse-insert-tag-with-newlines nil)
  1743. (org-lparse-begin 'FOOTNOTE-DEFINITION n))
  1744. (defun org-lparse-end-footnote-definition (n)
  1745. (org-lparse-end 'FOOTNOTE-DEFINITION n)
  1746. (setq org-lparse-insert-tag-with-newlines 'both)
  1747. (let ((footnote-def (org-lparse-end-collect)))
  1748. (push (cons n footnote-def) org-lparse-footnote-definitions)))
  1749. (defvar org-lparse-collect-buffer nil
  1750. "An auxiliary buffer named \"*Org Lparse Collect*\".
  1751. `org-do-lparse' uses this as output buffer while collecting
  1752. footnote definitions and table-cell contents of list-tables. See
  1753. `org-lparse-begin-collect' and `org-lparse-end-collect'.")
  1754. (defvar org-lparse-collect-count nil
  1755. "Count number of calls to `org-lparse-begin-collect'.
  1756. Use this counter to catch chained collections if they ever
  1757. happen.")
  1758. (defun org-lparse-begin-collect ()
  1759. "Temporarily switch to `org-lparse-collect-buffer'.
  1760. Also erase it's contents."
  1761. (unless (zerop org-lparse-collect-count)
  1762. (error "FIXME (org-lparse.el): Encountered chained collections"))
  1763. (incf org-lparse-collect-count)
  1764. (unless org-lparse-collect-buffer
  1765. (setq org-lparse-collect-buffer
  1766. (get-buffer-create "*Org Lparse Collect*")))
  1767. (set-buffer org-lparse-collect-buffer)
  1768. (erase-buffer))
  1769. (defun org-lparse-end-collect ()
  1770. "Switch to `org-lparse-output-buffer'.
  1771. Return contents of `org-lparse-collect-buffer' as a `string'."
  1772. (assert (> org-lparse-collect-count 0))
  1773. (decf org-lparse-collect-count)
  1774. (prog1 (buffer-string)
  1775. (erase-buffer)
  1776. (set-buffer org-lparse-output-buffer)))
  1777. (defun org-lparse-format (entity &rest args)
  1778. "Format ENTITY in backend-specific way and return it.
  1779. ARGS is specific to entity being formatted.
  1780. Use (org-lparse-format 'HEADING \"text\" 1) to format text as
  1781. level 1 heading.
  1782. See `org-xhtml-entity-format-callbacks-alist' for more information."
  1783. (when (and (member org-lparse-debug '(t format))
  1784. (not (equal entity 'COMMENT)))
  1785. (insert (org-lparse-format 'COMMENT "%s: %S" entity args)))
  1786. (cond
  1787. ((consp entity)
  1788. (let ((text (pop args)))
  1789. (apply 'org-lparse-format 'TAGS entity text args)))
  1790. (t
  1791. (let ((f (cdr (assoc entity org-lparse-entity-format-callbacks-alist))))
  1792. (unless f (error "Unknown entity: %s" entity))
  1793. (apply f args)))))
  1794. (defun org-lparse-insert (entity &rest args)
  1795. (insert (apply 'org-lparse-format entity args)))
  1796. (defun org-lparse-prepare-toc (lines level-offset opt-plist umax-toc)
  1797. (let* ((quote-re0 (concat "^[ \t]*" org-quote-string "\\>"))
  1798. (org-min-level (org-get-min-level lines level-offset))
  1799. (org-last-level org-min-level)
  1800. level)
  1801. (with-temp-buffer
  1802. (org-lparse-bind-local-variables opt-plist)
  1803. (erase-buffer)
  1804. (org-lparse-begin 'TOC (nth 3 (plist-get opt-plist :lang-words)))
  1805. (setq
  1806. lines
  1807. (mapcar
  1808. #'(lambda (line)
  1809. (when (and (string-match org-todo-line-regexp line)
  1810. (not (get-text-property 0 'org-protected line))
  1811. (<= (setq level (org-tr-level
  1812. (- (match-end 1) (match-beginning 1)
  1813. level-offset)))
  1814. umax-toc))
  1815. (let ((txt (save-match-data
  1816. (org-xml-encode-org-text-skip-links
  1817. (org-export-cleanup-toc-line
  1818. (match-string 3 line)))))
  1819. (todo (and
  1820. org-export-mark-todo-in-toc
  1821. (or (and (match-beginning 2)
  1822. (not (member (match-string 2 line)
  1823. org-done-keywords)))
  1824. (and (= level umax-toc)
  1825. (org-search-todo-below
  1826. line lines level)))))
  1827. tags)
  1828. ;; Check for targets
  1829. (while (string-match org-any-target-regexp line)
  1830. (setq line
  1831. (replace-match
  1832. (let ((org-lparse-encode-pending t))
  1833. (org-lparse-format 'FONTIFY
  1834. (match-string 1 line) "target"))
  1835. t t line)))
  1836. (when (string-match
  1837. (org-re "[ \t]+:\\([[:alnum:]_@:]+\\):[ \t]*$") txt)
  1838. (setq tags (match-string 1 txt)
  1839. txt (replace-match "" t nil txt)))
  1840. (when (string-match quote-re0 txt)
  1841. (setq txt (replace-match "" t t txt)))
  1842. (while (string-match "&lt;\\(&lt;\\)+\\|&gt;\\(&gt;\\)+" txt)
  1843. (setq txt (replace-match "" t t txt)))
  1844. (org-lparse-format
  1845. 'TOC-ITEM
  1846. (let* ((snumber (org-section-number level))
  1847. (href (replace-regexp-in-string
  1848. "\\." "-" (format "sec-%s" snumber)))
  1849. (href
  1850. (or
  1851. (cdr (assoc
  1852. href org-export-preferred-target-alist))
  1853. href))
  1854. (href (org-solidify-link-text href)))
  1855. (org-lparse-format 'TOC-ENTRY snumber todo txt tags href))
  1856. level org-last-level)
  1857. (setq org-last-level level)))
  1858. line)
  1859. lines))
  1860. (org-lparse-end 'TOC)
  1861. (setq org-lparse-toc (buffer-string))))
  1862. lines)
  1863. (defun org-lparse-format-table-row (fields &optional text-for-empty-fields)
  1864. (unless 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