org-lparse.el 87 KB

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