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