ox-odt.el 159 KB

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