org-e-odt.el 147 KB

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