org-e-odt.el 144 KB

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