ox-odt.el 159 KB

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