org-e-odt.el 146 KB

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