org-e-odt.el 144 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033
  1. ;;; org-e-odt.el --- OpenDocument Text exporter for Org-mode
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Jambunathan K <kjambunathan at gmail dot com>
  4. ;; Keywords: outlines, hypermedia, calendar, wp
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is not part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (eval-when-compile
  20. (require 'cl)
  21. (require 'table))
  22. (require 'format-spec)
  23. (require 'org-export)
  24. ;;; Define Back-End
  25. (org-export-define-backend e-odt
  26. ((bold . org-e-odt-bold)
  27. (center-block . org-e-odt-center-block)
  28. (clock . org-e-odt-clock)
  29. (code . org-e-odt-code)
  30. (drawer . org-e-odt-drawer)
  31. (dynamic-block . org-e-odt-dynamic-block)
  32. (entity . org-e-odt-entity)
  33. (example-block . org-e-odt-example-block)
  34. (export-block . org-e-odt-export-block)
  35. (export-snippet . org-e-odt-export-snippet)
  36. (fixed-width . org-e-odt-fixed-width)
  37. (footnote-definition . org-e-odt-footnote-definition)
  38. (footnote-reference . org-e-odt-footnote-reference)
  39. (headline . org-e-odt-headline)
  40. (horizontal-rule . org-e-odt-horizontal-rule)
  41. (inline-src-block . org-e-odt-inline-src-block)
  42. (inlinetask . org-e-odt-inlinetask)
  43. (italic . org-e-odt-italic)
  44. (item . org-e-odt-item)
  45. (keyword . org-e-odt-keyword)
  46. (latex-environment . org-e-odt-latex-environment)
  47. (latex-fragment . org-e-odt-latex-fragment)
  48. (line-break . org-e-odt-line-break)
  49. (link . org-e-odt-link)
  50. (paragraph . org-e-odt-paragraph)
  51. (plain-list . org-e-odt-plain-list)
  52. (plain-text . org-e-odt-plain-text)
  53. (planning . org-e-odt-planning)
  54. (property-drawer . org-e-odt-property-drawer)
  55. (quote-block . org-e-odt-quote-block)
  56. (quote-section . org-e-odt-quote-section)
  57. (radio-target . org-e-odt-radio-target)
  58. (section . org-e-odt-section)
  59. (special-block . org-e-odt-special-block)
  60. (src-block . org-e-odt-src-block)
  61. (statistics-cookie . org-e-odt-statistics-cookie)
  62. (strike-through . org-e-odt-strike-through)
  63. (subscript . org-e-odt-subscript)
  64. (superscript . org-e-odt-superscript)
  65. (table . org-e-odt-table)
  66. (table-cell . org-e-odt-table-cell)
  67. (table-row . org-e-odt-table-row)
  68. (target . org-e-odt-target)
  69. (template . org-e-odt-template)
  70. (timestamp . org-e-odt-timestamp)
  71. (underline . org-e-odt-underline)
  72. (verbatim . org-e-odt-verbatim)
  73. (verse-block . org-e-odt-verse-block))
  74. :export-block "ODT"
  75. :filters-alist ((:filter-parse-tree
  76. . (org-e-odt--translate-description-lists
  77. org-e-odt--translate-list-tables)))
  78. :menu-entry
  79. (?o "Export to ODT"
  80. ((?o "As ODT file" org-e-odt-export-to-odt)
  81. (?O "As ODT file and open"
  82. (lambda (s v b)
  83. (org-open-file (org-e-odt-export-to-odt s v b) 'system)))))
  84. :options-alist
  85. ((:odt-styles-file "ODT_STYLES_FILE" nil nil t)
  86. (:LaTeX-fragments nil "LaTeX" org-export-with-LaTeX-fragments)))
  87. ;;; Dependencies
  88. ;;; Hooks
  89. ;;; Function Declarations
  90. (declare-function org-id-find-id-file "org-id" (id))
  91. (declare-function hfy-face-to-style "htmlfontify" (fn))
  92. (declare-function hfy-face-or-def-to-name "htmlfontify" (fn))
  93. (declare-function archive-zip-extract "arc-mode.el" (archive name))
  94. (declare-function org-create-math-formula "org" (latex-frag &optional
  95. mathml-file))
  96. (declare-function browse-url-file-url "browse-url" (file))
  97. ;;; Internal Variables
  98. (defconst org-e-odt-lib-dir
  99. (file-name-directory load-file-name)
  100. "Location of ODT exporter.
  101. Use this to infer values of `org-e-odt-styles-dir' and
  102. `org-e-odt-schema-dir'.")
  103. (defvar org-e-odt-data-dir
  104. (expand-file-name "../../etc/" org-e-odt-lib-dir)
  105. "Data directory for ODT exporter.
  106. Use this to infer values of `org-e-odt-styles-dir' and
  107. `org-e-odt-schema-dir'.")
  108. (defconst org-e-odt-special-string-regexps
  109. '(("\\\\-" . "&#x00ad;\\1") ; shy
  110. ("---\\([^-]\\)" . "&#x2014;\\1") ; mdash
  111. ("--\\([^-]\\)" . "&#x2013;\\1") ; ndash
  112. ("\\.\\.\\." . "&#x2026;")) ; hellip
  113. "Regular expressions for special string conversion.")
  114. (defconst org-e-odt-schema-dir-list
  115. (list
  116. (and org-e-odt-data-dir
  117. (expand-file-name "./schema/" org-e-odt-data-dir)) ; bail out
  118. (eval-when-compile
  119. (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
  120. (expand-file-name "./schema/" org-e-odt-data-dir))))
  121. "List of directories to search for OpenDocument schema files.
  122. Use this list to set the default value of
  123. `org-e-odt-schema-dir'. The entries in this list are
  124. populated heuristically based on the values of `org-e-odt-lib-dir'
  125. and `org-e-odt-data-dir'.")
  126. (defconst org-e-odt-styles-dir-list
  127. (list
  128. (and org-e-odt-data-dir
  129. (expand-file-name "./styles/" org-e-odt-data-dir)) ; bail out
  130. (eval-when-compile
  131. (and (boundp 'org-e-odt-data-dir) org-e-odt-data-dir ; see make install
  132. (expand-file-name "./styles/" org-e-odt-data-dir)))
  133. (expand-file-name "../../etc/styles/" org-e-odt-lib-dir) ; git
  134. (expand-file-name "./etc/styles/" org-e-odt-lib-dir) ; elpa
  135. (expand-file-name "./org/" data-directory) ; system
  136. )
  137. "List of directories to search for OpenDocument styles files.
  138. See `org-e-odt-styles-dir'. The entries in this list are populated
  139. heuristically based on the values of `org-e-odt-lib-dir' and
  140. `org-e-odt-data-dir'.")
  141. (defconst org-e-odt-styles-dir
  142. (let* ((styles-dir
  143. (catch 'styles-dir
  144. (message "Debug (org-e-odt): Searching for OpenDocument styles files...")
  145. (mapc (lambda (styles-dir)
  146. (when styles-dir
  147. (message "Debug (org-e-odt): Trying %s..." styles-dir)
  148. (when (and (file-readable-p
  149. (expand-file-name
  150. "OrgOdtContentTemplate.xml" styles-dir))
  151. (file-readable-p
  152. (expand-file-name
  153. "OrgOdtStyles.xml" styles-dir)))
  154. (message "Debug (org-e-odt): Using styles under %s"
  155. styles-dir)
  156. (throw 'styles-dir styles-dir))))
  157. org-e-odt-styles-dir-list)
  158. nil)))
  159. (unless styles-dir
  160. (error "Error (org-e-odt): Cannot find factory styles files. Aborting."))
  161. styles-dir)
  162. "Directory that holds auxiliary XML files used by the ODT exporter.
  163. This directory contains the following XML files -
  164. \"OrgOdtStyles.xml\" and \"OrgOdtContentTemplate.xml\". These
  165. XML files are used as the default values of
  166. `org-e-odt-styles-file' and
  167. `org-e-odt-content-template-file'.
  168. The default value of this variable varies depending on the
  169. version of org in use and is initialized from
  170. `org-e-odt-styles-dir-list'. Note that the user could be using org
  171. from one of: org's own private git repository, GNU ELPA tar or
  172. standard Emacs.")
  173. (defconst org-e-odt-bookmark-prefix "OrgXref.")
  174. (defconst org-e-odt-manifest-file-entry-tag
  175. "\n<manifest:file-entry manifest:media-type=\"%s\" manifest:full-path=\"%s\"%s/>")
  176. (defconst org-e-odt-file-extensions
  177. '(("odt" . "OpenDocument Text")
  178. ("ott" . "OpenDocument Text Template")
  179. ("odm" . "OpenDocument Master Document")
  180. ("ods" . "OpenDocument Spreadsheet")
  181. ("ots" . "OpenDocument Spreadsheet Template")
  182. ("odg" . "OpenDocument Drawing (Graphics)")
  183. ("otg" . "OpenDocument Drawing Template")
  184. ("odp" . "OpenDocument Presentation")
  185. ("otp" . "OpenDocument Presentation Template")
  186. ("odi" . "OpenDocument Image")
  187. ("odf" . "OpenDocument Formula")
  188. ("odc" . "OpenDocument Chart")))
  189. (defvar org-e-odt-table-style-format
  190. "
  191. <style:style style:name=\"%s\" style:family=\"table\">
  192. <style:table-properties style:rel-width=\"%d%%\" fo:margin-top=\"0cm\" fo:margin-bottom=\"0.20cm\" table:align=\"center\"/>
  193. </style:style>
  194. "
  195. "Template for auto-generated Table styles.")
  196. (defvar org-e-odt-automatic-styles '()
  197. "Registry of automatic styles for various OBJECT-TYPEs.
  198. The variable has the following form:
  199. \(\(OBJECT-TYPE-A
  200. \(\(OBJECT-NAME-A.1 OBJECT-PROPS-A.1\)
  201. \(OBJECT-NAME-A.2 OBJECT-PROPS-A.2\) ...\)\)
  202. \(OBJECT-TYPE-B
  203. \(\(OBJECT-NAME-B.1 OBJECT-PROPS-B.1\)
  204. \(OBJECT-NAME-B.2 OBJECT-PROPS-B.2\) ...\)\)
  205. ...\).
  206. OBJECT-TYPEs could be \"Section\", \"Table\", \"Figure\" etc.
  207. OBJECT-PROPS is (typically) a plist created by passing
  208. \"#+ATTR_ODT: \" option to `org-e-odt-parse-block-attributes'.
  209. Use `org-e-odt-add-automatic-style' to add update this variable.'")
  210. (defvar org-e-odt-object-counters nil
  211. "Running counters for various OBJECT-TYPEs.
  212. Use this to generate automatic names and style-names. See
  213. `org-e-odt-add-automatic-style'.")
  214. (defvar org-e-odt-src-block-paragraph-format
  215. "<style:style style:name=\"OrgSrcBlock\" style:family=\"paragraph\" style:parent-style-name=\"Preformatted_20_Text\">
  216. <style:paragraph-properties fo:background-color=\"%s\" fo:padding=\"0.049cm\" fo:border=\"0.51pt solid #000000\" style:shadow=\"none\">
  217. <style:background-image/>
  218. </style:paragraph-properties>
  219. <style:text-properties fo:color=\"%s\"/>
  220. </style:style>"
  221. "Custom paragraph style for colorized source and example blocks.
  222. This style is much the same as that of \"OrgFixedWidthBlock\"
  223. except that the foreground and background colors are set
  224. according to the default face identified by the `htmlfontify'.")
  225. (defvar hfy-optimisations)
  226. (defvar org-e-odt-embedded-formulas-count 0)
  227. (defvar org-e-odt-entity-frame-styles
  228. '(("As-CharImage" "__Figure__" ("OrgInlineImage" nil "as-char"))
  229. ("ParagraphImage" "__Figure__" ("OrgDisplayImage" nil "paragraph"))
  230. ("PageImage" "__Figure__" ("OrgPageImage" nil "page"))
  231. ("CaptionedAs-CharImage" "__Figure__"
  232. ("OrgCaptionedImage"
  233. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  234. ("OrgInlineImage" nil "as-char"))
  235. ("CaptionedParagraphImage" "__Figure__"
  236. ("OrgCaptionedImage"
  237. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  238. ("OrgImageCaptionFrame" nil "paragraph"))
  239. ("CaptionedPageImage" "__Figure__"
  240. ("OrgCaptionedImage"
  241. " style:rel-width=\"100%\" style:rel-height=\"scale\"" "paragraph")
  242. ("OrgPageImageCaptionFrame" nil "page"))
  243. ("InlineFormula" "__MathFormula__" ("OrgInlineFormula" nil "as-char"))
  244. ("DisplayFormula" "__MathFormula__" ("OrgDisplayFormula" nil "as-char"))
  245. ("CaptionedDisplayFormula" "__MathFormula__"
  246. ("OrgCaptionedFormula" nil "paragraph")
  247. ("OrgFormulaCaptionFrame" nil "as-char"))))
  248. (defvar org-e-odt-embedded-images-count 0)
  249. (defvar org-e-odt-image-size-probe-method
  250. (append (and (executable-find "identify") '(imagemagick)) ; See Bug#10675
  251. '(emacs fixed))
  252. "Ordered list of methods for determining image sizes.")
  253. (defvar org-e-odt-default-image-sizes-alist
  254. '(("as-char" . (5 . 0.4))
  255. ("paragraph" . (5 . 5)))
  256. "Hardcoded image dimensions one for each of the anchor
  257. methods.")
  258. ;; A4 page size is 21.0 by 29.7 cms
  259. ;; The default page settings has 2cm margin on each of the sides. So
  260. ;; the effective text area is 17.0 by 25.7 cm
  261. (defvar org-e-odt-max-image-size '(17.0 . 20.0)
  262. "Limiting dimensions for an embedded image.")
  263. (defvar org-e-odt-label-styles
  264. '(("math-formula" "%c" "text" "(%n)")
  265. ("math-label" "(%n)" "text" "(%n)")
  266. ("category-and-value" "%e %n: %c" "category-and-value" "%e %n")
  267. ("value" "%e %n: %c" "value" "%n"))
  268. "Specify how labels are applied and referenced.
  269. This is an alist where each element is of the
  270. form (LABEL-STYLE-NAME LABEL-ATTACH-FMT LABEL-REF-MODE
  271. LABEL-REF-FMT).
  272. LABEL-ATTACH-FMT controls how labels and captions are attached to
  273. an entity. It may contain following specifiers - %e, %n and %c.
  274. %e is replaced with the CATEGORY-NAME. %n is replaced with
  275. \"<text:sequence ...> SEQNO </text:sequence>\". %c is replaced
  276. with CAPTION. See `org-e-odt-format-label-definition'.
  277. LABEL-REF-MODE and LABEL-REF-FMT controls how label references
  278. are generated. The following XML is generated for a label
  279. reference - \"<text:sequence-ref
  280. text:reference-format=\"LABEL-REF-MODE\" ...> LABEL-REF-FMT
  281. </text:sequence-ref>\". LABEL-REF-FMT may contain following
  282. specifiers - %e and %n. %e is replaced with the CATEGORY-NAME.
  283. %n is replaced with SEQNO. See
  284. `org-e-odt-format-label-reference'.")
  285. (defvar org-e-odt-category-map-alist
  286. '(("__Table__" "Table" "value" "Table")
  287. ("__Figure__" "Illustration" "value" "Figure")
  288. ("__MathFormula__" "Text" "math-formula" "Equation")
  289. ("__DvipngImage__" "Equation" "value" "Equation")
  290. ("__Listing__" "Listing" "value" "Listing")
  291. ;; ("__Table__" "Table" "category-and-value")
  292. ;; ("__Figure__" "Figure" "category-and-value")
  293. ;; ("__DvipngImage__" "Equation" "category-and-value")
  294. )
  295. "Map a CATEGORY-HANDLE to OD-VARIABLE and LABEL-STYLE.
  296. This is a list where each entry is of the form \\(CATEGORY-HANDLE
  297. OD-VARIABLE LABEL-STYLE CATEGORY-NAME\\). CATEGORY_HANDLE
  298. identifies the captionable entity in question. OD-VARIABLE is
  299. the OpenDocument sequence counter associated with the entity.
  300. These counters are declared within
  301. \"<text:sequence-decls>...</text:sequence-decls>\" block of
  302. `org-e-odt-content-template-file'. LABEL-STYLE is a key into
  303. `org-e-odt-label-styles' and specifies how a given entity should
  304. be captioned and referenced. CATEGORY-NAME is used for
  305. qualifying captions on export. You can modify the CATEGORY-NAME
  306. used in the exported document by modifying
  307. `org-export-dictionary'. For example, an embedded image in an
  308. English document is captioned as \"Figure 1: Orgmode Logo\", by
  309. default. If you want the image to be captioned as \"Illustration
  310. 1: Orgmode Logo\" instead, install an entry in
  311. `org-export-dictionary' which translates \"Figure\" to
  312. \"Illustration\" when the language is \"en\" and encoding is
  313. `:utf-8'.")
  314. (defvar org-e-odt-manifest-file-entries nil)
  315. (defvar hfy-user-sheet-assoc)
  316. (defvar org-e-odt-zip-dir nil
  317. "Temporary work directory for OpenDocument exporter.")
  318. ;;; User Configuration Variables
  319. (defgroup org-export-e-odt nil
  320. "Options for exporting Org mode files to ODT."
  321. :tag "Org Export ODT"
  322. :group 'org-export)
  323. ;;;; Debugging
  324. (defcustom org-e-odt-prettify-xml nil
  325. "Specify whether or not the xml output should be prettified.
  326. When this option is turned on, `indent-region' is run on all
  327. component xml buffers before they are saved. Turn this off for
  328. regular use. Turn this on if you need to examine the xml
  329. visually."
  330. :group 'org-export-e-odt
  331. :version "24.1"
  332. :type 'boolean)
  333. ;;;; Document schema
  334. (defcustom org-e-odt-schema-dir
  335. (let* ((schema-dir
  336. (catch 'schema-dir
  337. (message "Debug (org-e-odt): Searching for OpenDocument schema files...")
  338. (mapc
  339. (lambda (schema-dir)
  340. (when schema-dir
  341. (message "Debug (org-e-odt): Trying %s..." schema-dir)
  342. (when (and (file-readable-p
  343. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc"
  344. schema-dir))
  345. (file-readable-p
  346. (expand-file-name "od-schema-v1.2-cs01.rnc"
  347. schema-dir))
  348. (file-readable-p
  349. (expand-file-name "schemas.xml" schema-dir)))
  350. (message "Debug (org-e-odt): Using schema files under %s"
  351. schema-dir)
  352. (throw 'schema-dir schema-dir))))
  353. org-e-odt-schema-dir-list)
  354. (message "Debug (org-e-odt): No OpenDocument schema files installed")
  355. nil)))
  356. schema-dir)
  357. "Directory that contains OpenDocument schema files.
  358. This directory contains:
  359. 1. rnc files for OpenDocument schema
  360. 2. a \"schemas.xml\" file that specifies locating rules needed
  361. for auto validation of OpenDocument XML files.
  362. Use the customize interface to set this variable. This ensures
  363. that `rng-schema-locating-files' is updated and auto-validation
  364. of OpenDocument XML takes place based on the value
  365. `rng-nxml-auto-validate-flag'.
  366. The default value of this variable varies depending on the
  367. version of org in use and is initialized from
  368. `org-e-odt-schema-dir-list'. The OASIS schema files are available
  369. only in the org's private git repository. It is *not* bundled
  370. with GNU ELPA tar or standard Emacs distribution."
  371. :type '(choice
  372. (const :tag "Not set" nil)
  373. (directory :tag "Schema directory"))
  374. :group 'org-export-e-odt
  375. :version "24.1"
  376. :set
  377. (lambda (var value)
  378. "Set `org-e-odt-schema-dir'.
  379. Also add it to `rng-schema-locating-files'."
  380. (let ((schema-dir value))
  381. (set var
  382. (if (and
  383. (file-readable-p
  384. (expand-file-name "od-manifest-schema-v1.2-cs01.rnc" schema-dir))
  385. (file-readable-p
  386. (expand-file-name "od-schema-v1.2-cs01.rnc" schema-dir))
  387. (file-readable-p
  388. (expand-file-name "schemas.xml" schema-dir)))
  389. schema-dir
  390. (when value
  391. (message "Error (org-e-odt): %s has no OpenDocument schema files"
  392. value))
  393. nil)))
  394. (when org-e-odt-schema-dir
  395. (eval-after-load 'rng-loc
  396. '(add-to-list 'rng-schema-locating-files
  397. (expand-file-name "schemas.xml"
  398. org-e-odt-schema-dir))))))
  399. ;;;; Document styles
  400. (defcustom org-e-odt-content-template-file nil
  401. "Template file for \"content.xml\".
  402. The exporter embeds the exported content just before
  403. \"</office:text>\" element.
  404. If unspecified, the file named \"OrgOdtContentTemplate.xml\"
  405. under `org-e-odt-styles-dir' is used."
  406. :type 'file
  407. :group 'org-export-e-odt
  408. :version "24.1")
  409. (defcustom org-e-odt-styles-file nil
  410. "Default styles file for use with ODT export.
  411. Valid values are one of:
  412. 1. nil
  413. 2. path to a styles.xml file
  414. 3. path to a *.odt or a *.ott file
  415. 4. list of the form (ODT-OR-OTT-FILE (FILE-MEMBER-1 FILE-MEMBER-2
  416. ...))
  417. In case of option 1, an in-built styles.xml is used. See
  418. `org-e-odt-styles-dir' for more information.
  419. In case of option 3, the specified file is unzipped and the
  420. styles.xml embedded therein is used.
  421. In case of option 4, the specified ODT-OR-OTT-FILE is unzipped
  422. and FILE-MEMBER-1, FILE-MEMBER-2 etc are copied in to the
  423. generated odt file. Use relative path for specifying the
  424. FILE-MEMBERS. styles.xml must be specified as one of the
  425. FILE-MEMBERS.
  426. Use options 1, 2 or 3 only if styles.xml alone suffices for
  427. achieving the desired formatting. Use option 4, if the styles.xml
  428. references additional files like header and footer images for
  429. achieving the desired formatting.
  430. Use \"#+ODT_STYLES_FILE: ...\" directive to set this variable on
  431. a per-file basis. For example,
  432. #+ODT_STYLES_FILE: \"/path/to/styles.xml\" or
  433. #+ODT_STYLES_FILE: (\"/path/to/file.ott\" (\"styles.xml\" \"image/hdr.png\"))."
  434. :group 'org-export-e-odt
  435. :version "24.1"
  436. :type
  437. '(choice
  438. (const :tag "Factory settings" nil)
  439. (file :must-match t :tag "styles.xml")
  440. (file :must-match t :tag "ODT or OTT file")
  441. (list :tag "ODT or OTT file + Members"
  442. (file :must-match t :tag "ODF Text or Text Template file")
  443. (cons :tag "Members"
  444. (file :tag " Member" "styles.xml")
  445. (repeat (file :tag "Member"))))))
  446. (defcustom org-e-odt-display-outline-level 2
  447. "Outline levels considered for enumerating captioned entities."
  448. :group 'org-export-e-odt
  449. :version "24.2"
  450. :type 'integer)
  451. ;;;; Document conversion
  452. (defcustom org-e-odt-convert-processes
  453. '(("LibreOffice"
  454. "soffice --headless --convert-to %f%x --outdir %d %i")
  455. ("unoconv"
  456. "unoconv -f %f -o %d %i"))
  457. "Specify a list of document converters and their usage.
  458. The converters in this list are offered as choices while
  459. customizing `org-e-odt-convert-process'.
  460. This variable is a list where each element is of the
  461. form (CONVERTER-NAME CONVERTER-CMD). CONVERTER-NAME is the name
  462. of the converter. CONVERTER-CMD is the shell command for the
  463. converter and can contain format specifiers. These format
  464. specifiers are interpreted as below:
  465. %i input file name in full
  466. %I input file name as a URL
  467. %f format of the output file
  468. %o output file name in full
  469. %O output file name as a URL
  470. %d output dir in full
  471. %D output dir as a URL.
  472. %x extra options as set in `org-e-odt-convert-capabilities'."
  473. :group 'org-export-e-odt
  474. :version "24.1"
  475. :type
  476. '(choice
  477. (const :tag "None" nil)
  478. (alist :tag "Converters"
  479. :key-type (string :tag "Converter Name")
  480. :value-type (group (string :tag "Command line")))))
  481. (defcustom org-e-odt-convert-process "LibreOffice"
  482. "Use this converter to convert from \"odt\" format to other formats.
  483. During customization, the list of converter names are populated
  484. from `org-e-odt-convert-processes'."
  485. :group 'org-export-e-odt
  486. :version "24.1"
  487. :type '(choice :convert-widget
  488. (lambda (w)
  489. (apply 'widget-convert (widget-type w)
  490. (eval (car (widget-get w :args)))))
  491. `((const :tag "None" nil)
  492. ,@(mapcar (lambda (c)
  493. `(const :tag ,(car c) ,(car c)))
  494. org-e-odt-convert-processes))))
  495. (defcustom org-e-odt-convert-capabilities
  496. '(("Text"
  497. ("odt" "ott" "doc" "rtf" "docx")
  498. (("pdf" "pdf") ("odt" "odt") ("rtf" "rtf") ("ott" "ott")
  499. ("doc" "doc" ":\"MS Word 97\"") ("docx" "docx") ("html" "html")))
  500. ("Web"
  501. ("html")
  502. (("pdf" "pdf") ("odt" "odt") ("html" "html")))
  503. ("Spreadsheet"
  504. ("ods" "ots" "xls" "csv" "xlsx")
  505. (("pdf" "pdf") ("ots" "ots") ("html" "html") ("csv" "csv") ("ods" "ods")
  506. ("xls" "xls") ("xlsx" "xlsx")))
  507. ("Presentation"
  508. ("odp" "otp" "ppt" "pptx")
  509. (("pdf" "pdf") ("swf" "swf") ("odp" "odp") ("otp" "otp") ("ppt" "ppt")
  510. ("pptx" "pptx") ("odg" "odg"))))
  511. "Specify input and output formats of `org-e-odt-convert-process'.
  512. More correctly, specify the set of input and output formats that
  513. the user is actually interested in.
  514. This variable is an alist where each element is of the
  515. form (DOCUMENT-CLASS INPUT-FMT-LIST OUTPUT-FMT-ALIST).
  516. INPUT-FMT-LIST is a list of INPUT-FMTs. OUTPUT-FMT-ALIST is an
  517. alist where each element is of the form (OUTPUT-FMT
  518. OUTPUT-FILE-EXTENSION EXTRA-OPTIONS).
  519. The variable is interpreted as follows:
  520. `org-e-odt-convert-process' can take any document that is in
  521. INPUT-FMT-LIST and produce any document that is in the
  522. OUTPUT-FMT-LIST. A document converted to OUTPUT-FMT will have
  523. OUTPUT-FILE-EXTENSION as the file name extension. OUTPUT-FMT
  524. serves dual purposes:
  525. - It is used for populating completion candidates during
  526. `org-e-odt-convert' commands.
  527. - It is used as the value of \"%f\" specifier in
  528. `org-e-odt-convert-process'.
  529. EXTRA-OPTIONS is used as the value of \"%x\" specifier in
  530. `org-e-odt-convert-process'.
  531. DOCUMENT-CLASS is used to group a set of file formats in
  532. INPUT-FMT-LIST in to a single class.
  533. Note that this variable inherently captures how LibreOffice based
  534. converters work. LibreOffice maps documents of various formats
  535. to classes like Text, Web, Spreadsheet, Presentation etc and
  536. allow document of a given class (irrespective of it's source
  537. format) to be converted to any of the export formats associated
  538. with that class.
  539. See default setting of this variable for an typical
  540. configuration."
  541. :group 'org-export-e-odt
  542. :version "24.1"
  543. :type
  544. '(choice
  545. (const :tag "None" nil)
  546. (alist :tag "Capabilities"
  547. :key-type (string :tag "Document Class")
  548. :value-type
  549. (group (repeat :tag "Input formats" (string :tag "Input format"))
  550. (alist :tag "Output formats"
  551. :key-type (string :tag "Output format")
  552. :value-type
  553. (group (string :tag "Output file extension")
  554. (choice
  555. (const :tag "None" nil)
  556. (string :tag "Extra options"))))))))
  557. (defcustom org-e-odt-preferred-output-format nil
  558. "Automatically post-process to this format after exporting to \"odt\".
  559. Command `org-e-odt-export-to-odt' exports first to \"odt\" format
  560. and then uses `org-e-odt-convert-process' to convert the
  561. resulting document to this format. During customization of this
  562. variable, the list of valid values are populated based on
  563. `org-e-odt-convert-capabilities'.
  564. You can set this option on per-file basis using file local
  565. values. See Info node `(emacs) File Variables'."
  566. :group 'org-export-e-odt
  567. :version "24.1"
  568. :type '(choice :convert-widget
  569. (lambda (w)
  570. (apply 'widget-convert (widget-type w)
  571. (eval (car (widget-get w :args)))))
  572. `((const :tag "None" nil)
  573. ,@(mapcar (lambda (c)
  574. `(const :tag ,c ,c))
  575. (org-e-odt-reachable-formats "odt")))))
  576. ;;;###autoload
  577. (put 'org-e-odt-preferred-output-format 'safe-local-variable 'stringp)
  578. ;;;; Drawers
  579. (defcustom org-e-odt-format-drawer-function nil
  580. "Function called to format a drawer in HTML code.
  581. The function must accept two parameters:
  582. NAME the drawer name, like \"LOGBOOK\"
  583. CONTENTS the contents of the drawer.
  584. The function should return the string to be exported.
  585. For example, the variable could be set to the following function
  586. in order to mimic default behaviour:
  587. \(defun org-e-odt-format-drawer-default \(name contents\)
  588. \"Format a drawer element for HTML export.\"
  589. contents\)"
  590. :group 'org-export-e-odt
  591. :type 'function)
  592. ;;;; Headline
  593. (defcustom org-e-odt-format-headline-function nil
  594. "Function to format headline text.
  595. This function will be called with 5 arguments:
  596. TODO the todo keyword \(string or nil\).
  597. TODO-TYPE the type of todo \(symbol: `todo', `done', nil\)
  598. PRIORITY the priority of the headline \(integer or nil\)
  599. TEXT the main headline text \(string\).
  600. TAGS the tags string, separated with colons \(string or nil\).
  601. The function result will be used in the section format string.
  602. As an example, one could set the variable to the following, in
  603. order to reproduce the default set-up:
  604. \(defun org-e-odt-format-headline \(todo todo-type priority text tags\)
  605. \"Default format function for an headline.\"
  606. \(concat \(when todo
  607. \(format \"\\\\textbf{\\\\textsc{\\\\textsf{%s}}} \" todo\)\)
  608. \(when priority
  609. \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  610. text
  611. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)"
  612. :group 'org-export-e-odt
  613. :type 'function)
  614. ;;;; Inlinetasks
  615. (defcustom org-e-odt-format-inlinetask-function nil
  616. "Function called to format an inlinetask in HTML code.
  617. The function must accept six parameters:
  618. TODO the todo keyword, as a string
  619. TODO-TYPE the todo type, a symbol among `todo', `done' and nil.
  620. PRIORITY the inlinetask priority, as a string
  621. NAME the inlinetask name, as a string.
  622. TAGS the inlinetask tags, as a string.
  623. CONTENTS the contents of the inlinetask, as a string.
  624. The function should return the string to be exported.
  625. For example, the variable could be set to the following function
  626. in order to mimic default behaviour:
  627. \(defun org-e-odt-format-inlinetask \(todo type priority name tags contents\)
  628. \"Format an inline task element for HTML export.\"
  629. \(let \(\(full-title
  630. \(concat
  631. \(when todo
  632. \(format \"\\\\textbf{\\\\textsf{\\\\textsc{%s}}} \" todo\)\)
  633. \(when priority \(format \"\\\\framebox{\\\\#%c} \" priority\)\)
  634. title
  635. \(when tags \(format \"\\\\hfill{}\\\\textsc{%s}\" tags\)\)\)\)\)
  636. \(format \(concat \"\\\\begin{center}\\n\"
  637. \"\\\\fbox{\\n\"
  638. \"\\\\begin{minipage}[c]{.6\\\\textwidth}\\n\"
  639. \"%s\\n\\n\"
  640. \"\\\\rule[.8em]{\\\\textwidth}{2pt}\\n\\n\"
  641. \"%s\"
  642. \"\\\\end{minipage}}\"
  643. \"\\\\end{center}\"\)
  644. full-title contents\)\)"
  645. :group 'org-export-e-odt
  646. :type 'function)
  647. ;;;; Links
  648. (defcustom org-e-odt-inline-image-rules
  649. '(("file" . "\\.\\(jpeg\\|jpg\\|png\\|gif\\)\\'"))
  650. "Rules characterizing image files that can be inlined into HTML.
  651. A rule consists in an association whose key is the type of link
  652. to consider, and value is a regexp that will be matched against
  653. link's path.
  654. Note that, by default, the image extension *actually* allowed
  655. depend on the way the HTML file is processed. When used with
  656. pdflatex, pdf, jpg and png images are OK. When processing
  657. through dvi to Postscript, only ps and eps are allowed. The
  658. default we use here encompasses both."
  659. :group 'org-export-e-odt
  660. :type '(alist :key-type (string :tag "Type")
  661. :value-type (regexp :tag "Path")))
  662. (defcustom org-e-odt-pixels-per-inch display-pixels-per-inch
  663. "Scaling factor for converting images pixels to inches.
  664. Use this for sizing of embedded images. See Info node `(org)
  665. Images in ODT export' for more information."
  666. :type 'float
  667. :group 'org-export-e-odt
  668. :version "24.1")
  669. ;;;; Plain text
  670. (defcustom org-e-odt-quotes
  671. '(("fr"
  672. ("\\(\\s-\\|[[(]\\|^\\)\"" . "« ")
  673. ("\\(\\S-\\)\"" . "» ")
  674. ("\\(\\s-\\|(\\|^\\)'" . "'"))
  675. ("en"
  676. ("\\(\\s-\\|[[(]\\|^\\)\"" . "“")
  677. ("\\(\\S-\\)\"" . "”")
  678. ("\\(\\s-\\|(\\|^\\)'" . "‘")
  679. ("\\(\\S-\\)'" . "’")))
  680. "Alist for quotes to use when converting english double-quotes.
  681. The CAR of each item in this alist is the language code.
  682. The CDR of each item in this alist is a list of three CONS:
  683. - the first CONS defines the opening quote;
  684. - the second CONS defines the closing quote;
  685. - the last CONS defines single quotes.
  686. For each item in a CONS, the first string is a regexp
  687. for allowed characters before/after the quote, the second
  688. string defines the replacement string for this quote."
  689. :group 'org-export-e-odt
  690. :type '(list
  691. (cons :tag "Opening quote"
  692. (string :tag "Regexp for char before")
  693. (string :tag "Replacement quote "))
  694. (cons :tag "Closing quote"
  695. (string :tag "Regexp for char after ")
  696. (string :tag "Replacement quote "))
  697. (cons :tag "Single quote"
  698. (string :tag "Regexp for char before")
  699. (string :tag "Replacement quote "))))
  700. ;;;; Src Block
  701. (defcustom org-e-odt-create-custom-styles-for-srcblocks t
  702. "Whether custom styles for colorized source blocks be automatically created.
  703. When this option is turned on, the exporter creates custom styles
  704. for source blocks based on the advice of `htmlfontify'. Creation
  705. of custom styles happen as part of `org-e-odt-hfy-face-to-css'.
  706. When this option is turned off exporter does not create such
  707. styles.
  708. Use the latter option if you do not want the custom styles to be
  709. based on your current display settings. It is necessary that the
  710. styles.xml already contains needed styles for colorizing to work.
  711. This variable is effective only if
  712. `org-e-odt-fontify-srcblocks' is turned on."
  713. :group 'org-export-e-odt
  714. :version "24.1"
  715. :type 'boolean)
  716. (defcustom org-e-odt-fontify-srcblocks t
  717. "Specify whether or not source blocks need to be fontified.
  718. Turn this option on if you want to colorize the source code
  719. blocks in the exported file. For colorization to work, you need
  720. to make available an enhanced version of `htmlfontify' library."
  721. :type 'boolean
  722. :group 'org-export-e-odt
  723. :version "24.1")
  724. ;;;; Table
  725. (defcustom org-e-odt-table-caption-above t
  726. "When non-nil, place caption string at the beginning of the table.
  727. Otherwise, place it near the end."
  728. :group 'org-export-e-odt
  729. :type 'boolean)
  730. (defcustom org-e-odt-table-styles
  731. '(("OrgEquation" "OrgEquation"
  732. ((use-first-column-styles . t)
  733. (use-last-column-styles . t)))
  734. ("TableWithHeaderRowAndColumn" "Custom"
  735. ((use-first-row-styles . t)
  736. (use-first-column-styles . t)))
  737. ("TableWithFirstRowandLastRow" "Custom"
  738. ((use-first-row-styles . t)
  739. (use-last-row-styles . t)))
  740. ("GriddedTable" "Custom" nil))
  741. "Specify how Table Styles should be derived from a Table Template.
  742. This is a list where each element is of the
  743. form (TABLE-STYLE-NAME TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS).
  744. TABLE-STYLE-NAME is the style associated with the table through
  745. \"#+ATTR_ODT: :style TABLE-STYLE-NAME\" line.
  746. TABLE-TEMPLATE-NAME is a set of - upto 9 - automatic
  747. TABLE-CELL-STYLE-NAMEs and PARAGRAPH-STYLE-NAMEs (as defined
  748. below) that is included in
  749. `org-e-odt-content-template-file'.
  750. TABLE-CELL-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  751. \"TableCell\"
  752. PARAGRAPH-STYLE-NAME := TABLE-TEMPLATE-NAME + TABLE-CELL-TYPE +
  753. \"TableParagraph\"
  754. TABLE-CELL-TYPE := \"FirstRow\" | \"LastColumn\" |
  755. \"FirstRow\" | \"LastRow\" |
  756. \"EvenRow\" | \"OddRow\" |
  757. \"EvenColumn\" | \"OddColumn\" | \"\"
  758. where \"+\" above denotes string concatenation.
  759. TABLE-CELL-OPTIONS is an alist where each element is of the
  760. form (TABLE-CELL-STYLE-SELECTOR . ON-OR-OFF).
  761. TABLE-CELL-STYLE-SELECTOR := `use-first-row-styles' |
  762. `use-last-row-styles' |
  763. `use-first-column-styles' |
  764. `use-last-column-styles' |
  765. `use-banding-rows-styles' |
  766. `use-banding-columns-styles' |
  767. `use-first-row-styles'
  768. ON-OR-OFF := `t' | `nil'
  769. For example, with the following configuration
  770. \(setq org-e-odt-table-styles
  771. '\(\(\"TableWithHeaderRowsAndColumns\" \"Custom\"
  772. \(\(use-first-row-styles . t\)
  773. \(use-first-column-styles . t\)\)\)
  774. \(\"TableWithHeaderColumns\" \"Custom\"
  775. \(\(use-first-column-styles . t\)\)\)\)\)
  776. 1. A table associated with \"TableWithHeaderRowsAndColumns\"
  777. style will use the following table-cell styles -
  778. \"CustomFirstRowTableCell\", \"CustomFirstColumnTableCell\",
  779. \"CustomTableCell\" and the following paragraph styles
  780. \"CustomFirstRowTableParagraph\",
  781. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  782. as appropriate.
  783. 2. A table associated with \"TableWithHeaderColumns\" style will
  784. use the following table-cell styles -
  785. \"CustomFirstColumnTableCell\", \"CustomTableCell\" and the
  786. following paragraph styles
  787. \"CustomFirstColumnTableParagraph\", \"CustomTableParagraph\"
  788. as appropriate..
  789. Note that TABLE-TEMPLATE-NAME corresponds to the
  790. \"<table:table-template>\" elements contained within
  791. \"<office:styles>\". The entries (TABLE-STYLE-NAME
  792. TABLE-TEMPLATE-NAME TABLE-CELL-OPTIONS) correspond to
  793. \"table:template-name\" and \"table:use-first-row-styles\" etc
  794. attributes of \"<table:table>\" element. Refer ODF-1.2
  795. specification for more information. Also consult the
  796. implementation filed under `org-e-odt-get-table-cell-styles'.
  797. The TABLE-STYLE-NAME \"OrgEquation\" is used internally for
  798. formatting of numbered display equations. Do not delete this
  799. style from the list."
  800. :group 'org-export-e-odt
  801. :version "24.1"
  802. :type '(choice
  803. (const :tag "None" nil)
  804. (repeat :tag "Table Styles"
  805. (list :tag "Table Style Specification"
  806. (string :tag "Table Style Name")
  807. (string :tag "Table Template Name")
  808. (alist :options (use-first-row-styles
  809. use-last-row-styles
  810. use-first-column-styles
  811. use-last-column-styles
  812. use-banding-rows-styles
  813. use-banding-columns-styles)
  814. :key-type symbol
  815. :value-type (const :tag "True" t))))))
  816. ;;; Internal functions
  817. ;;;; Date
  818. (defun org-e-odt--date (&optional org-ts fmt)
  819. (save-match-data
  820. (let* ((time
  821. (and (stringp org-ts)
  822. (string-match org-ts-regexp0 org-ts)
  823. (apply 'encode-time
  824. (org-fix-decoded-time
  825. (org-parse-time-string (match-string 0 org-ts) t)))))
  826. date)
  827. (cond
  828. (fmt (format-time-string fmt time))
  829. (t (setq date (format-time-string "%Y-%m-%dT%H:%M:%S%z" time))
  830. (format "%s:%s" (substring date 0 -2) (substring date -2)))))))
  831. ;;;; Frame
  832. (defun org-e-odt--frame (text width height style &optional extra
  833. anchor-type)
  834. (let ((frame-attrs
  835. (concat
  836. (if width (format " svg:width=\"%0.2fcm\"" width) "")
  837. (if height (format " svg:height=\"%0.2fcm\"" height) "")
  838. extra
  839. (format " text:anchor-type=\"%s\"" (or anchor-type "paragraph")))))
  840. (format
  841. "\n<draw:frame draw:style-name=\"%s\"%s>\n%s\n</draw:frame>"
  842. style frame-attrs
  843. (concat text
  844. (let ((title (get-text-property 0 :title text))
  845. (desc (get-text-property 0 :description text)))
  846. (concat (and title
  847. (format "<svg:title>%s</svg:title>"
  848. (org-e-odt-encode-plain-text title t)))
  849. (and desc
  850. (format "<svg:desc>%s</svg:desc>"
  851. (org-e-odt-encode-plain-text desc t)))))))))
  852. ;;;; Inline Images
  853. (defun org-e-odt--copy-image-file (path)
  854. "Returns the internal name of the file"
  855. (let* ((image-type (file-name-extension path))
  856. (media-type (format "image/%s" image-type))
  857. (target-dir "Images/")
  858. (target-file
  859. (format "%s%04d.%s" target-dir
  860. (incf org-e-odt-embedded-images-count) image-type)))
  861. (message "Embedding %s as %s ..."
  862. (substring-no-properties path) target-file)
  863. (when (= 1 org-e-odt-embedded-images-count)
  864. (make-directory (concat org-e-odt-zip-dir target-dir))
  865. (org-e-odt-create-manifest-file-entry "" target-dir))
  866. (copy-file path (concat org-e-odt-zip-dir target-file) 'overwrite)
  867. (org-e-odt-create-manifest-file-entry media-type target-file)
  868. target-file))
  869. (defun org-e-odt--image-size (file &optional user-width
  870. user-height scale dpi embed-as)
  871. (let* ((--pixels-to-cms
  872. (function (lambda (pixels dpi)
  873. (let ((cms-per-inch 2.54)
  874. (inches (/ pixels dpi)))
  875. (* cms-per-inch inches)))))
  876. (--size-in-cms
  877. (function
  878. (lambda (size-in-pixels dpi)
  879. (and size-in-pixels
  880. (cons (funcall --pixels-to-cms (car size-in-pixels) dpi)
  881. (funcall --pixels-to-cms (cdr size-in-pixels) dpi))))))
  882. (dpi (or dpi org-e-odt-pixels-per-inch))
  883. (anchor-type (or embed-as "paragraph"))
  884. (user-width (and (not scale) user-width))
  885. (user-height (and (not scale) user-height))
  886. (size
  887. (and
  888. (not (and user-height user-width))
  889. (or
  890. ;; Use Imagemagick.
  891. (and (executable-find "identify")
  892. (let ((size-in-pixels
  893. (let ((dim (shell-command-to-string
  894. (format "identify -format \"%%w:%%h\" \"%s\""
  895. file))))
  896. (when (string-match "\\([0-9]+\\):\\([0-9]+\\)" dim)
  897. (cons (string-to-number (match-string 1 dim))
  898. (string-to-number (match-string 2 dim)))))))
  899. (funcall --size-in-cms size-in-pixels dpi)))
  900. ;; Use Emacs.
  901. (let ((size-in-pixels
  902. (ignore-errors ; Emacs could be in batch mode
  903. (clear-image-cache)
  904. (image-size (create-image file) 'pixels))))
  905. (funcall --size-in-cms size-in-pixels dpi))
  906. ;; Use hard-coded values.
  907. (cdr (assoc-string anchor-type
  908. org-e-odt-default-image-sizes-alist))
  909. ;; Error out.
  910. (error "Cannot determine Image size. Aborting ..."))))
  911. (width (car size)) (height (cdr size)))
  912. (cond
  913. (scale
  914. (setq width (* width scale) height (* height scale)))
  915. ((and user-height user-width)
  916. (setq width user-width height user-height))
  917. (user-height
  918. (setq width (* user-height (/ width height)) height user-height))
  919. (user-width
  920. (setq height (* user-width (/ height width)) width user-width))
  921. (t (ignore)))
  922. ;; ensure that an embedded image fits comfortably within a page
  923. (let ((max-width (car org-e-odt-max-image-size))
  924. (max-height (cdr org-e-odt-max-image-size)))
  925. (when (or (> width max-width) (> height max-height))
  926. (let* ((scale1 (/ max-width width))
  927. (scale2 (/ max-height height))
  928. (scale (min scale1 scale2)))
  929. (setq width (* scale width) height (* scale height)))))
  930. (cons width height)))
  931. (defun org-e-odt-link--inline-image (element info)
  932. "Return HTML code for an inline image.
  933. LINK is the link pointing to the inline image. INFO is a plist
  934. used as a communication channel."
  935. (let* ((src (cond
  936. ((eq (org-element-type element) 'link)
  937. (let* ((type (org-element-property :type element))
  938. (raw-path (org-element-property :path element)))
  939. (cond ((member type '("http" "https"))
  940. (concat type ":" raw-path))
  941. ((file-name-absolute-p raw-path)
  942. (expand-file-name raw-path))
  943. (t raw-path))))
  944. ((member (org-element-type element)
  945. '(latex-fragment latex-environment))
  946. (let* ((latex-frag (org-remove-indentation
  947. (org-element-property :value element)))
  948. (formula-link (org-e-odt-format-latex
  949. latex-frag 'dvipng info)))
  950. (and formula-link
  951. (string-match "file:\\([^]]*\\)" formula-link)
  952. (match-string 1 formula-link))))
  953. (t (error "what is this?"))))
  954. (src-expanded (if (file-name-absolute-p src) src
  955. (expand-file-name src (file-name-directory
  956. (plist-get info :input-file)))))
  957. (href (format
  958. "\n<draw:image xlink:href=\"%s\" xlink:type=\"simple\" xlink:show=\"embed\" xlink:actuate=\"onLoad\"/>"
  959. (org-e-odt--copy-image-file src-expanded)))
  960. ;; extract attributes from #+ATTR_ODT line.
  961. (attr-from (case (org-element-type element)
  962. (link (org-export-get-parent-element element))
  963. (t element)))
  964. ;; convert attributes to a plist.
  965. (attr-plist (org-export-read-attribute :attr_odt attr-from))
  966. ;; handle `:anchor', `:style' and `:attributes' properties.
  967. (user-frame-anchor
  968. (car (assoc-string (plist-get attr-plist :anchor)
  969. '(("as-char") ("paragraph") ("page")) t)))
  970. (user-frame-style
  971. (and user-frame-anchor (plist-get attr-plist :style)))
  972. (user-frame-attrs
  973. (and user-frame-anchor (plist-get attr-plist :attributes)))
  974. (user-frame-params
  975. (list user-frame-style user-frame-attrs user-frame-anchor))
  976. ;; (embed-as (or embed-as user-frame-anchor "paragraph"))
  977. ;; extrac
  978. ;; handle `:width', `:height' and `:scale' properties.
  979. (size (org-e-odt--image-size
  980. src-expanded (plist-get attr-plist :width)
  981. (plist-get attr-plist :height)
  982. (plist-get attr-plist :scale) nil ;; embed-as
  983. "paragraph" ; FIXME
  984. ))
  985. (width (car size)) (height (cdr size))
  986. (embed-as
  987. (case (org-element-type element)
  988. ((org-e-odt-standalone-image-p element info) "paragraph")
  989. (latex-fragment "as-char")
  990. (latex-environment "paragraph")
  991. (t "paragraph")))
  992. (captions (org-e-odt-format-label element info 'definition))
  993. (caption (car captions)) (short-caption (cdr captions))
  994. (entity (concat (and caption "Captioned") embed-as "Image")))
  995. (org-e-odt-format-entity entity href width height
  996. captions user-frame-params )))
  997. ;;;; Library wrappers
  998. (defun org-e-odt--zip-extract (archive members target)
  999. (when (atom members) (setq members (list members)))
  1000. (mapc (lambda (archive member target)
  1001. (require 'arc-mode)
  1002. (let* ((--quote-file-name
  1003. ;; This is shamelessly stolen from `archive-zip-extract'.
  1004. (lambda (name)
  1005. (if (or (not (memq system-type '(windows-nt ms-dos)))
  1006. (and (boundp 'w32-quote-process-args)
  1007. (null w32-quote-process-args)))
  1008. (shell-quote-argument name)
  1009. name)))
  1010. (target (funcall --quote-file-name target))
  1011. (archive (expand-file-name archive))
  1012. (archive-zip-extract
  1013. (list "unzip" "-qq" "-o" "-d" target))
  1014. exit-code command-output)
  1015. (setq command-output
  1016. (with-temp-buffer
  1017. (setq exit-code (archive-zip-extract archive member))
  1018. (buffer-string)))
  1019. (unless (zerop exit-code)
  1020. (message command-output)
  1021. (error "Extraction failed"))))
  1022. members))
  1023. ;;;; Target
  1024. (defun org-e-odt--target (text id)
  1025. (if (not id) text
  1026. (concat
  1027. (format "\n<text:bookmark-start text:name=\"OrgXref.%s\"/>" id)
  1028. (format "\n<text:bookmark text:name=\"%s\"/>" id) text
  1029. (format "\n<text:bookmark-end text:name=\"OrgXref.%s\"/>" id))))
  1030. ;;;; Textbox
  1031. (defun org-e-odt--textbox (text width height style &optional
  1032. extra anchor-type)
  1033. (org-e-odt--frame
  1034. (format "\n<draw:text-box %s>%s\n</draw:text-box>"
  1035. (concat (format " fo:min-height=\"%0.2fcm\"" (or height .2))
  1036. (and (not width)
  1037. (format " fo:min-width=\"%0.2fcm\"" (or width .2))))
  1038. text)
  1039. width nil style extra anchor-type))
  1040. ;;;; Table of Contents
  1041. (defun org-e-odt-begin-toc (index-title depth)
  1042. (concat
  1043. (format "
  1044. <text:table-of-content text:style-name=\"Sect2\" text:protected=\"true\" text:name=\"Table of Contents1\">
  1045. <text:table-of-content-source text:outline-level=\"%d\">
  1046. <text:index-title-template text:style-name=\"Contents_20_Heading\">%s</text:index-title-template>
  1047. " depth index-title)
  1048. (let ((levels (number-sequence 1 10)))
  1049. (mapconcat
  1050. (lambda (level)
  1051. (format
  1052. "
  1053. <text:table-of-content-entry-template text:outline-level=\"%d\" text:style-name=\"Contents_20_%d\">
  1054. <text:index-entry-link-start text:style-name=\"Internet_20_link\"/>
  1055. <text:index-entry-chapter/>
  1056. <text:index-entry-text/>
  1057. <text:index-entry-link-end/>
  1058. </text:table-of-content-entry-template>
  1059. " level level)) levels ""))
  1060. (format "
  1061. </text:table-of-content-source>
  1062. <text:index-body>
  1063. <text:index-title text:style-name=\"Sect1\" text:name=\"Table of Contents1_Head\">
  1064. <text:p text:style-name=\"Contents_20_Heading\">%s</text:p>
  1065. </text:index-title>
  1066. " index-title)))
  1067. (defun org-e-odt-end-toc ()
  1068. (format "
  1069. </text:index-body>
  1070. </text:table-of-content>
  1071. "))
  1072. (defun* org-e-odt-format-toc-headline
  1073. (todo todo-type priority text tags
  1074. &key level section-number headline-label &allow-other-keys)
  1075. (setq text (concat
  1076. (and org-export-with-section-numbers
  1077. (concat section-number ". "))
  1078. text
  1079. (and tags
  1080. (concat
  1081. "<text:tab/>"
  1082. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1083. "OrgTag" tags)))))
  1084. (when todo
  1085. (setq text (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1086. "OrgTodo" text)))
  1087. (format "<text:a xlink:type=\"simple\" xlink:href=\"#%s\">%s</text:a>"
  1088. headline-label text))
  1089. (defun org-e-odt-toc (depth info)
  1090. (assert (wholenump depth))
  1091. (let* ((title (org-export-translate "Table of Contents" :utf-8 info))
  1092. (headlines (org-export-collect-headlines
  1093. info (and (wholenump depth) depth))))
  1094. (when headlines
  1095. (concat
  1096. (org-e-odt-begin-toc title depth)
  1097. (mapconcat
  1098. (lambda (headline)
  1099. (let* ((entry (org-e-odt-format-headline--wrap
  1100. headline info 'org-e-odt-format-toc-headline))
  1101. (level (org-export-get-relative-level headline info))
  1102. (style (format "Contents_20_%d" level)))
  1103. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1104. style entry)))
  1105. headlines "\n")
  1106. (org-e-odt-end-toc)))))
  1107. ;;;; Document styles
  1108. (defun org-e-odt-add-automatic-style (object-type &optional object-props)
  1109. "Create an automatic style of type OBJECT-TYPE with param OBJECT-PROPS.
  1110. OBJECT-PROPS is (typically) a plist created by passing
  1111. \"#+ATTR_ODT: \" option of the object in question to
  1112. `org-e-odt-parse-block-attributes'.
  1113. Use `org-e-odt-object-counters' to generate an automatic
  1114. OBJECT-NAME and STYLE-NAME. If OBJECT-PROPS is non-nil, add a
  1115. new entry in `org-e-odt-automatic-styles'. Return (OBJECT-NAME
  1116. . STYLE-NAME)."
  1117. (assert (stringp object-type))
  1118. (let* ((object (intern object-type))
  1119. (seqvar object)
  1120. (seqno (1+ (or (plist-get org-e-odt-object-counters seqvar) 0)))
  1121. (object-name (format "%s%d" object-type seqno)) style-name)
  1122. (setq org-e-odt-object-counters
  1123. (plist-put org-e-odt-object-counters seqvar seqno))
  1124. (when object-props
  1125. (setq style-name (format "Org%s" object-name))
  1126. (setq org-e-odt-automatic-styles
  1127. (plist-put org-e-odt-automatic-styles object
  1128. (append (list (list style-name object-props))
  1129. (plist-get org-e-odt-automatic-styles object)))))
  1130. (cons object-name style-name)))
  1131. ;;;; Caption and Labels
  1132. (defun org-e-odt--wrap-label (element output)
  1133. "Wrap label associated to ELEMENT around OUTPUT, if appropriate.
  1134. This function shouldn't be used for floats. See
  1135. `org-e-odt--caption/label-string'."
  1136. ;; (let ((label (org-element-property :name element)))
  1137. ;; (if (or (not output) (not label) (string= output "") (string= label ""))
  1138. ;; output
  1139. ;; (concat (format "\\label{%s}\n" label) output)))
  1140. output)
  1141. (defun org-e-odt--caption/label-string (caption label info)
  1142. "Return caption and label HTML string for floats.
  1143. CAPTION is a cons cell of secondary strings, the car being the
  1144. standard caption and the cdr its short form. LABEL is a string
  1145. representing the label. INFO is a plist holding contextual
  1146. information.
  1147. If there's no caption nor label, return the empty string.
  1148. For non-floats, see `org-e-odt--wrap-label'."
  1149. (setq label nil) ;; FIXME
  1150. (let ((label-str (if label (format "\\label{%s}" label) "")))
  1151. (cond
  1152. ((and (not caption) (not label)) "")
  1153. ((not caption) (format "\\label{%s}\n" label))
  1154. ;; Option caption format with short name.
  1155. ((cdr caption)
  1156. (format "\\caption[%s]{%s%s}\n"
  1157. (org-export-data (cdr caption) info)
  1158. label-str
  1159. (org-export-data (car caption) info)))
  1160. ;; Standard caption format.
  1161. ;; (t (format "\\caption{%s%s}\n"
  1162. ;; label-str
  1163. ;; (org-export-data (car caption) info)))
  1164. (t (org-export-data (car caption) info)))))
  1165. ;;;; Checkbox
  1166. (defun org-e-odt--checkbox (item)
  1167. "Return check-box string associated to ITEM."
  1168. (let ((checkbox (org-element-property :checkbox item)))
  1169. (if (not checkbox) ""
  1170. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1171. "OrgCode" (case checkbox
  1172. (on "[&#x2713;] ") ; CHECK MARK
  1173. (off "[ ] ")
  1174. (trans "[-] "))))))
  1175. ;;; Template
  1176. (defun org-e-odt-template (contents info)
  1177. "Return complete document string after HTML conversion.
  1178. CONTENTS is the transcoded contents string. RAW-DATA is the
  1179. original parsed data. INFO is a plist holding export options."
  1180. ;; Write meta file.
  1181. (let ((title (org-export-data (plist-get info :title) info))
  1182. (author (let ((author (plist-get info :author)))
  1183. (if (not author) "" (org-export-data author info))))
  1184. (date (org-e-odt--date
  1185. (org-export-data (plist-get info :date) info)))
  1186. (email (plist-get info :email))
  1187. (keywords (plist-get info :keywords))
  1188. (description (plist-get info :description)))
  1189. (write-region
  1190. (concat
  1191. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  1192. <office:document-meta
  1193. xmlns:office=\"urn:oasis:names:tc:opendocument:xmlns:office:1.0\"
  1194. xmlns:xlink=\"http://www.w3.org/1999/xlink\"
  1195. xmlns:dc=\"http://purl.org/dc/elements/1.1/\"
  1196. xmlns:meta=\"urn:oasis:names:tc:opendocument:xmlns:meta:1.0\"
  1197. xmlns:ooo=\"http://openoffice.org/2004/office\"
  1198. office:version=\"1.2\">
  1199. <office:meta>\n"
  1200. (format "<dc:creator>%s</dc:creator>\n" author)
  1201. (format "<meta:initial-creator>%s</meta:initial-creator>\n" author)
  1202. (format "<dc:date>%s</dc:date>\n" date)
  1203. (format "<meta:creation-date>%s</meta:creation-date>\n" date)
  1204. (format "<meta:generator>%s</meta:generator>\n"
  1205. (let ((creator-info (plist-get info :with-creator)))
  1206. (if (or (not creator-info) (eq creator-info 'comment)) ""
  1207. (plist-get info :creator))))
  1208. (format "<meta:keyword>%s</meta:keyword>\n" keywords)
  1209. (format "<dc:subject>%s</dc:subject>\n" description)
  1210. (format "<dc:title>%s</dc:title>\n" title)
  1211. "\n"
  1212. " </office:meta>\n" "</office:document-meta>")
  1213. nil (concat org-e-odt-zip-dir "meta.xml"))
  1214. ;; Add meta.xml in to manifest.
  1215. (org-e-odt-create-manifest-file-entry "text/xml" "meta.xml"))
  1216. ;; Update styles file.
  1217. ;; Copy styles.xml. Also dump htmlfontify styles, if there is any.
  1218. ;; Write styles file.
  1219. (let* ((styles-file (plist-get info :odt-styles-file))
  1220. (styles-file (and styles-file (read (org-trim styles-file))))
  1221. ;; Non-availability of styles.xml is not a critical
  1222. ;; error. For now throw an error purely for aesthetic
  1223. ;; reasons.
  1224. (styles-file (or styles-file
  1225. org-e-odt-styles-file
  1226. (expand-file-name "OrgOdtStyles.xml"
  1227. org-e-odt-styles-dir)
  1228. (error "org-e-odt: Missing styles file?"))))
  1229. (cond
  1230. ((listp styles-file)
  1231. (let ((archive (nth 0 styles-file))
  1232. (members (nth 1 styles-file)))
  1233. (org-e-odt--zip-extract archive members org-e-odt-zip-dir)
  1234. (mapc
  1235. (lambda (member)
  1236. (when (org-file-image-p member)
  1237. (let* ((image-type (file-name-extension member))
  1238. (media-type (format "image/%s" image-type)))
  1239. (org-e-odt-create-manifest-file-entry media-type member))))
  1240. members)))
  1241. ((and (stringp styles-file) (file-exists-p styles-file))
  1242. (let ((styles-file-type (file-name-extension styles-file)))
  1243. (cond
  1244. ((string= styles-file-type "xml")
  1245. (copy-file styles-file (concat org-e-odt-zip-dir "styles.xml") t))
  1246. ((member styles-file-type '("odt" "ott"))
  1247. (org-e-odt--zip-extract styles-file "styles.xml" org-e-odt-zip-dir)))))
  1248. (t
  1249. (error (format "Invalid specification of styles.xml file: %S"
  1250. org-e-odt-styles-file))))
  1251. ;; create a manifest entry for styles.xml
  1252. (org-e-odt-create-manifest-file-entry "text/xml" "styles.xml")
  1253. ;; FIXME: Who is opening an empty styles.xml before this point?
  1254. (with-current-buffer
  1255. (find-file-noselect (concat org-e-odt-zip-dir "styles.xml") t)
  1256. (revert-buffer t t)
  1257. ;; Write custom styles for source blocks
  1258. ;; Save STYLES used for colorizing of source blocks.
  1259. ;; Update styles.xml with styles that were collected as part of
  1260. ;; `org-e-odt-hfy-face-to-css' callbacks.
  1261. (let ((styles (mapconcat (lambda (style) (format " %s\n" (cddr style)))
  1262. hfy-user-sheet-assoc "")))
  1263. (when styles
  1264. (goto-char (point-min))
  1265. (when (re-search-forward "</office:styles>" nil t)
  1266. (goto-char (match-beginning 0))
  1267. (insert "\n<!-- Org Htmlfontify Styles -->\n" styles "\n"))))
  1268. ;; Update styles.xml - take care of outline numbering
  1269. ;; Don't make automatic backup of styles.xml file. This setting
  1270. ;; prevents the backed-up styles.xml file from being zipped in to
  1271. ;; odt file. This is more of a hackish fix. Better alternative
  1272. ;; would be to fix the zip command so that the output odt file
  1273. ;; includes only the needed files and excludes any auto-generated
  1274. ;; extra files like backups and auto-saves etc etc. Note that
  1275. ;; currently the zip command zips up the entire temp directory so
  1276. ;; that any auto-generated files created under the hood ends up in
  1277. ;; the resulting odt file.
  1278. (set (make-local-variable 'backup-inhibited) t)
  1279. ;; Outline numbering is retained only upto LEVEL.
  1280. ;; To disable outline numbering pass a LEVEL of 0.
  1281. (goto-char (point-min))
  1282. (let ((regex
  1283. "<text:outline-level-style\\([^>]*\\)text:level=\"\\([^\"]*\\)\"\\([^>]*\\)>")
  1284. (replacement
  1285. "<text:outline-level-style\\1text:level=\"\\2\" style:num-format=\"\">"))
  1286. (while (re-search-forward regex nil t)
  1287. (unless (let ((sec-num (plist-get info :section-numbers))
  1288. (level (string-to-number (match-string 2))))
  1289. (if (wholenump sec-num) (<= level sec-num) sec-num))
  1290. (replace-match replacement t nil))))
  1291. (save-buffer 0)))
  1292. ;; Update content.xml.
  1293. (with-temp-buffer
  1294. (insert-file-contents
  1295. (or org-e-odt-content-template-file
  1296. (expand-file-name "OrgOdtContentTemplate.xml"
  1297. org-e-odt-styles-dir)))
  1298. ;; Write automatic styles.
  1299. ;; - Position the cursor.
  1300. (goto-char (point-min))
  1301. (re-search-forward " </office:automatic-styles>" nil t)
  1302. (goto-char (match-beginning 0))
  1303. ;; - Dump automatic table styles
  1304. (loop for (style-name props) in
  1305. (plist-get org-e-odt-automatic-styles 'Table) do
  1306. (when (setq props (or (plist-get props :rel-width) 96))
  1307. (insert (format org-e-odt-table-style-format style-name props))))
  1308. ;; Update display level.
  1309. ;; - Remove existing sequence decls. Also position the cursor.
  1310. (goto-char (point-min))
  1311. (when (re-search-forward "<text:sequence-decls" nil t)
  1312. (delete-region (match-beginning 0)
  1313. (re-search-forward "</text:sequence-decls>" nil nil)))
  1314. ;; Update sequence decls according to user preference.
  1315. (insert
  1316. (format
  1317. "\n<text:sequence-decls>\n%s\n</text:sequence-decls>"
  1318. (mapconcat
  1319. (lambda (x)
  1320. (format
  1321. "<text:sequence-decl text:display-outline-level=\"%d\" text:name=\"%s\"/>"
  1322. org-e-odt-display-outline-level (nth 1 x)))
  1323. org-e-odt-category-map-alist "\n")))
  1324. ;; Position the cursor to document body.
  1325. (goto-char (point-min))
  1326. (re-search-forward "</office:text>" nil nil)
  1327. (goto-char (match-beginning 0))
  1328. ;; Preamble - Title, Author, Date etc.
  1329. (insert
  1330. (let* ((title (org-export-data (plist-get info :title) info))
  1331. (author (and (plist-get info :with-author)
  1332. (let ((auth (plist-get info :author)))
  1333. (and auth (org-export-data auth info)))))
  1334. (date (org-export-data (plist-get info :date) info))
  1335. (iso-date (org-e-odt--date date))
  1336. (date (org-e-odt--date date "%d %b %Y"))
  1337. (email (plist-get info :email))
  1338. ;; switch on or off above vars based on user settings
  1339. (author (and (plist-get info :with-author) (or author email)))
  1340. ;; (date (and (plist-get info :time-stamp-file) date))
  1341. (email (and (plist-get info :with-email) email)))
  1342. (concat
  1343. ;; title
  1344. (when title
  1345. (concat
  1346. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1347. "OrgTitle" (format "\n<text:title>%s</text:title>" title))
  1348. ;; separator
  1349. "\n<text:p text:style-name=\"OrgTitle\"/>"))
  1350. (cond
  1351. ((and author (not email))
  1352. ;; author only
  1353. (concat
  1354. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1355. "OrgSubtitle"
  1356. (format "<text:initial-creator>%s</text:initial-creator>" author))
  1357. ;; separator
  1358. "\n<text:p text:style-name=\"OrgSubtitle\"/>"))
  1359. ((and author email)
  1360. ;; author and email
  1361. (concat
  1362. (format
  1363. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1364. "OrgSubtitle"
  1365. (format
  1366. "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  1367. (concat "mailto:" email)
  1368. (format "<text:initial-creator>%s</text:initial-creator>" author)))
  1369. ;; separator
  1370. "\n<text:p text:style-name=\"OrgSubtitle\"/>")))
  1371. ;; date
  1372. (when date
  1373. (concat
  1374. (format
  1375. "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1376. "OrgSubtitle"
  1377. (format
  1378. "\n<text:date style:data-style-name=\"%s\" text:date-value=\"%s\">%s</text:date>"
  1379. "OrgDate" iso-date date))
  1380. ;; separator
  1381. "<text:p text:style-name=\"OrgSubtitle\"/>")))))
  1382. ;; Table of Contents
  1383. (let* ((with-toc (plist-get info :with-toc))
  1384. (depth (and with-toc (if (wholenump with-toc)
  1385. with-toc
  1386. (plist-get info :headline-levels)))))
  1387. (when depth (insert (or (org-e-odt-toc depth info) ""))))
  1388. ;; Contents.
  1389. (insert contents)
  1390. ;; Return contents.
  1391. (buffer-substring-no-properties (point-min) (point-max))))
  1392. ;;; Transcode Functions
  1393. ;;;; Bold
  1394. (defun org-e-odt-bold (bold contents info)
  1395. "Transcode BOLD from Org to ODT.
  1396. CONTENTS is the text with bold markup. INFO is a plist holding
  1397. contextual information."
  1398. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1399. "Bold" contents))
  1400. ;;;; Center Block
  1401. (defun org-e-odt-center-block (center-block contents info)
  1402. "Transcode a CENTER-BLOCK element from Org to ODT.
  1403. CONTENTS holds the contents of the center block. INFO is a plist
  1404. holding contextual information."
  1405. (org-e-odt--wrap-label center-block contents))
  1406. ;;;; Clock
  1407. (defun org-e-odt-clock (clock contents info)
  1408. "Transcode a CLOCK element from Org to ODT.
  1409. CONTENTS is nil. INFO is a plist used as a communication
  1410. channel."
  1411. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1412. "OrgTimestampWrapper"
  1413. (concat
  1414. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1415. "OrgTimestampKeyword" org-clock-string)
  1416. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1417. "OrgTimestamp"
  1418. (concat (org-translate-time
  1419. (org-element-property :value clock))
  1420. (let ((time (org-element-property :time clock)))
  1421. (and time (format " (%s)" time))))))))
  1422. ;;;; Code
  1423. (defun org-e-odt-code (code contents info)
  1424. "Transcode a CODE object from Org to ODT.
  1425. CONTENTS is nil. INFO is a plist used as a communication
  1426. channel."
  1427. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1428. "OrgCode" (org-element-property :value code)))
  1429. ;;;; Comment
  1430. ;; Comments are ignored.
  1431. ;;;; Comment Block
  1432. ;; Comment Blocks are ignored.
  1433. ;;;; Drawer
  1434. (defun org-e-odt-drawer (drawer contents info)
  1435. "Transcode a DRAWER element from Org to ODT.
  1436. CONTENTS holds the contents of the block. INFO is a plist
  1437. holding contextual information."
  1438. (let* ((name (org-element-property :drawer-name drawer))
  1439. (output (if (functionp org-e-odt-format-drawer-function)
  1440. (funcall org-e-odt-format-drawer-function
  1441. name contents)
  1442. ;; If there's no user defined function: simply
  1443. ;; display contents of the drawer.
  1444. contents)))
  1445. (org-e-odt--wrap-label drawer output)))
  1446. ;;;; Dynamic Block
  1447. (defun org-e-odt-dynamic-block (dynamic-block contents info)
  1448. "Transcode a DYNAMIC-BLOCK element from Org to ODT.
  1449. CONTENTS holds the contents of the block. INFO is a plist
  1450. holding contextual information. See `org-export-data'."
  1451. (org-e-odt--wrap-label dynamic-block contents))
  1452. ;;;; Entity
  1453. (defun org-e-odt-entity (entity contents info)
  1454. "Transcode an ENTITY object from Org to ODT.
  1455. CONTENTS are the definition itself. INFO is a plist holding
  1456. contextual information."
  1457. ;; (let ((ent (org-element-property :latex entity)))
  1458. ;; (if (org-element-property :latex-math-p entity)
  1459. ;; (format "$%s$" ent)
  1460. ;; ent))
  1461. (org-element-property :utf-8 entity))
  1462. ;;;; Example Block
  1463. (defun org-e-odt-example-block (example-block contents info)
  1464. "Transcode a EXAMPLE-BLOCK element from Org to ODT.
  1465. CONTENTS is nil. INFO is a plist holding contextual information."
  1466. (org-e-odt--wrap-label
  1467. example-block (org-e-odt-format-code example-block info)))
  1468. ;;;; Export Snippet
  1469. (defun org-e-odt-export-snippet (export-snippet contents info)
  1470. "Transcode a EXPORT-SNIPPET object from Org to ODT.
  1471. CONTENTS is nil. INFO is a plist holding contextual information."
  1472. (when (eq (org-export-snippet-backend export-snippet) 'e-odt)
  1473. (org-element-property :value export-snippet)))
  1474. ;;;; Export Block
  1475. (defun org-e-odt-export-block (export-block contents info)
  1476. "Transcode a EXPORT-BLOCK element from Org to ODT.
  1477. CONTENTS is nil. INFO is a plist holding contextual information."
  1478. (when (string= (org-element-property :type export-block) "ODT")
  1479. (org-remove-indentation (org-element-property :value export-block))))
  1480. ;;;; Fixed Width
  1481. (defun org-e-odt-fixed-width (fixed-width contents info)
  1482. "Transcode a FIXED-WIDTH element from Org to ODT.
  1483. CONTENTS is nil. INFO is a plist holding contextual information."
  1484. (org-e-odt--wrap-label
  1485. fixed-width (org-e-odt-do-format-code
  1486. (org-element-property :value fixed-width))))
  1487. ;;;; Footnote Definition
  1488. ;; Footnote Definitions are ignored.
  1489. ;;;; Footnote Reference
  1490. (defun org-e-odt-footnote-reference (footnote-reference contents info)
  1491. "Transcode a FOOTNOTE-REFERENCE element from Org to ODT.
  1492. CONTENTS is nil. INFO is a plist holding contextual information."
  1493. (let ((--format-footnote-definition
  1494. (function
  1495. (lambda (n def)
  1496. (setq n (format "%d" n))
  1497. (let ((id (concat "fn" n))
  1498. (note-class "footnote")
  1499. (par-style "Footnote"))
  1500. (format
  1501. "<text:note text:id=\"%s\" text:note-class=\"%s\">%s</text:note>"
  1502. id note-class
  1503. (concat
  1504. (format "<text:note-citation>%s</text:note-citation>" n)
  1505. (format "<text:note-body>%s</text:note-body>" def)))))))
  1506. (--format-footnote-reference
  1507. (function
  1508. (lambda (n)
  1509. (setq n (format "%d" n))
  1510. (let ((note-class "footnote")
  1511. (ref-format "text")
  1512. (ref-name (concat "fn" n)))
  1513. (format
  1514. "<text:span text:style-name=\"%s\">%s</text:span>"
  1515. "OrgSuperscript"
  1516. (format "<text:note-ref text:note-class=\"%s\" text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:note-ref>"
  1517. note-class ref-format ref-name n)))))))
  1518. (concat
  1519. ;; Insert separator between two footnotes in a row.
  1520. (let ((prev (org-export-get-previous-element footnote-reference info)))
  1521. (and (eq (org-element-type prev) 'footnote-reference)
  1522. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1523. "OrgSuperscript" ",")))
  1524. ;; Trancode footnote reference.
  1525. (let ((n (org-export-get-footnote-number footnote-reference info)))
  1526. (cond
  1527. ((not (org-export-footnote-first-reference-p footnote-reference info))
  1528. (funcall --format-footnote-reference n))
  1529. ;; Inline definitions are secondary strings.
  1530. ;; Non-inline footnotes definitions are full Org data.
  1531. (t
  1532. (let* ((raw (org-export-get-footnote-definition footnote-reference
  1533. info))
  1534. (def (let ((def (org-trim (org-export-data raw info))))
  1535. (if (eq (org-element-type raw) 'org-data) def
  1536. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1537. "Footnote" def)))))
  1538. (funcall --format-footnote-definition n def))))))))
  1539. ;;;; Headline
  1540. (defun* org-e-odt-format-headline
  1541. (todo todo-type priority text tags
  1542. &key level section-number headline-label &allow-other-keys)
  1543. (concat
  1544. ;; Todo.
  1545. (and todo
  1546. (concat
  1547. (let ((style (if (member todo org-done-keywords) "OrgDone" "OrgTodo")))
  1548. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1549. style todo)) " "))
  1550. ;; Title.
  1551. text
  1552. ;; Tags.
  1553. (and tags
  1554. (concat "<text:tab/>"
  1555. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1556. "OrgTag" (mapconcat 'org-trim tags " : "))))))
  1557. (defun org-e-odt-format-headline--wrap (headline info
  1558. &optional format-function
  1559. &rest extra-keys)
  1560. "Transcode an HEADLINE element from Org to ODT.
  1561. CONTENTS holds the contents of the headline. INFO is a plist
  1562. holding contextual information."
  1563. (let* ((level (+ (org-export-get-relative-level headline info)))
  1564. (headline-number (org-export-get-headline-number headline info))
  1565. (section-number (and (org-export-numbered-headline-p headline info)
  1566. (mapconcat 'number-to-string
  1567. headline-number ".")))
  1568. (todo (and (plist-get info :with-todo-keywords)
  1569. (let ((todo (org-element-property :todo-keyword headline)))
  1570. (and todo (org-export-data todo info)))))
  1571. (todo-type (and todo (org-element-property :todo-type headline)))
  1572. (priority (and (plist-get info :with-priority)
  1573. (org-element-property :priority headline)))
  1574. (text (org-export-data (org-element-property :title headline) info))
  1575. (tags (and (plist-get info :with-tags)
  1576. (org-export-get-tags headline info)))
  1577. (headline-label (concat "sec-" (mapconcat 'number-to-string
  1578. headline-number "-")))
  1579. (format-function (cond
  1580. ((functionp format-function) format-function)
  1581. ((functionp org-e-odt-format-headline-function)
  1582. (function*
  1583. (lambda (todo todo-type priority text tags
  1584. &allow-other-keys)
  1585. (funcall org-e-odt-format-headline-function
  1586. todo todo-type priority text tags))))
  1587. (t 'org-e-odt-format-headline))))
  1588. (apply format-function
  1589. todo todo-type priority text tags
  1590. :headline-label headline-label :level level
  1591. :section-number section-number extra-keys)))
  1592. (defun org-e-odt-headline (headline contents info)
  1593. "Transcode an HEADLINE element from Org to ODT.
  1594. CONTENTS holds the contents of the headline. INFO is a plist
  1595. holding contextual information."
  1596. ;; Case 1: This is a footnote section: ignore it.
  1597. (unless (org-element-property :footnote-section-p headline)
  1598. (let* ((text (org-export-data (org-element-property :title headline) info))
  1599. ;; Create the headline text.
  1600. (full-text (org-e-odt-format-headline--wrap headline info))
  1601. ;; Get level relative to current parsed data.
  1602. (level (org-export-get-relative-level headline info))
  1603. ;; Get canonical label for the headline.
  1604. (id (concat "sec-" (mapconcat 'number-to-string
  1605. (org-export-get-headline-number
  1606. headline info) "-")))
  1607. ;; Get user-specified labels for the headline.
  1608. (extra-ids (list (org-element-property :custom-id headline)
  1609. (org-element-property :id headline)))
  1610. ;; Extra targets.
  1611. (extra-targets
  1612. (mapconcat (lambda (x)
  1613. (when x
  1614. (let ((x (if (org-uuidgen-p x) (concat "ID-" x) x)))
  1615. (org-e-odt--target
  1616. "" (org-export-solidify-link-text x)))))
  1617. extra-ids ""))
  1618. ;; Title.
  1619. (anchored-title (org-e-odt--target full-text id)))
  1620. (cond
  1621. ;; Case 2. This is a deep sub-tree: export it as a list item.
  1622. ;; Also export as items headlines for which no section
  1623. ;; format has been found.
  1624. ((org-export-low-level-p headline info)
  1625. ;; Build the real contents of the sub-tree.
  1626. (concat
  1627. (and (org-export-first-sibling-p headline info)
  1628. (format "\n<text:list text:style-name=\"%s\" %s>"
  1629. ;; Choose style based on list type.
  1630. (if (org-export-numbered-headline-p headline info)
  1631. "OrgNumberedList" "OrgBulletedList")
  1632. ;; If top-level list, re-start numbering. Otherwise,
  1633. ;; continue numbering.
  1634. (format "text:continue-numbering=\"%s\""
  1635. (let* ((parent (org-export-get-parent-headline
  1636. headline)))
  1637. (if (and parent
  1638. (org-export-low-level-p parent info))
  1639. "true" "false")))))
  1640. (let ((headline-has-table-p
  1641. (let ((section (assq 'section (org-element-contents headline))))
  1642. (assq 'table (and section (org-element-contents section))))))
  1643. (format "\n<text:list-item>\n%s\n%s"
  1644. (concat
  1645. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1646. "Text_20_body"
  1647. (concat extra-targets anchored-title))
  1648. contents)
  1649. (if headline-has-table-p
  1650. "</text:list-header>"
  1651. "</text:list-item>")))
  1652. (and (org-export-last-sibling-p headline info)
  1653. "</text:list>")))
  1654. ;; Case 3. Standard headline. Export it as a section.
  1655. (t
  1656. (concat
  1657. (format
  1658. "\n<text:h text:style-name=\"%s\" text:outline-level=\"%s\">%s</text:h>"
  1659. (format "Heading_20_%s" level)
  1660. level
  1661. (concat extra-targets anchored-title))
  1662. contents))))))
  1663. ;;;; Horizontal Rule
  1664. (defun org-e-odt-horizontal-rule (horizontal-rule contents info)
  1665. "Transcode an HORIZONTAL-RULE object from Org to ODT.
  1666. CONTENTS is nil. INFO is a plist holding contextual information."
  1667. (org-e-odt--wrap-label
  1668. horizontal-rule
  1669. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1670. "Horizontal_20_Line" "")))
  1671. ;;;; Inline Babel Call
  1672. ;; Inline Babel Calls are ignored.
  1673. ;;;; Inline Src Block
  1674. (defun org-e-odt--find-verb-separator (s)
  1675. "Return a character not used in string S.
  1676. This is used to choose a separator for constructs like \\verb."
  1677. (let ((ll "~,./?;':\"|!@#%^&-_=+abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ<>()[]{}"))
  1678. (loop for c across ll
  1679. when (not (string-match (regexp-quote (char-to-string c)) s))
  1680. return (char-to-string c))))
  1681. (defun org-e-odt-inline-src-block (inline-src-block contents info)
  1682. "Transcode an INLINE-SRC-BLOCK element from Org to ODT.
  1683. CONTENTS holds the contents of the item. INFO is a plist holding
  1684. contextual information."
  1685. (let* ((org-lang (org-element-property :language inline-src-block))
  1686. (code (org-element-property :value inline-src-block))
  1687. (separator (org-e-odt--find-verb-separator code)))
  1688. (error "FIXME")))
  1689. ;;;; Inlinetask
  1690. (defun org-e-odt-inlinetask (inlinetask contents info)
  1691. "Transcode an INLINETASK element from Org to ODT.
  1692. CONTENTS holds the contents of the block. INFO is a plist
  1693. holding contextual information."
  1694. (cond
  1695. ;; If `org-e-odt-format-inlinetask-function' is provided, call it
  1696. ;; with appropriate arguments.
  1697. ((functionp org-e-odt-format-inlinetask-function)
  1698. (let ((format-function
  1699. (function*
  1700. (lambda (todo todo-type priority text tags
  1701. &key contents &allow-other-keys)
  1702. (funcall org-e-odt-format-inlinetask-function
  1703. todo todo-type priority text tags contents)))))
  1704. (org-e-odt-format-headline--wrap
  1705. inlinetask info format-function :contents contents)))
  1706. ;; Otherwise, use a default template.
  1707. (t (org-e-odt--wrap-label
  1708. inlinetask
  1709. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1710. "Text_20_body"
  1711. (org-e-odt--textbox
  1712. (concat
  1713. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1714. "OrgInlineTaskHeading"
  1715. (org-e-odt-format-headline--wrap
  1716. inlinetask info))
  1717. contents)
  1718. nil nil "OrgInlineTaskFrame" " style:rel-width=\"100%\""))))))
  1719. ;;;; Italic
  1720. (defun org-e-odt-italic (italic contents info)
  1721. "Transcode ITALIC from Org to ODT.
  1722. CONTENTS is the text with italic markup. INFO is a plist holding
  1723. contextual information."
  1724. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  1725. "Emphasis" contents))
  1726. ;;;; Item
  1727. (defun org-e-odt-item (item contents info)
  1728. "Transcode an ITEM element from Org to ODT.
  1729. CONTENTS holds the contents of the item. INFO is a plist holding
  1730. contextual information."
  1731. (let* ((plain-list (org-export-get-parent item))
  1732. (type (org-element-property :type plain-list))
  1733. (counter (org-element-property :counter item))
  1734. (tag (let ((tag (org-element-property :tag item)))
  1735. (and tag
  1736. (concat (org-e-odt--checkbox item)
  1737. (org-export-data tag info))))))
  1738. (case type
  1739. ((ordered unordered descriptive-1 descriptive-2)
  1740. (format "\n<text:list-item>\n%s\n%s"
  1741. contents
  1742. (let* ((--element-has-a-table-p
  1743. (function
  1744. (lambda (element info)
  1745. (loop for el in (org-element-contents element)
  1746. thereis (eq (org-element-type el) 'table))))))
  1747. (cond
  1748. ((funcall --element-has-a-table-p item info)
  1749. "</text:list-header>")
  1750. (t "</text:list-item>")))))
  1751. (t (error "Unknown list type: %S" type)))))
  1752. ;;;; Keyword
  1753. (defun org-e-odt-keyword (keyword contents info)
  1754. "Transcode a KEYWORD element from Org to ODT.
  1755. CONTENTS is nil. INFO is a plist holding contextual information."
  1756. (let ((key (org-element-property :key keyword))
  1757. (value (org-element-property :value keyword)))
  1758. (cond
  1759. ((string= key "ODT") value)
  1760. ((string= key "INDEX") (format "\\index{%s}" value))
  1761. ((string= key "TARGET") nil ; FIXME
  1762. ;; (format "\\label{%s}" (org-export-solidify-link-text value))
  1763. )
  1764. ((string= key "toc")
  1765. (let ((value (downcase value)))
  1766. (cond
  1767. ((string-match "\\<headlines\\>" value)
  1768. (let ((depth (or (and (string-match "[0-9]+" value)
  1769. (string-to-number (match-string 0 value)))
  1770. (plist-get info :with-toc))))
  1771. (when (wholenump depth) (org-e-odt-toc depth info))))
  1772. ((string= "tables" value) "FIXME")
  1773. ((string= "figures" value) "FIXME")
  1774. ((string= "listings" value)
  1775. (cond
  1776. ;; At the moment, src blocks with a caption are wrapped
  1777. ;; into a figure environment.
  1778. (t "FIXME")))))))))
  1779. ;;;; Latex Environment
  1780. (eval-after-load 'org-odt
  1781. '(ad-deactivate 'org-format-latex-as-mathml))
  1782. ;; (defadvice org-format-latex-as-mathml ; FIXME
  1783. ;; (after org-e-odt-protect-latex-fragment activate)
  1784. ;; "Encode LaTeX fragment as XML.
  1785. ;; Do this when translation to MathML fails."
  1786. ;; (when (or (not (> (length ad-return-value) 0))
  1787. ;; (get-text-property 0 'org-protected ad-return-value))
  1788. ;; (setq ad-return-value
  1789. ;; (org-propertize (org-e-odt-encode-plain-text (ad-get-arg 0))
  1790. ;; 'org-protected t))))
  1791. (defun org-e-odt-format-latex (latex-frag processing-type info)
  1792. (let* ((prefix (case processing-type
  1793. (dvipng "ltxpng/")
  1794. (mathml "ltxmathml/")))
  1795. (input-file (plist-get info :input-file))
  1796. (cache-subdir
  1797. (concat prefix (file-name-sans-extension
  1798. (file-name-nondirectory input-file))))
  1799. (cache-dir (file-name-directory input-file))
  1800. (display-msg (case processing-type
  1801. (dvipng "Creating LaTeX Image...")
  1802. (mathml "Creating MathML snippet..."))))
  1803. (with-temp-buffer
  1804. (insert latex-frag)
  1805. (org-format-latex cache-subdir cache-dir nil display-msg
  1806. nil nil processing-type)
  1807. (buffer-string))))
  1808. (defun org-e-odt-latex-environment (latex-environment contents info)
  1809. "Transcode a LATEX-ENVIRONMENT element from Org to ODT.
  1810. CONTENTS is nil. INFO is a plist holding contextual information."
  1811. (org-e-odt--wrap-label
  1812. latex-environment
  1813. (let* ((latex-frag
  1814. (org-remove-indentation
  1815. (org-element-property :value latex-environment)))
  1816. (processing-type (plist-get info :LaTeX-fragments))
  1817. (caption (org-element-property :caption latex-environment))
  1818. (short-caption (and (cdr caption)
  1819. (org-export-data (cdr caption) info)))
  1820. (caption (and (car caption) (org-export-data (car caption) info)))
  1821. (label (org-element-property :name latex-environment))
  1822. (attr nil) ; FIXME
  1823. (label (org-element-property :name latex-environment)))
  1824. (when (memq processing-type '(t mathjax))
  1825. (unless (and (fboundp 'org-format-latex-mathml-available-p)
  1826. (org-format-latex-mathml-available-p))
  1827. (message "LaTeX to MathML converter not available. Trying dvinpng...")
  1828. (setq processing-type 'dvipng)))
  1829. (when (eq processing-type 'dvipng)
  1830. (unless (and (org-check-external-command "latex" "" t)
  1831. (org-check-external-command "dvipng" "" t))
  1832. (message "LaTeX to PNG converter not available. Using verbatim.")
  1833. (setq processing-type 'verbatim)))
  1834. (case processing-type
  1835. ((t mathjax)
  1836. (org-e-odt-format-formula latex-environment info))
  1837. (dvipng
  1838. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  1839. "Text_20_body"
  1840. (org-e-odt-link--inline-image latex-environment info)))
  1841. (t (org-e-odt-do-format-code latex-frag))))))
  1842. ;;;; Latex Fragment
  1843. ;; (when latex-frag ; FIXME
  1844. ;; (setq href (org-propertize href :title "LaTeX Fragment"
  1845. ;; :description latex-frag)))
  1846. ;; handle verbatim
  1847. ;; provide descriptions
  1848. (defun org-e-odt-latex-fragment (latex-fragment contents info)
  1849. "Transcode a LATEX-FRAGMENT object from Org to ODT.
  1850. CONTENTS is nil. INFO is a plist holding contextual information."
  1851. (let* ((latex-frag (org-element-property :value latex-fragment))
  1852. (processing-type (plist-get info :LaTeX-fragments)))
  1853. (cond
  1854. ((member processing-type '(t mathjax))
  1855. (org-e-odt-format-formula latex-fragment info))
  1856. ((eq processing-type 'dvipng)
  1857. (org-e-odt-link--inline-image latex-fragment info))
  1858. (t (org-e-odt-encode-plain-text latex-frag t)))))
  1859. ;;;; Line Break
  1860. (defun org-e-odt-line-break (line-break contents info)
  1861. "Transcode a LINE-BREAK object from Org to ODT.
  1862. CONTENTS is nil. INFO is a plist holding contextual information."
  1863. "<text:line-break/>\n")
  1864. ;;;; Link
  1865. ;;;; Links :: Generic
  1866. ;; (org-lparse-link-description-is-image
  1867. ;; (format "\n<draw:a xlink:type=\"simple\" xlink:href=\"%s\">\n%s\n</draw:a>"
  1868. ;; href desc))
  1869. ;;;; Links :: Label references
  1870. (defun org-e-odt-enumerate-element (element info &optional predicate n)
  1871. (let* ((--numbered-parent-headline-at-<=-n
  1872. (function
  1873. (lambda (element n info)
  1874. (loop for x in (org-export-get-genealogy element)
  1875. thereis (and (eq (org-element-type x) 'headline)
  1876. (<= (org-export-get-relative-level x info) n)
  1877. (org-export-numbered-headline-p x info)
  1878. x)))))
  1879. (--enumerate
  1880. (function
  1881. (lambda (element scope info &optional predicate)
  1882. (let ((counter 0))
  1883. (org-element-map
  1884. (or scope (plist-get info :parse-tree))
  1885. (org-element-type element)
  1886. (lambda (el)
  1887. (and (or (not predicate) (funcall predicate el info))
  1888. (incf counter)
  1889. (eq element el)
  1890. counter))
  1891. info 'first-match)))))
  1892. (scope (funcall --numbered-parent-headline-at-<=-n
  1893. element (or n org-e-odt-display-outline-level) info))
  1894. (ordinal (funcall --enumerate element scope info predicate))
  1895. (tag
  1896. (concat
  1897. ;; Section number.
  1898. (and scope
  1899. (mapconcat 'number-to-string
  1900. (org-export-get-headline-number scope info) "."))
  1901. ;; Separator.
  1902. (and scope ".")
  1903. ;; Ordinal.
  1904. (number-to-string ordinal))))
  1905. tag))
  1906. (defun org-e-odt-format-label (element info op)
  1907. (let* ((caption-from
  1908. (case (org-element-type element)
  1909. (link (org-export-get-parent-element element))
  1910. (t element)))
  1911. ;; get label and caption.
  1912. (label (org-element-property :name caption-from))
  1913. (caption (org-element-property :caption caption-from))
  1914. (short-caption (cdr caption))
  1915. ;; transcode captions.
  1916. (caption (and (car caption) (org-export-data (car caption) info)))
  1917. (short-caption (and short-caption
  1918. (org-export-data short-caption info))))
  1919. (when (or label caption)
  1920. (let* ((default-category
  1921. (cond
  1922. ((eq (org-element-type element) 'table)
  1923. "__Table__")
  1924. ((org-e-odt-standalone-image-p element info)
  1925. "__Figure__")
  1926. ((member (org-element-type element)
  1927. '(latex-environment latex-fragment))
  1928. (let ((processing-type (plist-get info :LaTeX-fragments)))
  1929. (cond
  1930. ((eq processing-type 'dvipng) "__DvipngImage__")
  1931. ((eq processing-type 'mathjax) "__MathFormula__")
  1932. ((eq processing-type 't) "__MathFormula__")
  1933. (t (error "Handle LaTeX:verbatim")))))
  1934. ((eq (org-element-type element) 'src-block)
  1935. "__Listing__")
  1936. (t (error "Handle enumeration of %S" element))))
  1937. (predicate
  1938. (cond
  1939. ((member (org-element-type element)
  1940. '(table latex-environment src-block))
  1941. nil)
  1942. ((org-e-odt-standalone-image-p element info)
  1943. 'org-e-odt-standalone-image-p)
  1944. (t (error "Handle enumeration of %S" element))))
  1945. (seqno (org-e-odt-enumerate-element
  1946. element info predicate)) ; FIXME
  1947. ;; handle label props.
  1948. (label-props (assoc default-category org-e-odt-category-map-alist))
  1949. ;; identify opendocument counter
  1950. (counter (nth 1 label-props))
  1951. ;; identify label style
  1952. (label-style (nth 2 label-props))
  1953. ;; retrieve localized category sting
  1954. (category (org-export-translate (nth 3 label-props) :utf-8 info)))
  1955. (case op
  1956. (definition
  1957. ;; assign an internal label, if user has not provided one
  1958. (setq label (or label (format "%s-%s" default-category seqno)))
  1959. (setq label (org-export-solidify-link-text label))
  1960. (cons
  1961. (format-spec
  1962. (cadr (assoc-string label-style org-e-odt-label-styles t))
  1963. `((?e . ,category)
  1964. (?n . ,(format
  1965. "<text:sequence text:ref-name=\"%s\" text:name=\"%s\" text:formula=\"ooow:%s+1\" style:num-format=\"1\">%s</text:sequence>"
  1966. label counter counter seqno))
  1967. (?c . ,(or caption ""))))
  1968. short-caption))
  1969. (reference
  1970. (assert label)
  1971. (setq label (org-export-solidify-link-text label))
  1972. (let* ((fmt (cddr (assoc-string label-style org-e-odt-label-styles t)))
  1973. (fmt1 (car fmt))
  1974. (fmt2 (cadr fmt)))
  1975. (format "<text:sequence-ref text:reference-format=\"%s\" text:ref-name=\"%s\">%s</text:sequence-ref>"
  1976. fmt1 label (format-spec fmt2 `((?e . ,category)
  1977. (?n . ,seqno))))))
  1978. (t (error "Unknow %S on label" op)))))))
  1979. ;;;; Links :: Math formula
  1980. (defun org-e-odt-format-formula (element info)
  1981. (let* ((src (cond
  1982. ((eq (org-element-type element) 'link) ; FIXME
  1983. (let* ((type (org-element-property :type element))
  1984. (raw-path (org-element-property :path element)))
  1985. (cond
  1986. ((file-name-absolute-p raw-path)
  1987. (expand-file-name raw-path))
  1988. (t raw-path))))
  1989. ((member (org-element-type element)
  1990. '(latex-fragment latex-environment))
  1991. (let* ((latex-frag (org-remove-indentation
  1992. (org-element-property :value element)))
  1993. (formula-link (org-e-odt-format-latex
  1994. latex-frag 'mathml info)))
  1995. (and formula-link
  1996. (string-match "file:\\([^]]*\\)" formula-link)
  1997. (match-string 1 formula-link))))
  1998. (t (error "what is this?"))))
  1999. (full-src (if (file-name-absolute-p src) src
  2000. (expand-file-name src (file-name-directory
  2001. (plist-get info :input-file)))))
  2002. (caption-from
  2003. (case (org-element-type element)
  2004. (link (org-export-get-parent-element element))
  2005. (t element)))
  2006. (captions (org-e-odt-format-label caption-from info 'definition))
  2007. (caption (car captions))
  2008. (href
  2009. (format "\n<draw:object %s xlink:href=\"%s\" xlink:type=\"simple\"/>"
  2010. " xlink:show=\"embed\" xlink:actuate=\"onLoad\""
  2011. (file-name-directory (org-e-odt-copy-formula-file full-src))))
  2012. (embed-as (if caption 'paragraph 'character))
  2013. width height)
  2014. (cond
  2015. ((eq embed-as 'character)
  2016. (org-e-odt-format-entity "InlineFormula" href width height))
  2017. (t
  2018. (let* ((equation (org-e-odt-format-entity
  2019. "CaptionedDisplayFormula" href width height captions))
  2020. (label
  2021. (let* ((org-e-odt-category-map-alist
  2022. '(("__Table__" "Table" "value")
  2023. ("__Figure__" "Illustration" "value")
  2024. ("__MathFormula__" "Text" "math-label")
  2025. ("__DvipngImage__" "Equation" "value")
  2026. ("__Listing__" "Listing" "value"))))
  2027. (car (org-e-odt-format-label caption-from info 'definition))))
  2028. (formula-tree
  2029. (org-element-adopt-elements
  2030. (list 'table '(:type org :attr_odt (":style \"OrgEquation\"")))
  2031. (org-element-adopt-elements
  2032. (list 'table-row '(:type standard))
  2033. (list 'table-cell nil "<c8>") (list 'table-cell nil "<c1>"))
  2034. (org-element-adopt-elements
  2035. (list 'table-row '(:type standard))
  2036. (org-element-adopt-elements
  2037. (list 'table-cell nil)
  2038. (list 'export-block (list :type "ODT" :value equation)))
  2039. (org-element-adopt-elements
  2040. (list 'table-cell nil)
  2041. (list 'export-block (list :type "ODT" :value label))))))
  2042. (formula-info
  2043. (org-export-collect-tree-properties
  2044. formula-tree (org-export-get-environment 'e-odt))))
  2045. (org-export-data formula-tree formula-info))))))
  2046. (defun org-e-odt-copy-formula-file (src-file)
  2047. "Returns the internal name of the file"
  2048. (let* ((target-dir (format "Formula-%04d/"
  2049. (incf org-e-odt-embedded-formulas-count)))
  2050. (target-file (concat target-dir "content.xml")))
  2051. ;; Create a directory for holding formula file. Also enter it in
  2052. ;; to manifest.
  2053. (make-directory (concat org-e-odt-zip-dir target-dir))
  2054. (org-e-odt-create-manifest-file-entry
  2055. "application/vnd.oasis.opendocument.formula" target-dir "1.2")
  2056. ;; Copy over the formula file from user directory to zip
  2057. ;; directory.
  2058. (message "Embedding %s as %s ..." src-file target-file)
  2059. (let ((case-fold-search nil))
  2060. (cond
  2061. ;; Case 1: Mathml.
  2062. ((string-match "\\.\\(mathml\\|mml\\)\\'" src-file)
  2063. (copy-file src-file (concat org-e-odt-zip-dir target-file) 'overwrite))
  2064. ;; Case 2: OpenDocument formula.
  2065. ((string-match "\\.odf\\'" src-file)
  2066. (org-e-odt--zip-extract src-file "content.xml"
  2067. (concat org-e-odt-zip-dir target-dir)))
  2068. (t (error "%s is not a formula file" src-file))))
  2069. ;; Enter the formula file in to manifest.
  2070. (org-e-odt-create-manifest-file-entry "text/xml" target-file)
  2071. target-file))
  2072. ;;;; Targets
  2073. (defun org-e-odt-format-entity (entity href width height &optional
  2074. captions user-frame-params)
  2075. (let* ((caption (car captions)) (short-caption (cdr captions))
  2076. (entity-style (assoc-string entity org-e-odt-entity-frame-styles t))
  2077. default-frame-params frame-params
  2078. (--merge-frame-params
  2079. (function
  2080. (lambda (default-frame-params user-frame-params)
  2081. (if (not user-frame-params) default-frame-params
  2082. (assert (= (length default-frame-params) 3))
  2083. (assert (= (length user-frame-params) 3))
  2084. (loop for user-frame-param in user-frame-params
  2085. for default-frame-param in default-frame-params
  2086. collect (or user-frame-param default-frame-param)))))))
  2087. (cond
  2088. ((not caption)
  2089. (setq default-frame-params (nth 2 entity-style))
  2090. (setq frame-params (funcall --merge-frame-params
  2091. default-frame-params user-frame-params))
  2092. (apply 'org-e-odt--frame href width height frame-params))
  2093. (t
  2094. (setq default-frame-params (nth 3 entity-style))
  2095. (setq frame-params (funcall --merge-frame-params
  2096. default-frame-params user-frame-params))
  2097. (apply 'org-e-odt--textbox
  2098. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2099. "Illustration"
  2100. (concat
  2101. (apply 'org-e-odt--frame href width height
  2102. (let ((entity-style-1 (copy-sequence
  2103. (nth 2 entity-style))))
  2104. (setcar (cdr entity-style-1)
  2105. (concat
  2106. (cadr entity-style-1)
  2107. (and short-caption
  2108. (format " draw:name=\"%s\" "
  2109. short-caption))))
  2110. entity-style-1))
  2111. caption))
  2112. width height frame-params)))))
  2113. (defun org-e-odt-standalone-image-p (element info)
  2114. "Test if ELEMENT is a standalone image for the purpose ODT export.
  2115. INFO is a plist holding contextual information.
  2116. Return non-nil, if ELEMENT is of type paragraph and it's sole
  2117. content, save for whitespaces, is a link that qualifies as an
  2118. inline image.
  2119. Return non-nil, if ELEMENT is of type link and it's containing
  2120. paragraph has no other content save for leading and trailing
  2121. whitespaces.
  2122. Return nil, otherwise.
  2123. Bind `org-e-odt-standalone-image-predicate' to constrain
  2124. paragraph further. For example, to check for only captioned
  2125. standalone images, do the following.
  2126. \(setq org-e-odt-standalone-image-predicate
  2127. \(lambda \(paragraph\)
  2128. \(org-element-property :caption paragraph\)\)\)
  2129. "
  2130. (let ((--standalone-image-predicate
  2131. (function (lambda (paragraph)
  2132. (or (org-element-property :caption paragraph)
  2133. (org-element-property :name paragraph)))))
  2134. (paragraph (case (org-element-type element)
  2135. (paragraph element)
  2136. (link (and (org-export-inline-image-p
  2137. element org-e-odt-inline-image-rules)
  2138. (org-export-get-parent element)))
  2139. (t nil))))
  2140. (when paragraph
  2141. (assert (eq (org-element-type paragraph) 'paragraph))
  2142. (when (funcall --standalone-image-predicate paragraph)
  2143. (let ((contents (org-element-contents paragraph)))
  2144. (loop for x in contents
  2145. with inline-image-count = 0
  2146. always (cond
  2147. ((eq (org-element-type x) 'plain-text)
  2148. (not (org-string-nw-p x)))
  2149. ((eq (org-element-type x) 'link)
  2150. (when (org-export-inline-image-p
  2151. x org-e-odt-inline-image-rules)
  2152. (= (incf inline-image-count) 1)))
  2153. (t nil))))))))
  2154. (defun org-e-odt-get-previous-elements (blob info)
  2155. (let ((parent (org-export-get-parent blob)))
  2156. (cdr (memq blob (reverse (org-element-contents parent))))))
  2157. (defun org-e-odt-resolve-numbered-paragraph (element info)
  2158. (when (eq (org-element-type element) 'item)
  2159. (let ((el element) ordinal)
  2160. (while (eq (org-element-type el) 'item)
  2161. (push (1+ (length (org-e-odt-get-previous-elements el info))) ordinal)
  2162. (setq el (org-export-get-parent (org-export-get-parent el))))
  2163. ordinal)))
  2164. (defun org-e-odt-link (link desc info)
  2165. "Transcode a LINK object from Org to ODT.
  2166. DESC is the description part of the link, or the empty string.
  2167. INFO is a plist holding contextual information. See
  2168. `org-export-data'."
  2169. (let* ((type (org-element-property :type link))
  2170. (raw-path (org-element-property :path link))
  2171. ;; Ensure DESC really exists, or set it to nil.
  2172. (desc (and (not (string= desc "")) desc))
  2173. (imagep (org-export-inline-image-p
  2174. link org-e-odt-inline-image-rules))
  2175. (path (cond
  2176. ((member type '("http" "https" "ftp" "mailto"))
  2177. (concat type ":" raw-path))
  2178. ((string= type "file")
  2179. (if (file-name-absolute-p raw-path)
  2180. (concat "file://" (expand-file-name raw-path))
  2181. (concat "file://" raw-path)))
  2182. (t raw-path)))
  2183. protocol)
  2184. (cond
  2185. ;; Image file.
  2186. ((and (not desc) (org-export-inline-image-p
  2187. link org-e-odt-inline-image-rules))
  2188. (org-e-odt-link--inline-image link info))
  2189. ;; Radio target: Transcode target's contents and use them as
  2190. ;; link's description.
  2191. ((string= type "radio")
  2192. (let ((destination (org-export-resolve-radio-link link info)))
  2193. (when destination
  2194. (let ((desc (org-export-data (org-element-contents destination) info))
  2195. (href (org-export-solidify-link-text path)))
  2196. (format
  2197. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2198. href desc)))))
  2199. ;; Links pointing to an headline: Find destination and build
  2200. ;; appropriate referencing command.
  2201. ((member type '("custom-id" "fuzzy" "id"))
  2202. (let ((destination (if (string= type "fuzzy")
  2203. (org-export-resolve-fuzzy-link link info)
  2204. (org-export-resolve-id-link link info))))
  2205. (case (org-element-type destination)
  2206. ;; Fuzzy link points nowhere.
  2207. ('nil
  2208. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2209. "Emphasis" (or desc (org-export-data
  2210. (org-element-property
  2211. :raw-link link) info))))
  2212. ;; Fuzzy link points to an invisible target.
  2213. (keyword nil)
  2214. ;; LINK points to an headline. Check if LINK should display
  2215. ;; section numbers.
  2216. (headline
  2217. (let* ((headline-no (org-export-get-headline-number destination info))
  2218. (label (format "sec-%s" (mapconcat 'number-to-string
  2219. headline-no "-"))))
  2220. (cond
  2221. ;; Case 1: Headline is numbered and LINK has no
  2222. ;; description or LINK's description matches headline's
  2223. ;; title. Display section number.
  2224. ((and (org-export-numbered-headline-p destination info)
  2225. (or (not desc) (string= desc (org-element-property
  2226. :raw-value destination))))
  2227. (format
  2228. "<text:bookmark-ref text:reference-format=\"chapter\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2229. label (mapconcat 'number-to-string headline-no ".")))
  2230. ;; Case 2: Either the headline is un-numbered or
  2231. ;; LINK has a custom description. Display LINK's
  2232. ;; description or headline's title.
  2233. (t
  2234. (let ((desc (or desc (org-export-data
  2235. (org-element-property :title destination)
  2236. info))))
  2237. (format
  2238. "<text:bookmark-ref text:reference-format=\"text\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2239. label desc))))))
  2240. ;; Fuzzy link points to a target. Do as above.
  2241. (target
  2242. ;; Identify nearest meaningful container
  2243. (let ((container
  2244. (loop for parent in (org-export-get-genealogy destination)
  2245. when
  2246. (memq
  2247. (org-element-type parent)
  2248. '(footnote-definition footnote-reference headline item
  2249. table))
  2250. return parent)))
  2251. ;; There is a meaningful container
  2252. (when container
  2253. (case (org-element-type container)
  2254. ;; Container is item
  2255. (item
  2256. (format
  2257. "<text:bookmark-ref text:reference-format=\"number-all-superior\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2258. (org-solidify-link-text path)
  2259. (mapconcat 'number-to-string
  2260. (org-e-odt-resolve-numbered-paragraph
  2261. container info) ".")))))))
  2262. (otherwise
  2263. ;; (unless desc
  2264. ;; (setq number (cond
  2265. ;; ((org-e-odt-standalone-image-p destination info)
  2266. ;; (org-export-get-ordinal
  2267. ;; (assoc 'link (org-element-contents destination))
  2268. ;; info 'link 'org-e-odt-standalone-image-p))
  2269. ;; (t (org-export-get-ordinal destination info))))
  2270. ;; (setq desc (when number
  2271. ;; (if (atom number) (number-to-string number)
  2272. ;; (mapconcat 'number-to-string number ".")))))
  2273. (let ((label-reference
  2274. (org-e-odt-format-label destination info 'reference)))
  2275. (assert label-reference)
  2276. label-reference)))))
  2277. ;; Coderef: replace link with the reference name or the
  2278. ;; equivalent line number.
  2279. ((string= type "coderef")
  2280. (let* ((line-no (format "%d" (org-export-resolve-coderef path info)))
  2281. (href (concat "coderef-" path)))
  2282. (format
  2283. (org-export-get-coderef-format path desc)
  2284. (format
  2285. "<text:bookmark-ref text:reference-format=\"number\" text:ref-name=\"OrgXref.%s\">%s</text:bookmark-ref>"
  2286. href line-no))))
  2287. ;; Link type is handled by a special function.
  2288. ((functionp (setq protocol (nth 2 (assoc type org-link-protocols))))
  2289. (funcall protocol (org-link-unescape path) desc 'odt))
  2290. ;; External link with a description part.
  2291. ((and path desc)
  2292. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2293. path desc))
  2294. ;; External link without a description part.
  2295. (path
  2296. (format "<text:a xlink:type=\"simple\" xlink:href=\"%s\">%s</text:a>"
  2297. path path))
  2298. ;; No path, only description. Try to do something useful.
  2299. (t (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2300. "Emphasis" desc)))))
  2301. ;;;; Paragraph
  2302. (defun org-e-odt-paragraph (paragraph contents info)
  2303. "Transcode a PARAGRAPH element from Org to ODT.
  2304. CONTENTS is the contents of the paragraph, as a string. INFO is
  2305. the plist used as a communication channel."
  2306. (let* ((parent (org-export-get-parent paragraph))
  2307. (parent-type (org-element-type parent))
  2308. (style (case parent-type
  2309. (quote-block "Quotations")
  2310. (center-block "OrgCenter")
  2311. (footnote-definition "Footnote")
  2312. (t (or (org-element-property :style paragraph)
  2313. "Text_20_body")))))
  2314. ;; If this paragraph is a leading paragraph in an item and the
  2315. ;; item has a checkbox, splice the checkbox and paragraph contents
  2316. ;; together.
  2317. (when (and (eq (org-element-type parent) 'item)
  2318. (eq paragraph (car (org-element-contents parent))))
  2319. (setq contents (concat (org-e-odt--checkbox parent) contents)))
  2320. (assert style)
  2321. (format "\n<text:p text:style-name=\"%s\">%s</text:p>" style contents)))
  2322. ;;;; Plain List
  2323. (defun org-e-odt-plain-list (plain-list contents info)
  2324. "Transcode a PLAIN-LIST element from Org to ODT.
  2325. CONTENTS is the contents of the list. INFO is a plist holding
  2326. contextual information."
  2327. (org-e-odt--wrap-label
  2328. plain-list
  2329. (format "\n<text:list text:style-name=\"%s\" %s>\n%s</text:list>"
  2330. ;; Choose style based on list type.
  2331. (case (org-element-property :type plain-list)
  2332. (ordered "OrgNumberedList")
  2333. (unordered "OrgBulletedList")
  2334. (descriptive-1 "OrgDescriptionList")
  2335. (descriptive-2 "OrgDescriptionList"))
  2336. ;; If top-level list, re-start numbering. Otherwise,
  2337. ;; continue numbering.
  2338. (format "text:continue-numbering=\"%s\""
  2339. (let* ((parent (org-export-get-parent plain-list)))
  2340. (if (and parent (eq (org-element-type parent) 'item))
  2341. "true" "false")))
  2342. contents)))
  2343. ;;;; Plain Text
  2344. (defun org-e-odt-fill-tabs-and-spaces (line)
  2345. (replace-regexp-in-string
  2346. "\\([\t]\\|\\([ ]+\\)\\)"
  2347. (lambda (s)
  2348. (cond
  2349. ((string= s "\t") "<text:tab/>")
  2350. (t (let ((n (length s)))
  2351. (cond
  2352. ((= n 1) " ")
  2353. ((> n 1) (concat " " (format "<text:s text:c=\"%d\"/>" (1- n))))
  2354. (t ""))))))
  2355. line))
  2356. (defun org-e-odt-encode-plain-text (text &optional no-whitespace-filling)
  2357. (mapc
  2358. (lambda (pair)
  2359. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t t)))
  2360. '(("&" . "&amp;") ("<" . "&lt;") (">" . "&gt;")))
  2361. (if no-whitespace-filling text
  2362. (org-e-odt-fill-tabs-and-spaces text)))
  2363. (defun org-e-odt--quotation-marks (text info)
  2364. "Export quotation marks depending on language conventions.
  2365. TEXT is a string containing quotation marks to be replaced. INFO
  2366. is a plist used as a communication channel."
  2367. (mapc (lambda(l)
  2368. (let ((start 0))
  2369. (while (setq start (string-match (car l) text start))
  2370. (let ((new-quote (concat (match-string 1 text) (cdr l))))
  2371. (setq text (replace-match new-quote t t text))))))
  2372. (cdr (or (assoc (plist-get info :language) org-e-odt-quotes)
  2373. ;; Falls back on English.
  2374. (assoc "en" org-e-odt-quotes))))
  2375. text)
  2376. (defun org-e-odt-plain-text (text info)
  2377. "Transcode a TEXT string from Org to ODT.
  2378. TEXT is the string to transcode. INFO is a plist holding
  2379. contextual information."
  2380. ;; Protect &, < and >.
  2381. (setq text (org-e-odt-encode-plain-text text t))
  2382. ;; Handle quotation marks
  2383. (setq text (org-e-odt--quotation-marks text info))
  2384. ;; Convert special strings.
  2385. (when (plist-get info :with-special-strings)
  2386. (mapc
  2387. (lambda (pair)
  2388. (setq text (replace-regexp-in-string (car pair) (cdr pair) text t nil)))
  2389. org-e-odt-special-string-regexps))
  2390. ;; Handle break preservation if required.
  2391. (when (plist-get info :preserve-breaks)
  2392. (setq text (replace-regexp-in-string
  2393. "\\(\\\\\\\\\\)?[ \t]*\n" "<text:line-break/>\n" text t)))
  2394. ;; Return value.
  2395. text)
  2396. ;;;; Planning
  2397. (defun org-e-odt-planning (planning contents info)
  2398. "Transcode a PLANNING element from Org to ODT.
  2399. CONTENTS is nil. INFO is a plist used as a communication
  2400. channel."
  2401. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2402. "OrgTimestampWrapper"
  2403. (concat
  2404. (let ((closed (org-element-property :closed planning)))
  2405. (when closed
  2406. (concat
  2407. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2408. "OrgTimestampKeyword" org-closed-string)
  2409. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2410. "OrgTimestamp" (org-translate-time closed)))))
  2411. (let ((deadline (org-element-property :deadline planning)))
  2412. (when deadline
  2413. (concat
  2414. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2415. "OrgTimestampKeyword" org-deadline-string)
  2416. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2417. "OrgTimestamp" (org-translate-time deadline)))))
  2418. (let ((scheduled (org-element-property :scheduled planning)))
  2419. (when scheduled
  2420. (concat
  2421. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2422. "OrgTimestampKeyword" org-scheduled-string)
  2423. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2424. "OrgTimestamp" (org-translate-time scheduled))))))))
  2425. ;;;; Property Drawer
  2426. (defun org-e-odt-property-drawer (property-drawer contents info)
  2427. "Transcode a PROPERTY-DRAWER element from Org to ODT.
  2428. CONTENTS is nil. INFO is a plist holding contextual
  2429. information."
  2430. ;; The property drawer isn't exported but we want separating blank
  2431. ;; lines nonetheless.
  2432. "")
  2433. ;;;; Quote Block
  2434. (defun org-e-odt-quote-block (quote-block contents info)
  2435. "Transcode a QUOTE-BLOCK element from Org to ODT.
  2436. CONTENTS holds the contents of the block. INFO is a plist
  2437. holding contextual information."
  2438. (org-e-odt--wrap-label quote-block contents))
  2439. ;;;; Quote Section
  2440. (defun org-e-odt-quote-section (quote-section contents info)
  2441. "Transcode a QUOTE-SECTION element from Org to ODT.
  2442. CONTENTS is nil. INFO is a plist holding contextual information."
  2443. (let ((value (org-remove-indentation
  2444. (org-element-property :value quote-section))))
  2445. (when value (org-e-odt-do-format-code value))))
  2446. ;;;; Section
  2447. (defun org-e-odt-format-section (text style &optional name)
  2448. (let ((default-name (car (org-e-odt-add-automatic-style "Section"))))
  2449. (format "\n<text:section text:style-name=\"%s\" %s>\n%s\n</text:section>"
  2450. style
  2451. (format "text:name=\"%s\"" (or name default-name))
  2452. text)))
  2453. (defun org-e-odt-section (section contents info) ; FIXME
  2454. "Transcode a SECTION element from Org to ODT.
  2455. CONTENTS holds the contents of the section. INFO is a plist
  2456. holding contextual information."
  2457. contents)
  2458. ;;;; Radio Target
  2459. (defun org-e-odt-radio-target (radio-target text info)
  2460. "Transcode a RADIO-TARGET object from Org to ODT.
  2461. TEXT is the text of the target. INFO is a plist holding
  2462. contextual information."
  2463. (org-e-odt--target
  2464. text (org-export-solidify-link-text
  2465. (org-element-property :value radio-target))))
  2466. ;;;; Special Block
  2467. (defun org-e-odt-special-block (special-block contents info)
  2468. "Transcode a SPECIAL-BLOCK element from Org to ODT.
  2469. CONTENTS holds the contents of the block. INFO is a plist
  2470. holding contextual information."
  2471. (let ((type (downcase (org-element-property :type special-block)))
  2472. (attributes (org-export-read-attribute :attr_odt special-block)))
  2473. (org-e-odt--wrap-label
  2474. special-block
  2475. (cond
  2476. ;; Annotation.
  2477. ((string= type "annotation")
  2478. (let ((author (or (plist-get attributes :author)
  2479. (let ((author (plist-get info :author)))
  2480. (and author (org-export-data author info)))))
  2481. (date (or (plist-get attributes :date)
  2482. (plist-get info :date))))
  2483. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2484. "Text_20_body"
  2485. (format "<office:annotation>\n%s\n</office:annotation>"
  2486. (concat
  2487. (and author
  2488. (format "<dc:creator>%s</dc:creator>" author))
  2489. (and date
  2490. (format "<dc:date>%s</dc:date>"
  2491. (org-e-odt--date date)))
  2492. contents)))))
  2493. ;; Textbox.
  2494. ((string= type "textbox")
  2495. (let ((width (plist-get attributes :width))
  2496. (height (plist-get attributes :height))
  2497. (style (plist-get attributes :style))
  2498. (extra (plist-get attributes :extra))
  2499. (anchor (plist-get attributes :anchor)))
  2500. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2501. "Text_20_body" (org-e-odt--textbox contents width height
  2502. style extra anchor))))
  2503. (t contents)))))
  2504. ;;;; Src Block
  2505. (defun org-e-odt-hfy-face-to-css (fn)
  2506. "Create custom style for face FN.
  2507. When FN is the default face, use it's foreground and background
  2508. properties to create \"OrgSrcBlock\" paragraph style. Otherwise
  2509. use it's color attribute to create a character style whose name
  2510. is obtained from FN. Currently all attributes of FN other than
  2511. color are ignored.
  2512. The style name for a face FN is derived using the following
  2513. operations on the face name in that order - de-dash, CamelCase
  2514. and prefix with \"OrgSrc\". For example,
  2515. `font-lock-function-name-face' is associated with
  2516. \"OrgSrcFontLockFunctionNameFace\"."
  2517. (let* ((css-list (hfy-face-to-style fn))
  2518. (style-name ((lambda (fn)
  2519. (concat "OrgSrc"
  2520. (mapconcat
  2521. 'capitalize (split-string
  2522. (hfy-face-or-def-to-name fn) "-")
  2523. ""))) fn))
  2524. (color-val (cdr (assoc "color" css-list)))
  2525. (background-color-val (cdr (assoc "background" css-list)))
  2526. (style (and org-e-odt-create-custom-styles-for-srcblocks
  2527. (cond
  2528. ((eq fn 'default)
  2529. (format org-e-odt-src-block-paragraph-format
  2530. background-color-val color-val))
  2531. (t
  2532. (format
  2533. "
  2534. <style:style style:name=\"%s\" style:family=\"text\">
  2535. <style:text-properties fo:color=\"%s\"/>
  2536. </style:style>" style-name color-val))))))
  2537. (cons style-name style)))
  2538. (defun org-e-odt-htmlfontify-string (line)
  2539. (let* ((hfy-html-quote-regex "\\([<\"&> ]\\)")
  2540. (hfy-html-quote-map '(("\"" "&quot;")
  2541. ("<" "&lt;")
  2542. ("&" "&amp;")
  2543. (">" "&gt;")
  2544. (" " "<text:s/>")
  2545. (" " "<text:tab/>")))
  2546. (hfy-face-to-css 'org-e-odt-hfy-face-to-css)
  2547. (hfy-optimisations-1 (copy-sequence hfy-optimisations))
  2548. (hfy-optimisations (add-to-list 'hfy-optimisations-1
  2549. 'body-text-only))
  2550. (hfy-begin-span-handler
  2551. (lambda (style text-block text-id text-begins-block-p)
  2552. (insert (format "<text:span text:style-name=\"%s\">" style))))
  2553. (hfy-end-span-handler (lambda nil (insert "</text:span>"))))
  2554. (with-no-warnings (htmlfontify-string line))))
  2555. (defun org-e-odt-do-format-code
  2556. (code &optional lang refs retain-labels num-start)
  2557. (let* ((lang (or (assoc-default lang org-src-lang-modes) lang))
  2558. (lang-mode (and lang (intern (format "%s-mode" lang))))
  2559. (code-lines (org-split-string code "\n"))
  2560. (code-length (length code-lines))
  2561. (use-htmlfontify-p (and (functionp lang-mode)
  2562. org-e-odt-fontify-srcblocks
  2563. (require 'htmlfontify nil t)
  2564. (fboundp 'htmlfontify-string)))
  2565. (code (if (not use-htmlfontify-p) code
  2566. (with-temp-buffer
  2567. (insert code)
  2568. (funcall lang-mode)
  2569. (font-lock-fontify-buffer)
  2570. (buffer-string))))
  2571. (fontifier (if use-htmlfontify-p 'org-e-odt-htmlfontify-string
  2572. 'org-e-odt-encode-plain-text))
  2573. (par-style (if use-htmlfontify-p "OrgSrcBlock"
  2574. "OrgFixedWidthBlock"))
  2575. (i 0))
  2576. (assert (= code-length (length (org-split-string code "\n"))))
  2577. (setq code
  2578. (org-export-format-code
  2579. code
  2580. (lambda (loc line-num ref)
  2581. (setq par-style
  2582. (concat par-style (and (= (incf i) code-length) "LastLine")))
  2583. (setq loc (concat loc (and ref retain-labels (format " (%s)" ref))))
  2584. (setq loc (funcall fontifier loc))
  2585. (when ref
  2586. (setq loc (org-e-odt--target loc (concat "coderef-" ref))))
  2587. (assert par-style)
  2588. (setq loc (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2589. par-style loc))
  2590. (if (not line-num) loc
  2591. (format "\n<text:list-item>%s\n</text:list-item>" loc)))
  2592. num-start refs))
  2593. (cond
  2594. ((not num-start) code)
  2595. ((= num-start 0)
  2596. (format
  2597. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2598. " text:continue-numbering=\"false\"" code))
  2599. (t
  2600. (format
  2601. "\n<text:list text:style-name=\"OrgSrcBlockNumberedLine\"%s>%s</text:list>"
  2602. " text:continue-numbering=\"true\"" code)))))
  2603. (defun org-e-odt-format-code (element info)
  2604. (let* ((lang (org-element-property :language element))
  2605. ;; Extract code and references.
  2606. (code-info (org-export-unravel-code element))
  2607. (code (car code-info))
  2608. (refs (cdr code-info))
  2609. ;; Does the src block contain labels?
  2610. (retain-labels (org-element-property :retain-labels element))
  2611. ;; Does it have line numbers?
  2612. (num-start (case (org-element-property :number-lines element)
  2613. (continued (org-export-get-loc element info))
  2614. (new 0))))
  2615. (org-e-odt-do-format-code code lang refs retain-labels num-start)))
  2616. (defun org-e-odt-src-block (src-block contents info)
  2617. "Transcode a SRC-BLOCK element from Org to ODT.
  2618. CONTENTS holds the contents of the item. INFO is a plist holding
  2619. contextual information."
  2620. (let* ((lang (org-element-property :language src-block))
  2621. (caption (org-element-property :caption src-block))
  2622. (short-caption (and (cdr caption)
  2623. (org-export-data (cdr caption) info)))
  2624. (caption (and (car caption) (org-export-data (car caption) info)))
  2625. (label (org-element-property :name src-block))
  2626. (attributes (org-export-read-attribute :attr_odt src-block)))
  2627. ;; FIXME: Handle caption
  2628. ;; caption-str (when caption)
  2629. ;; (main (org-export-data (car caption) info))
  2630. ;; (secondary (org-export-data (cdr caption) info))
  2631. ;; (caption-str (org-e-odt--caption/label-string caption label info))
  2632. (let* ((captions (org-e-odt-format-label src-block info 'definition))
  2633. (caption (car captions)) (short-caption (cdr captions)))
  2634. (concat
  2635. (and caption
  2636. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2637. "Listing" caption))
  2638. (let ((--src-block (org-e-odt-format-code src-block info)))
  2639. (if (not (plist-get attributes :textbox)) --src-block
  2640. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2641. "Text_20_body"
  2642. (org-e-odt--textbox --src-block nil nil nil))))))))
  2643. ;;;; Statistics Cookie
  2644. (defun org-e-odt-statistics-cookie (statistics-cookie contents info)
  2645. "Transcode a STATISTICS-COOKIE object from Org to ODT.
  2646. CONTENTS is nil. INFO is a plist holding contextual information."
  2647. (let ((cookie-value (org-element-property :value statistics-cookie)))
  2648. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2649. "OrgCode" cookie-value)))
  2650. ;;;; Strike-Through
  2651. (defun org-e-odt-strike-through (strike-through contents info)
  2652. "Transcode STRIKE-THROUGH from Org to ODT.
  2653. CONTENTS is the text with strike-through markup. INFO is a plist
  2654. holding contextual information."
  2655. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2656. "Strikethrough" contents))
  2657. ;;;; Subscript
  2658. (defun org-e-odt-subscript (subscript contents info)
  2659. "Transcode a SUBSCRIPT object from Org to ODT.
  2660. CONTENTS is the contents of the object. INFO is a plist holding
  2661. contextual information."
  2662. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2663. "OrgSubscript" contents))
  2664. ;;;; Superscript
  2665. (defun org-e-odt-superscript (superscript contents info)
  2666. "Transcode a SUPERSCRIPT object from Org to ODT.
  2667. CONTENTS is the contents of the object. INFO is a plist holding
  2668. contextual information."
  2669. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  2670. "OrgSuperscript" contents))
  2671. ;;;; Table Cell
  2672. (defun org-e-odt-table-style-spec (element info)
  2673. (let* ((table (org-export-get-parent-table element))
  2674. (table-attributes (org-export-read-attribute :attr_odt table))
  2675. (table-style (plist-get table-attributes :style)))
  2676. (assoc table-style org-e-odt-table-styles)))
  2677. (defun org-e-odt-get-table-cell-styles (table-cell info)
  2678. "Retrieve styles applicable to a table cell.
  2679. R and C are (zero-based) row and column numbers of the table
  2680. cell. STYLE-SPEC is an entry in `org-e-odt-table-styles'
  2681. applicable to the current table. It is `nil' if the table is not
  2682. associated with any style attributes.
  2683. Return a cons of (TABLE-CELL-STYLE-NAME . PARAGRAPH-STYLE-NAME).
  2684. When STYLE-SPEC is nil, style the table cell the conventional way
  2685. - choose cell borders based on row and column groupings and
  2686. choose paragraph alignment based on `org-col-cookies' text
  2687. property. See also
  2688. `org-e-odt-get-paragraph-style-cookie-for-table-cell'.
  2689. When STYLE-SPEC is non-nil, ignore the above cookie and return
  2690. styles congruent with the ODF-1.2 specification."
  2691. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2692. (r (car table-cell-address)) (c (cdr table-cell-address))
  2693. (style-spec (org-e-odt-table-style-spec table-cell info))
  2694. (table-dimensions (org-export-table-dimensions
  2695. (org-export-get-parent-table table-cell)
  2696. info)))
  2697. (when style-spec
  2698. ;; LibreOffice - particularly the Writer - honors neither table
  2699. ;; templates nor custom table-cell styles. Inorder to retain
  2700. ;; inter-operability with LibreOffice, only automatic styles are
  2701. ;; used for styling of table-cells. The current implementation is
  2702. ;; congruent with ODF-1.2 specification and hence is
  2703. ;; future-compatible.
  2704. ;; Additional Note: LibreOffice's AutoFormat facility for tables -
  2705. ;; which recognizes as many as 16 different cell types - is much
  2706. ;; richer. Unfortunately it is NOT amenable to easy configuration
  2707. ;; by hand.
  2708. (let* ((template-name (nth 1 style-spec))
  2709. (cell-style-selectors (nth 2 style-spec))
  2710. (cell-type
  2711. (cond
  2712. ((and (cdr (assoc 'use-first-column-styles cell-style-selectors))
  2713. (= c 0)) "FirstColumn")
  2714. ((and (cdr (assoc 'use-last-column-styles cell-style-selectors))
  2715. (= (1+ c) (cdr table-dimensions)))
  2716. "LastColumn")
  2717. ((and (cdr (assoc 'use-first-row-styles cell-style-selectors))
  2718. (= r 0)) "FirstRow")
  2719. ((and (cdr (assoc 'use-last-row-styles cell-style-selectors))
  2720. (= (1+ r) (car table-dimensions)))
  2721. "LastRow")
  2722. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2723. (= (% r 2) 1)) "EvenRow")
  2724. ((and (cdr (assoc 'use-banding-rows-styles cell-style-selectors))
  2725. (= (% r 2) 0)) "OddRow")
  2726. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2727. (= (% c 2) 1)) "EvenColumn")
  2728. ((and (cdr (assoc 'use-banding-columns-styles cell-style-selectors))
  2729. (= (% c 2) 0)) "OddColumn")
  2730. (t ""))))
  2731. (concat template-name cell-type)))))
  2732. (defun org-e-odt-table-cell (table-cell contents info)
  2733. "Transcode a TABLE-CELL element from Org to ODT.
  2734. CONTENTS is nil. INFO is a plist used as a communication
  2735. channel."
  2736. (let* ((table-cell-address (org-export-table-cell-address table-cell info))
  2737. (r (car table-cell-address))
  2738. (c (cdr table-cell-address))
  2739. (horiz-span (or (org-export-table-cell-width table-cell info) 0))
  2740. (table-row (org-export-get-parent table-cell))
  2741. (custom-style-prefix (org-e-odt-get-table-cell-styles
  2742. table-cell info))
  2743. (paragraph-style
  2744. (or
  2745. (and custom-style-prefix
  2746. (format "%sTableParagraph" custom-style-prefix))
  2747. (concat
  2748. (cond
  2749. ((and (= 1 (org-export-table-row-group table-row info))
  2750. (org-export-table-has-header-p
  2751. (org-export-get-parent-table table-row) info))
  2752. "OrgTableHeading")
  2753. ((let* ((table (org-export-get-parent-table table-cell))
  2754. (table-attrs (org-export-read-attribute :attr_odt table))
  2755. (table-header-columns (plist-get table-attrs
  2756. :header-columns)))
  2757. (<= c (cond ((wholenump table-header-columns)
  2758. (- table-header-columns 1))
  2759. (table-header-columns 0)
  2760. (t -1))))
  2761. "OrgTableHeading")
  2762. (t "OrgTableContents"))
  2763. (capitalize (symbol-name (org-export-table-cell-alignment
  2764. table-cell info))))))
  2765. (cell-style-name
  2766. (or
  2767. (and custom-style-prefix (format "%sTableCell"
  2768. custom-style-prefix))
  2769. (concat
  2770. "OrgTblCell"
  2771. (when (or (org-export-table-row-starts-rowgroup-p table-row info)
  2772. (zerop r)) "T")
  2773. (when (org-export-table-row-ends-rowgroup-p table-row info) "B")
  2774. (when (and (org-export-table-cell-starts-colgroup-p table-cell info)
  2775. (not (zerop c)) ) "L"))))
  2776. (cell-attributes
  2777. (concat
  2778. (format " table:style-name=\"%s\"" cell-style-name)
  2779. (and (> horiz-span 0)
  2780. (format " table:number-columns-spanned=\"%d\""
  2781. (1+ horiz-span))))))
  2782. (unless contents (setq contents ""))
  2783. (concat
  2784. (assert paragraph-style)
  2785. (format "\n<table:table-cell%s>\n%s\n</table:table-cell>"
  2786. cell-attributes
  2787. (let ((table-cell-contents (org-element-contents table-cell)))
  2788. (if (memq (org-element-type (car table-cell-contents))
  2789. org-element-all-elements)
  2790. contents
  2791. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2792. paragraph-style contents))))
  2793. (let (s)
  2794. (dotimes (i horiz-span s)
  2795. (setq s (concat s "\n<table:covered-table-cell/>"))))
  2796. "\n")))
  2797. ;;;; Table Row
  2798. (defun org-e-odt-table-row (table-row contents info)
  2799. "Transcode a TABLE-ROW element from Org to ODT.
  2800. CONTENTS is the contents of the row. INFO is a plist used as a
  2801. communication channel."
  2802. ;; Rules are ignored since table separators are deduced from
  2803. ;; borders of the current row.
  2804. (when (eq (org-element-property :type table-row) 'standard)
  2805. (let* ((rowgroup-tags
  2806. (if (and (= 1 (org-export-table-row-group table-row info))
  2807. (org-export-table-has-header-p
  2808. (org-export-get-parent-table table-row) info))
  2809. ;; If the row belongs to the first rowgroup and the
  2810. ;; table has more than one row groups, then this row
  2811. ;; belongs to the header row group.
  2812. '("\n<table:table-header-rows>" . "\n</table:table-header-rows>")
  2813. ;; Otherwise, it belongs to non-header row group.
  2814. '("\n<table:table-rows>" . "\n</table:table-rows>"))))
  2815. (concat
  2816. ;; Does this row begin a rowgroup?
  2817. (when (org-export-table-row-starts-rowgroup-p table-row info)
  2818. (car rowgroup-tags))
  2819. ;; Actual table row
  2820. (format "\n<table:table-row>\n%s\n</table:table-row>" contents)
  2821. ;; Does this row end a rowgroup?
  2822. (when (org-export-table-row-ends-rowgroup-p table-row info)
  2823. (cdr rowgroup-tags))))))
  2824. ;;;; Table
  2825. (defun org-e-odt-table-first-row-data-cells (table info)
  2826. (let ((table-row
  2827. (org-element-map
  2828. table 'table-row
  2829. (lambda (row)
  2830. (unless (eq (org-element-property :type row) 'rule) row))
  2831. info 'first-match))
  2832. (special-column-p (org-export-table-has-special-column-p table)))
  2833. (if (not special-column-p) (org-element-contents table-row)
  2834. (cdr (org-element-contents table-row)))))
  2835. (defun org-e-odt--table (table contents info)
  2836. "Transcode a TABLE element from Org to ODT.
  2837. CONTENTS is the contents of the table. INFO is a plist holding
  2838. contextual information."
  2839. (case (org-element-property :type table)
  2840. ;; Case 1: table.el doesn't support export to OD format. Strip
  2841. ;; such tables from export.
  2842. (table.el
  2843. (prog1 nil
  2844. (message
  2845. (concat
  2846. "(org-e-odt): Found table.el-type table in the source Org file."
  2847. " table.el doesn't support export to ODT format."
  2848. " Stripping the table from export."))))
  2849. ;; Case 2: Native Org tables.
  2850. (otherwise
  2851. (let* ((captions (org-e-odt-format-label table info 'definition))
  2852. (caption (car captions)) (short-caption (cdr captions))
  2853. (attributes (org-export-read-attribute :attr_odt table))
  2854. (custom-table-style (nth 1 (org-e-odt-table-style-spec table info)))
  2855. (table-column-specs
  2856. (function
  2857. (lambda (table info)
  2858. (let* ((table-style (or custom-table-style "OrgTable"))
  2859. (column-style (format "%sColumn" table-style)))
  2860. (mapconcat
  2861. (lambda (table-cell)
  2862. (let ((width (1+ (or (org-export-table-cell-width
  2863. table-cell info) 0)))
  2864. (s (format
  2865. "\n<table:table-column table:style-name=\"%s\"/>"
  2866. column-style))
  2867. out)
  2868. (dotimes (i width out) (setq out (concat s out)))))
  2869. (org-e-odt-table-first-row-data-cells table info) "\n"))))))
  2870. (concat
  2871. ;; caption.
  2872. (when caption
  2873. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  2874. "Table" caption))
  2875. ;; begin table.
  2876. (let* ((automatic-name
  2877. (org-e-odt-add-automatic-style "Table" attributes)))
  2878. (format
  2879. "\n<table:table table:name=\"%s\" table:style-name=\"%s\">"
  2880. (or short-caption (car automatic-name))
  2881. (or custom-table-style (cdr automatic-name) "OrgTable")))
  2882. ;; column specification.
  2883. (funcall table-column-specs table info)
  2884. ;; actual contents.
  2885. "\n" contents
  2886. ;; end table.
  2887. "</table:table>")))))
  2888. (defun org-e-odt-table (table contents info)
  2889. "Transcode a TABLE element from Org to ODT.
  2890. CONTENTS is the contents of the table. INFO is a plist holding
  2891. contextual information.
  2892. Use `org-e-odt--table' to typeset the table. Handle details
  2893. pertaining to indentation here."
  2894. (let* ((--get-previous-elements
  2895. (function
  2896. (lambda (blob info)
  2897. (let ((parent (org-export-get-parent blob)))
  2898. (cdr (memq blob (reverse (org-element-contents parent))))))))
  2899. (--element-preceded-by-table-p
  2900. (function
  2901. (lambda (element info)
  2902. (loop for el in (funcall --get-previous-elements element info)
  2903. thereis (eq (org-element-type el) 'table)))))
  2904. (--walk-list-genealogy-and-collect-tags
  2905. (function
  2906. (lambda (table info)
  2907. (let* ((genealogy (org-export-get-genealogy table))
  2908. (list-genealogy
  2909. (when (eq (org-element-type (car genealogy)) 'item)
  2910. (loop for el in genealogy
  2911. when (memq (org-element-type el)
  2912. '(item plain-list))
  2913. collect el)))
  2914. (llh-genealogy
  2915. (apply 'nconc
  2916. (loop for el in genealogy
  2917. when (and (eq (org-element-type el) 'headline)
  2918. (org-export-low-level-p el info))
  2919. collect
  2920. (list el
  2921. (assq 'headline
  2922. (org-element-contents
  2923. (org-export-get-parent el)))))))
  2924. parent-list)
  2925. (nconc
  2926. ;; Handle list genealogy.
  2927. (loop for el in list-genealogy collect
  2928. (case (org-element-type el)
  2929. (plain-list
  2930. (setq parent-list el)
  2931. (cons "</text:list>"
  2932. (format "\n<text:list text:style-name=\"%s\" %s>"
  2933. (case (org-element-property :type el)
  2934. (ordered "OrgNumberedList")
  2935. (unordered "OrgBulletedList")
  2936. (descriptive-1 "OrgDescriptionList")
  2937. (descriptive-2 "OrgDescriptionList"))
  2938. "text:continue-numbering=\"true\"")))
  2939. (item
  2940. (cond
  2941. ((not parent-list)
  2942. (if (funcall --element-preceded-by-table-p table info)
  2943. '("</text:list-header>" . "<text:list-header>")
  2944. '("</text:list-item>" . "<text:list-header>")))
  2945. ((funcall --element-preceded-by-table-p
  2946. parent-list info)
  2947. '("</text:list-header>" . "<text:list-header>"))
  2948. (t '("</text:list-item>" . "<text:list-item>"))))))
  2949. ;; Handle low-level headlines.
  2950. (loop for el in llh-genealogy
  2951. with step = 'item collect
  2952. (case step
  2953. (plain-list
  2954. (setq step 'item) ; Flip-flop
  2955. (setq parent-list el)
  2956. (cons "</text:list>"
  2957. (format "\n<text:list text:style-name=\"%s\" %s>"
  2958. (if (org-export-numbered-headline-p
  2959. el info)
  2960. "OrgNumberedList"
  2961. "OrgBulletedList")
  2962. "text:continue-numbering=\"true\"")))
  2963. (item
  2964. (setq step 'plain-list) ; Flip-flop
  2965. (cond
  2966. ((not parent-list)
  2967. (if (funcall --element-preceded-by-table-p table info)
  2968. '("</text:list-header>" . "<text:list-header>")
  2969. '("</text:list-item>" . "<text:list-header>")))
  2970. ((let ((section? (org-export-get-previous-element
  2971. parent-list info)))
  2972. (and section?
  2973. (eq (org-element-type section?) 'section)
  2974. (assq 'table (org-element-contents section?))))
  2975. '("</text:list-header>" . "<text:list-header>"))
  2976. (t
  2977. '("</text:list-item>" . "<text:list-item>")))))))))))
  2978. (close-open-tags (funcall --walk-list-genealogy-and-collect-tags
  2979. table info)))
  2980. ;; OpenDocument schema does not permit table to occur within a
  2981. ;; list item.
  2982. ;; One solution - the easiest and lightweight, in terms of
  2983. ;; implementation - is to put the table in an indented text box
  2984. ;; and make the text box part of the list-item. Unfortunately if
  2985. ;; the table is big and spans multiple pages, the text box could
  2986. ;; overflow. In this case, the following attribute will come
  2987. ;; handy.
  2988. ;; ,---- From OpenDocument-v1.1.pdf
  2989. ;; | 15.27.28 Overflow behavior
  2990. ;; |
  2991. ;; | For text boxes contained within text document, the
  2992. ;; | style:overflow-behavior property specifies the behavior of text
  2993. ;; | boxes where the containing text does not fit into the text
  2994. ;; | box.
  2995. ;; |
  2996. ;; | If the attribute's value is clip, the text that does not fit
  2997. ;; | into the text box is not displayed.
  2998. ;; |
  2999. ;; | If the attribute value is auto-create-new-frame, a new frame
  3000. ;; | will be created on the next page, with the same position and
  3001. ;; | dimensions of the original frame.
  3002. ;; |
  3003. ;; | If the style:overflow-behavior property's value is
  3004. ;; | auto-create-new-frame and the text box has a minimum width or
  3005. ;; | height specified, then the text box will grow until the page
  3006. ;; | bounds are reached before a new frame is created.
  3007. ;; `----
  3008. ;; Unfortunately, LibreOffice-3.4.6 doesn't honor
  3009. ;; auto-create-new-frame property and always resorts to clipping
  3010. ;; the text box. This results in table being truncated.
  3011. ;; So we solve the problem the hard (and fun) way using list
  3012. ;; continuations.
  3013. ;; The problem only becomes more interesting if you take in to
  3014. ;; account the following facts:
  3015. ;;
  3016. ;; - Description lists are simulated as plain lists.
  3017. ;; - Low-level headlines can be listified.
  3018. ;; - In Org-mode, a table can occur not only as a regular list
  3019. ;; item, but also within description lists and low-level
  3020. ;; headlines.
  3021. ;; See `org-e-odt-translate-description-lists' and
  3022. ;; `org-e-odt-translate-low-level-headlines' for how this is
  3023. ;; tackled.
  3024. (concat "\n"
  3025. ;; Discontinue the list.
  3026. (mapconcat 'car close-open-tags "\n")
  3027. ;; Put the table in an indented section.
  3028. (let* ((table (org-e-odt--table table contents info))
  3029. (level (/ (length (mapcar 'car close-open-tags)) 2))
  3030. (style (format "OrgIndentedSection-Level-%d" level)))
  3031. (when table (org-e-odt-format-section table style)))
  3032. ;; Continue the list.
  3033. (mapconcat 'cdr (nreverse close-open-tags) "\n"))))
  3034. ;;;; Target
  3035. (defun org-e-odt-target (target contents info)
  3036. "Transcode a TARGET object from Org to ODT.
  3037. CONTENTS is nil. INFO is a plist holding contextual
  3038. information."
  3039. (let ((value (org-element-property :value target)))
  3040. (org-e-odt--target "" (org-export-solidify-link-text value))))
  3041. ;;;; Timestamp
  3042. (defun org-e-odt-timestamp (timestamp contents info)
  3043. "Transcode a TIMESTAMP object from Org to ODT.
  3044. CONTENTS is nil. INFO is a plist used as a communication
  3045. channel."
  3046. (let ((timestamp-1 (org-element-property :value timestamp))
  3047. (timestamp-2 (org-element-property :range-end timestamp)))
  3048. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3049. "OrgTimestampWrapper"
  3050. (concat
  3051. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3052. "OrgTimestamp" (org-translate-time timestamp-1))
  3053. (and timestamp-2
  3054. "&#x2013;"
  3055. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3056. "OrgTimestamp" (org-translate-time timestamp-2)))))))
  3057. ;;;; Underline
  3058. (defun org-e-odt-underline (underline contents info)
  3059. "Transcode UNDERLINE from Org to ODT.
  3060. CONTENTS is the text with underline markup. INFO is a plist
  3061. holding contextual information."
  3062. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3063. "Underline" contents))
  3064. ;;;; Verbatim
  3065. (defun org-e-odt-verbatim (verbatim contents info)
  3066. "Transcode a VERBATIM object from Org to ODT.
  3067. CONTENTS is nil. INFO is a plist used as a communication
  3068. channel."
  3069. (format "<text:span text:style-name=\"%s\">%s</text:span>"
  3070. "OrgCode" (org-element-property :value verbatim)))
  3071. ;;;; Verse Block
  3072. (defun org-e-odt-verse-block (verse-block contents info)
  3073. "Transcode a VERSE-BLOCK element from Org to ODT.
  3074. CONTENTS is verse block contents. INFO is a plist holding
  3075. contextual information."
  3076. ;; Add line breaks to each line of verse.
  3077. (setq contents (replace-regexp-in-string
  3078. "\\(<text:line-break/>\\)?[ \t]*\n"
  3079. "<text:line-break/>" contents))
  3080. ;; Replace tabs and spaces.
  3081. (setq contents (org-e-odt-fill-tabs-and-spaces contents))
  3082. ;; Surround it in a verse environment.
  3083. (org-e-odt--wrap-label
  3084. verse-block
  3085. (format "\n<text:p text:style-name=\"%s\">%s</text:p>"
  3086. "OrgVerse" contents)))
  3087. ;;; Filters
  3088. ;;;; Description lists
  3089. ;; This translator is necessary to handle indented tables in a uniform
  3090. ;; manner. See comment in `org-e-odt--table'.
  3091. (defun org-e-odt--translate-description-lists (tree backend info)
  3092. ;; OpenDocument has no notion of a description list. So simulate it
  3093. ;; using plain lists. Description lists in the exported document
  3094. ;; are typeset in the same manner as they are in a typical HTML
  3095. ;; document.
  3096. ;;
  3097. ;; Specifically, a description list like this:
  3098. ;;
  3099. ;; ,----
  3100. ;; | - term-1 :: definition-1
  3101. ;; | - term-2 :: definition-2
  3102. ;; `----
  3103. ;;
  3104. ;; gets translated in to the following form:
  3105. ;;
  3106. ;; ,----
  3107. ;; | - term-1
  3108. ;; | - definition-1
  3109. ;; | - term-2
  3110. ;; | - definition-2
  3111. ;; `----
  3112. ;;
  3113. ;; Further effect is achieved by fixing the OD styles as below:
  3114. ;;
  3115. ;; 1. Set the :type property of the simulated lists to
  3116. ;; `descriptive-1' and `descriptive-2'. Map these to list-styles
  3117. ;; that has *no* bullets whatsoever.
  3118. ;;
  3119. ;; 2. The paragraph containing the definition term is styled to be
  3120. ;; in bold.
  3121. ;;
  3122. (org-element-map
  3123. tree 'plain-list
  3124. (lambda (el)
  3125. (when (equal (org-element-property :type el) 'descriptive)
  3126. (org-element-set-element
  3127. el
  3128. (apply 'org-element-adopt-elements
  3129. (list 'plain-list (list :type 'descriptive-1))
  3130. (mapcar
  3131. (lambda (item)
  3132. (org-element-adopt-elements
  3133. (list 'item (list :checkbox (org-element-property
  3134. :checkbox item)))
  3135. (list 'paragraph (list :style "Text_20_body_20_bold")
  3136. (or (org-element-property :tag item) "(no term)"))
  3137. (org-element-adopt-elements
  3138. (list 'plain-list (list :type 'descriptive-2))
  3139. (apply 'org-element-adopt-elements
  3140. (list 'item nil)
  3141. (org-element-contents item)))))
  3142. (org-element-contents el)))))
  3143. nil)
  3144. info)
  3145. tree)
  3146. ;;;; List tables
  3147. ;; Lists that are marked with attribute `:list-table' are called as
  3148. ;; list tables. They will be rendered as a table within the exported
  3149. ;; document.
  3150. ;; Consider an example. The following list table
  3151. ;;
  3152. ;; #+attr_odt :list-table t
  3153. ;; - Row 1
  3154. ;; - 1.1
  3155. ;; - 1.2
  3156. ;; - 1.3
  3157. ;; - Row 2
  3158. ;; - 2.1
  3159. ;; - 2.2
  3160. ;; - 2.3
  3161. ;;
  3162. ;; will be exported as though it were an Org table like the one show
  3163. ;; below.
  3164. ;;
  3165. ;; | Row 1 | 1.1 | 1.2 | 1.3 |
  3166. ;; | Row 2 | 2.1 | 2.2 | 2.3 |
  3167. ;;
  3168. ;; Note that org-tables are NOT multi-line and each line is mapped to
  3169. ;; a unique row in the exported document. So if an exported table
  3170. ;; needs to contain a single paragraph (with copious text) it needs to
  3171. ;; be typed up in a single line. Editing such long lines using the
  3172. ;; table editor will be a cumbersome task. Furthermore inclusion of
  3173. ;; multi-paragraph text in a table cell is well-nigh impossible.
  3174. ;;
  3175. ;; A LIST-TABLE circumvents above problems.
  3176. ;;
  3177. ;; Note that in the example above the list items could be paragraphs
  3178. ;; themselves and the list can be arbitrarily deep.
  3179. ;;
  3180. ;; Inspired by following thread:
  3181. ;; https://lists.gnu.org/archive/html/emacs-orgmode/2011-03/msg01101.html
  3182. ;; Translate lists to tables
  3183. (defun org-e-odt--translate-list-tables (tree backend info)
  3184. (org-element-map
  3185. tree 'plain-list
  3186. (lambda (l1-list)
  3187. (when (org-export-read-attribute :attr_odt l1-list :list-table)
  3188. ;; Replace list with table.
  3189. (org-element-set-element
  3190. l1-list
  3191. ;; Build replacement table.
  3192. (apply 'org-element-adopt-elements
  3193. (list 'table '(:type org :attr_odt (":style \"GriddedTable\"")))
  3194. (org-element-map
  3195. l1-list
  3196. 'item
  3197. (lambda (l1-item)
  3198. (let* ((l1-item-contents (org-element-contents l1-item))
  3199. l1-item-leading-text l2-list)
  3200. ;; Remove Level-2 list from the Level-item. It
  3201. ;; will be subsequently attached as table-cells.
  3202. (let ((cur l1-item-contents) prev)
  3203. (while (and cur (not (eq (org-element-type (car cur))
  3204. 'plain-list)))
  3205. (setq prev cur)
  3206. (setq cur (cdr cur)))
  3207. (when prev
  3208. (setcdr prev nil)
  3209. (setq l2-list (car cur)))
  3210. (setq l1-item-leading-text l1-item-contents))
  3211. ;; Level-1 items start a table row.
  3212. (apply 'org-element-adopt-elements
  3213. (list 'table-row (list :type 'standard))
  3214. ;; Leading text of level-1 item define the
  3215. ;; first table-cell.
  3216. (apply 'org-element-adopt-elements
  3217. (list 'table-cell nil)
  3218. l1-item-leading-text)
  3219. ;; Level-2 items define subsequent
  3220. ;; table-cells of the row.
  3221. (org-element-map
  3222. l2-list
  3223. 'item
  3224. (lambda (l2-item)
  3225. (apply 'org-element-adopt-elements
  3226. (list 'table-cell nil)
  3227. (org-element-contents l2-item)))
  3228. info nil 'item))))
  3229. info nil 'item))))
  3230. nil)
  3231. info)
  3232. tree)
  3233. ;;; Interactive functions
  3234. (defun org-e-odt-create-manifest-file-entry (&rest args)
  3235. (push args org-e-odt-manifest-file-entries))
  3236. (defun org-e-odt-write-manifest-file ()
  3237. (make-directory (concat org-e-odt-zip-dir "META-INF"))
  3238. (let ((manifest-file (concat org-e-odt-zip-dir "META-INF/manifest.xml")))
  3239. (with-current-buffer
  3240. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3241. (find-file-noselect manifest-file t))
  3242. (insert
  3243. "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
  3244. <manifest:manifest xmlns:manifest=\"urn:oasis:names:tc:opendocument:xmlns:manifest:1.0\" manifest:version=\"1.2\">\n")
  3245. (mapc
  3246. (lambda (file-entry)
  3247. (let* ((version (nth 2 file-entry))
  3248. (extra (if (not version) ""
  3249. (format " manifest:version=\"%s\"" version))))
  3250. (insert
  3251. (format org-e-odt-manifest-file-entry-tag
  3252. (nth 0 file-entry) (nth 1 file-entry) extra))))
  3253. org-e-odt-manifest-file-entries)
  3254. (insert "\n</manifest:manifest>"))))
  3255. (defmacro org-e-odt--export-wrap (out-file &rest body)
  3256. `(let* ((--out-file ,out-file)
  3257. (out-file-type (file-name-extension --out-file))
  3258. (org-e-odt-xml-files '("META-INF/manifest.xml" "content.xml"
  3259. "meta.xml" "styles.xml"))
  3260. ;; Initialize workarea. All files that end up in the
  3261. ;; exported get created here.
  3262. (org-e-odt-zip-dir (file-name-as-directory
  3263. (make-temp-file (format "%s-" out-file-type) t)))
  3264. (org-e-odt-manifest-file-entries nil)
  3265. (--cleanup-xml-buffers
  3266. (function
  3267. (lambda nil
  3268. ;; Kill all XML buffers.
  3269. (mapc (lambda (file)
  3270. (let ((buf (get-file-buffer
  3271. (concat org-e-odt-zip-dir file))))
  3272. (when buf
  3273. (set-buffer-modified-p nil)
  3274. (kill-buffer buf))))
  3275. org-e-odt-xml-files)
  3276. ;; Delete temporary directory and also other embedded
  3277. ;; files that get copied there.
  3278. (delete-directory org-e-odt-zip-dir t)))))
  3279. (org-condition-case-unless-debug err
  3280. (progn
  3281. (unless (executable-find "zip")
  3282. ;; Not at all OSes ship with zip by default
  3283. (error "Executable \"zip\" needed for creating OpenDocument files"))
  3284. ;; Do export. This creates a bunch of xml files ready to be
  3285. ;; saved and zipped.
  3286. (progn ,@body)
  3287. ;; Create a manifest entry for content.xml.
  3288. (org-e-odt-create-manifest-file-entry "text/xml" "content.xml")
  3289. ;; Write mimetype file
  3290. (let* ((mimetypes
  3291. '(("odt" . "application/vnd.oasis.opendocument.text")
  3292. ("odf" . "application/vnd.oasis.opendocument.formula")))
  3293. (mimetype (cdr (assoc-string out-file-type mimetypes t))))
  3294. (unless mimetype
  3295. (error "Unknown OpenDocument backend %S" out-file-type))
  3296. (write-region mimetype nil (concat org-e-odt-zip-dir "mimetype"))
  3297. (org-e-odt-create-manifest-file-entry mimetype "/" "1.2"))
  3298. ;; Write out the manifest entries before zipping
  3299. (org-e-odt-write-manifest-file)
  3300. ;; Save all XML files.
  3301. (mapc (lambda (file)
  3302. (let ((buf (get-file-buffer (concat org-e-odt-zip-dir file))))
  3303. (when buf
  3304. (with-current-buffer buf
  3305. ;; Prettify output if needed.
  3306. (when org-e-odt-prettify-xml
  3307. (indent-region (point-min) (point-max)))
  3308. (save-buffer 0)))))
  3309. org-e-odt-xml-files)
  3310. ;; Run zip.
  3311. (let* ((target --out-file)
  3312. (target-name (file-name-nondirectory target))
  3313. (target-dir (file-name-directory target))
  3314. (cmds `(("zip" "-mX0" ,target-name "mimetype")
  3315. ("zip" "-rmTq" ,target-name "."))))
  3316. ;; If a file with same name as the desired output file
  3317. ;; exists, remove it.
  3318. (when (file-exists-p target)
  3319. (delete-file target))
  3320. ;; Zip up the xml files.
  3321. (let ((coding-system-for-write 'no-conversion) exitcode err-string)
  3322. (message "Creating ODT file...")
  3323. ;; Switch temporarily to content.xml. This way Zip
  3324. ;; process will inherit `org-e-odt-zip-dir' as the current
  3325. ;; directory.
  3326. (with-current-buffer
  3327. (find-file-noselect (concat org-e-odt-zip-dir "content.xml") t)
  3328. (mapc
  3329. (lambda (cmd)
  3330. (message "Running %s" (mapconcat 'identity cmd " "))
  3331. (setq err-string
  3332. (with-output-to-string
  3333. (setq exitcode
  3334. (apply 'call-process (car cmd)
  3335. nil standard-output nil (cdr cmd)))))
  3336. (or (zerop exitcode)
  3337. (error (concat "Unable to create OpenDocument file."
  3338. (format " Zip failed with error (%s)"
  3339. err-string)))))
  3340. cmds)
  3341. ;; Zip file is now in the rightful place.
  3342. (rename-file target-name target)))
  3343. (message "Created %s" target)
  3344. ;; Cleanup work directory and work files.
  3345. (funcall --cleanup-xml-buffers)
  3346. ;; Open the OpenDocument file in archive-mode for
  3347. ;; examination.
  3348. (find-file-noselect target t)
  3349. ;; Return exported file.
  3350. (cond
  3351. ;; Case 1: Conversion desired on exported file. Run the
  3352. ;; converter on the OpenDocument file. Return the
  3353. ;; converted file.
  3354. (org-e-odt-preferred-output-format
  3355. (or (org-e-odt-convert target org-e-odt-preferred-output-format)
  3356. target))
  3357. ;; Case 2: No further conversion. Return exported
  3358. ;; OpenDocument file.
  3359. (t target))))
  3360. (error
  3361. ;; Cleanup work directory and work files.
  3362. (funcall --cleanup-xml-buffers)
  3363. (message "OpenDocument export failed: %s"
  3364. (error-message-string err))))))
  3365. ;;;; Export to OpenDocument formula
  3366. ;;;###autoload
  3367. (defun org-e-odt-export-as-odf (latex-frag &optional odf-file)
  3368. "Export LATEX-FRAG as OpenDocument formula file ODF-FILE.
  3369. Use `org-create-math-formula' to convert LATEX-FRAG first to
  3370. MathML. When invoked as an interactive command, use
  3371. `org-latex-regexps' to infer LATEX-FRAG from currently active
  3372. region. If no LaTeX fragments are found, prompt for it. Push
  3373. MathML source to kill ring, if `org-export-copy-to-kill-ring' is
  3374. non-nil."
  3375. (interactive
  3376. `(,(let (frag)
  3377. (setq frag (and (setq frag (and (region-active-p)
  3378. (buffer-substring (region-beginning)
  3379. (region-end))))
  3380. (loop for e in org-latex-regexps
  3381. thereis (when (string-match (nth 1 e) frag)
  3382. (match-string (nth 2 e) frag)))))
  3383. (read-string "LaTeX Fragment: " frag nil frag))
  3384. ,(let ((odf-filename (expand-file-name
  3385. (concat
  3386. (file-name-sans-extension
  3387. (or (file-name-nondirectory buffer-file-name)))
  3388. "." "odf")
  3389. (file-name-directory buffer-file-name))))
  3390. (read-file-name "ODF filename: " nil odf-filename nil
  3391. (file-name-nondirectory odf-filename)))))
  3392. (let ((filename (or odf-file
  3393. (expand-file-name
  3394. (concat
  3395. (file-name-sans-extension
  3396. (or (file-name-nondirectory buffer-file-name)))
  3397. "." "odf")
  3398. (file-name-directory buffer-file-name)))))
  3399. (org-e-odt--export-wrap
  3400. filename
  3401. (let* ((buffer (progn
  3402. (require 'nxml-mode)
  3403. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3404. (find-file-noselect (concat org-e-odt-zip-dir
  3405. "content.xml") t))))
  3406. (coding-system-for-write 'utf-8)
  3407. (save-buffer-coding-system 'utf-8))
  3408. (set-buffer buffer)
  3409. (set-buffer-file-coding-system coding-system-for-write)
  3410. (let ((mathml (org-create-math-formula latex-frag)))
  3411. (unless mathml (error "No Math formula created"))
  3412. (insert mathml)
  3413. ;; Add MathML to kill ring, if needed.
  3414. (when org-export-copy-to-kill-ring
  3415. (org-kill-new (buffer-string))))))))
  3416. ;;;###autoload
  3417. (defun org-e-odt-export-as-odf-and-open ()
  3418. "Export LaTeX fragment as OpenDocument formula and immediately open it.
  3419. Use `org-e-odt-export-as-odf' to read LaTeX fragment and OpenDocument
  3420. formula file."
  3421. (interactive)
  3422. (org-open-file (call-interactively 'org-e-odt-export-as-odf) 'system))
  3423. ;;;; Export to OpenDocument Text
  3424. ;;;###autoload
  3425. (defun org-e-odt-export-to-odt
  3426. (&optional subtreep visible-only body-only ext-plist pub-dir)
  3427. "Export current buffer to a HTML file.
  3428. If narrowing is active in the current buffer, only export its
  3429. narrowed part.
  3430. If a region is active, export that region.
  3431. When optional argument SUBTREEP is non-nil, export the sub-tree
  3432. at point, extracting information from the headline properties
  3433. first.
  3434. When optional argument VISIBLE-ONLY is non-nil, don't export
  3435. contents of hidden elements.
  3436. When optional argument BODY-ONLY is non-nil, only write code
  3437. between \"\\begin{document}\" and \"\\end{document}\".
  3438. EXT-PLIST, when provided, is a property list with external
  3439. parameters overriding Org default settings, but still inferior to
  3440. file-local settings.
  3441. When optional argument PUB-DIR is set, use it as the publishing
  3442. directory.
  3443. Return output file's name."
  3444. (interactive)
  3445. (org-e-odt--export-wrap
  3446. (org-export-output-file-name ".odt" subtreep pub-dir)
  3447. (let* ((org-e-odt-embedded-images-count 0)
  3448. (org-e-odt-embedded-formulas-count 0)
  3449. (org-e-odt-automatic-styles nil)
  3450. (org-e-odt-object-counters nil)
  3451. ;; Let `htmlfontify' know that we are interested in collecting
  3452. ;; styles.
  3453. (hfy-user-sheet-assoc nil))
  3454. ;; Initialize content.xml and kick-off the export process.
  3455. (let ((out-buf (progn
  3456. (require 'nxml-mode)
  3457. (let ((nxml-auto-insert-xml-declaration-flag nil))
  3458. (find-file-noselect
  3459. (concat org-e-odt-zip-dir "content.xml") t)))))
  3460. (org-export-to-buffer 'e-odt out-buf subtreep visible-only body-only)))))
  3461. ;;;; Convert between OpenDocument and other formats
  3462. (defun org-e-odt-reachable-p (in-fmt out-fmt)
  3463. "Return non-nil if IN-FMT can be converted to OUT-FMT."
  3464. (catch 'done
  3465. (let ((reachable-formats (org-e-odt-do-reachable-formats in-fmt)))
  3466. (dolist (e reachable-formats)
  3467. (let ((out-fmt-spec (assoc out-fmt (cdr e))))
  3468. (when out-fmt-spec
  3469. (throw 'done (cons (car e) out-fmt-spec))))))))
  3470. (defun org-e-odt-do-convert (in-file out-fmt &optional prefix-arg)
  3471. "Workhorse routine for `org-e-odt-convert'."
  3472. (require 'browse-url)
  3473. (let* ((in-file (expand-file-name (or in-file buffer-file-name)))
  3474. (dummy (or (file-readable-p in-file)
  3475. (error "Cannot read %s" in-file)))
  3476. (in-fmt (file-name-extension in-file))
  3477. (out-fmt (or out-fmt (error "Output format unspecified")))
  3478. (how (or (org-e-odt-reachable-p in-fmt out-fmt)
  3479. (error "Cannot convert from %s format to %s format?"
  3480. in-fmt out-fmt)))
  3481. (convert-process (car how))
  3482. (out-file (concat (file-name-sans-extension in-file) "."
  3483. (nth 1 (or (cdr how) out-fmt))))
  3484. (extra-options (or (nth 2 (cdr how)) ""))
  3485. (out-dir (file-name-directory in-file))
  3486. (cmd (format-spec convert-process
  3487. `((?i . ,(shell-quote-argument in-file))
  3488. (?I . ,(browse-url-file-url in-file))
  3489. (?f . ,out-fmt)
  3490. (?o . ,out-file)
  3491. (?O . ,(browse-url-file-url out-file))
  3492. (?d . , (shell-quote-argument out-dir))
  3493. (?D . ,(browse-url-file-url out-dir))
  3494. (?x . ,extra-options)))))
  3495. (when (file-exists-p out-file)
  3496. (delete-file out-file))
  3497. (message "Executing %s" cmd)
  3498. (let ((cmd-output (shell-command-to-string cmd)))
  3499. (message "%s" cmd-output))
  3500. (cond
  3501. ((file-exists-p out-file)
  3502. (message "Exported to %s" out-file)
  3503. (when prefix-arg
  3504. (message "Opening %s..." out-file)
  3505. (org-open-file out-file 'system))
  3506. out-file)
  3507. (t
  3508. (message "Export to %s failed" out-file)
  3509. nil))))
  3510. (defun org-e-odt-do-reachable-formats (in-fmt)
  3511. "Return verbose info about formats to which IN-FMT can be converted.
  3512. Return a list where each element is of the
  3513. form (CONVERTER-PROCESS . OUTPUT-FMT-ALIST). See
  3514. `org-e-odt-convert-processes' for CONVERTER-PROCESS and see
  3515. `org-e-odt-convert-capabilities' for OUTPUT-FMT-ALIST."
  3516. (let* ((converter
  3517. (and org-e-odt-convert-process
  3518. (cadr (assoc-string org-e-odt-convert-process
  3519. org-e-odt-convert-processes t))))
  3520. (capabilities
  3521. (and org-e-odt-convert-process
  3522. (cadr (assoc-string org-e-odt-convert-process
  3523. org-e-odt-convert-processes t))
  3524. org-e-odt-convert-capabilities))
  3525. reachable-formats)
  3526. (when converter
  3527. (dolist (c capabilities)
  3528. (when (member in-fmt (nth 1 c))
  3529. (push (cons converter (nth 2 c)) reachable-formats))))
  3530. reachable-formats))
  3531. (defun org-e-odt-reachable-formats (in-fmt)
  3532. "Return list of formats to which IN-FMT can be converted.
  3533. The list of the form (OUTPUT-FMT-1 OUTPUT-FMT-2 ...)."
  3534. (let (l)
  3535. (mapc (lambda (e) (add-to-list 'l e))
  3536. (apply 'append (mapcar
  3537. (lambda (e) (mapcar 'car (cdr e)))
  3538. (org-e-odt-do-reachable-formats in-fmt))))
  3539. l))
  3540. (defun org-e-odt-convert-read-params ()
  3541. "Return IN-FILE and OUT-FMT params for `org-e-odt-do-convert'.
  3542. This is a helper routine for interactive use."
  3543. (let* ((input (if (featurep 'ido) 'ido-completing-read 'completing-read))
  3544. (in-file (read-file-name "File to be converted: "
  3545. nil buffer-file-name t))
  3546. (in-fmt (file-name-extension in-file))
  3547. (out-fmt-choices (org-e-odt-reachable-formats in-fmt))
  3548. (out-fmt
  3549. (or (and out-fmt-choices
  3550. (funcall input "Output format: "
  3551. out-fmt-choices nil nil nil))
  3552. (error
  3553. "No known converter or no known output formats for %s files"
  3554. in-fmt))))
  3555. (list in-file out-fmt)))
  3556. ;;;###autoload
  3557. (defun org-e-odt-convert (&optional in-file out-fmt prefix-arg)
  3558. "Convert IN-FILE to format OUT-FMT using a command line converter.
  3559. IN-FILE is the file to be converted. If unspecified, it defaults
  3560. to variable `buffer-file-name'. OUT-FMT is the desired output
  3561. format. Use `org-e-odt-convert-process' as the converter.
  3562. If PREFIX-ARG is non-nil then the newly converted file is opened
  3563. using `org-open-file'."
  3564. (interactive
  3565. (append (org-e-odt-convert-read-params) current-prefix-arg))
  3566. (org-e-odt-do-convert in-file out-fmt prefix-arg))
  3567. ;;; Library Initializations
  3568. (mapc
  3569. (lambda (desc)
  3570. ;; Let Emacs open all OpenDocument files in archive mode
  3571. (add-to-list 'auto-mode-alist
  3572. (cons (concat "\\." (car desc) "\\'") 'archive-mode)))
  3573. org-e-odt-file-extensions)
  3574. (provide 'org-e-odt)
  3575. ;;; org-e-odt.el ends here