org-e-odt.el 158 KB

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