org-e-odt.el 161 KB

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