org-e-odt.el 144 KB

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