ox-odt.el 158 KB

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