Exporting accented characters to latex from org-mode

| categories: uncategorized | tags:

I noticed recently in writing a technical paper in org-mode that I had some trouble exporting some accented characters to LaTeX.

Here are 5 words that render correctly in LaTeX

1. Jos\'{e}
2. peque\~{n}o
3. Gro\ss
4. Gr\"{u}neisen
5. N\o{}rskov

Here we wrap these words in a LaTeX block so it exports verbatim to see how they look in a PDF.

Note to see this in LaTeX, you must view the exporting-accented-characters.pdf. Now, we use the same characters in org-mode.

  1. Jos\'{e}
  2. peque\~{n}o
  3. Gro\ss
  4. Gr\"{u}neisen
  5. N\o{}rskov

The exported LaTeX code looks like:

\begin{enumerate}
\item Jos$\backslash$'\{e\}
\item peque$\backslash$\textasciitilde{}\{n\}o
\item Gro\ss
\item Gr$\backslash$"\{u\}neisen
\item N\o{}rskov
\end{enumerate}

The exporter does not handle all of them correctly. Org-mode is its own system, and it is not, and won't be a total replacement for LaTeX. Nevertheless, these are pretty common characters for me, and We need a solution! A clunky way we found to solve this is to add a LATEXHEADER line that defines a new LaTeX command like this:

#+LATEX_HEADER: \newcommand{\gruneisen}{Gr\"{u}neisen}

Then you can use the new command in org-mode. So this text:

We use \gruneisen in a sentence.

Renders like this:

We use \gruneisen in a sentence.

That is not too ideal, since some journals do not like you to define new commands. It turns out that org-mode has its own commands to solve this problem! There is a list of these commands stored in a variable called org-entities.

Here we print these entities for "the record". I add an extra star to the data in org-entities so they will all be nested in this post.

(mapcar (lambda(x)
  "print element x. If it is a heading, add an extra star"
  (interactive)
  (if (and (stringp x) (string= (substring x 0 1) "*"))
      (princ (format "*%s\n" x))
    (princ (format "%s\n" x)))) org-entities)

1 Letters

1.1 Latin

(Agrave \`{A} nil À A À À) (agrave \`{a} nil à a à à) (Aacute \'{A} nil Á A Á Á) (aacute \'{a} nil á a á á) (Acirc \A nil  A  Â) (acirc \a nil â a â â) (Atilde \~{A} nil à A à Ã) (atilde \~{a} nil ã a ã ã) (Auml \"{A} nil Ä Ae Ä Ä) (auml \"{a} nil ä ae ä ä) (Aring Å nil Å A Å Å) (AA Å nil Å A Å Å) (aring \aa{} nil å a å å) (AElig \AE{} nil Æ AE Æ Æ) (aelig \ae{} nil æ ae æ æ) (Ccedil \c{C} nil Ç C Ç Ç) (ccedil \c{c} nil ç c ç ç) (Egrave \`{E} nil È E È È) (egrave \`{e} nil è e è è) (Eacute \'{E} nil É E É É) (eacute \'{e} nil é e é é) (Ecirc \E nil Ê E Ê Ê) (ecirc \e nil ê e ê ê) (Euml \"{E} nil Ë E Ë Ë) (euml \"{e} nil ë e ë ë) (Igrave \`{I} nil Ì I Ì Ì) (igrave \`{i} nil ì i ì ì) (Iacute \'{I} nil Í I Í Í) (iacute \'{i} nil í i í í) (Icirc \I nil Î I Î Î) (icirc \i nil î i î î) (Iuml \"{I} nil Ï I Ï Ï) (iuml \"{i} nil ï i ï ï) (Ntilde \~{N} nil Ñ N Ñ Ñ) (ntilde \~{n} nil ñ n ñ ñ) (Ograve \`{O} nil Ò O Ò Ò) (ograve \`{o} nil ò o ò ò) (Oacute \'{O} nil Ó O Ó Ó) (oacute \'{o} nil ó o ó ó) (Ocirc \O nil Ô O Ô Ô) (ocirc \o nil ô o ô ô) (Otilde \~{O} nil Õ O Õ Õ) (otilde \~{o} nil õ o õ õ) (Ouml \"{O} nil Ö Oe Ö Ö) (ouml \"{o} nil ö oe ö ö) (Oslash \O nil Ø O Ø Ø) (oslash \o{} nil ø o ø ø) (OElig \OE{} nil Œ OE OE Œ) (oelig \oe{} nil œ oe oe œ) (Scaron \v{S} nil Š S S Š) (scaron \v{s} nil š s s š) (szlig \ss{} nil ß ss ß ß) (Ugrave \`{U} nil Ù U Ù Ù) (ugrave \`{u} nil ù u ù ù) (Uacute \'{U} nil Ú U Ú Ú) (uacute \'{u} nil ú u ú ú) (Ucirc \U nil Û U Û Û) (ucirc \u nil û u û û) (Uuml \"{U} nil Ü Ue Ü Ü) (uuml \"{u} nil ü ue ü ü) (Yacute \'{Y} nil Ý Y Ý Ý) (yacute \'{y} nil ý y ý ý) (Yuml \"{Y} nil Ÿ Y Y Ÿ) (yuml \"{y} nil ÿ y ÿ ÿ)

1.2 Latin (special face)

(fnof \textit{f} nil ƒ f f ƒ) (real \Re t ℜ R R ℜ) (image \Im t ℑ I I ℑ) (weierp \wp t ℘ P P ℘)

1.3 Greek

(Alpha A nil Α Alpha Alpha Α) (alpha α t α alpha alpha α) (Beta B nil Β Beta Beta Β) (beta β t β beta beta β) (Gamma Γ t Γ Gamma Gamma Γ) (gamma γ t γ gamma gamma γ) (Delta Δ t Δ Delta Gamma Δ) (delta δ t δ delta delta δ) (Epsilon E nil Ε Epsilon Epsilon Ε) (epsilon ε t ε epsilon epsilon ε) (varepsilon ε t ε varepsilon varepsilon ε) (Zeta Z nil Ζ Zeta Zeta Ζ) (zeta ζ t ζ zeta zeta ζ) (Eta H nil Η Eta Eta Η) (eta η t η eta eta η) (Theta Θ t Θ Theta Theta Θ) (theta θ t θ theta theta θ) (thetasym ϑ t ϑ theta theta ϑ) (vartheta ϑ t ϑ theta theta ϑ) (Iota I nil Ι Iota Iota Ι) (iota ι t ι iota iota ι) (Kappa K nil Κ Kappa Kappa Κ) (kappa κ t κ kappa kappa κ) (Lambda Λ t Λ Lambda Lambda Λ) (lambda λ t λ lambda lambda λ) (Mu M nil Μ Mu Mu Μ) (mu μ t μ mu mu μ) (nu ν t ν nu nu ν) (Nu N nil Ν Nu Nu Ν) (Xi Ξ t Ξ Xi Xi Ξ) (xi ξ t ξ xi xi ξ) (Omicron O nil Ο Omicron Omicron Ο) (omicron \textit{o} nil ο omicron omicron ο) (Pi Π t Π Pi Pi Π) (pi π t π pi pi π) (Rho P nil Ρ Rho Rho Ρ) (rho ρ t ρ rho rho ρ) (Sigma Σ t Σ Sigma Sigma Σ) (sigma σ t σ sigma sigma σ) (sigmaf ς t ς sigmaf sigmaf ς) (varsigma ς t ς varsigma varsigma ς) (Tau T nil Τ Tau Tau Τ) (Upsilon Υ t Υ Upsilon Upsilon Υ) (upsih Υ t ϒ upsilon upsilon ϒ) (upsilon υ t υ upsilon upsilon υ) (Phi Φ t Φ Phi Phi Φ) (phi φ t φ phi phi φ) (Chi X nil Χ Chi Chi Χ) (chi χ t χ chi chi χ) (acutex ´ x t ´x 'x 'x 𝑥́) (Psi Ψ t Ψ Psi Psi Ψ) (psi ψ t ψ psi psi ψ) (tau τ t τ tau tau τ) (Omega Ω t Ω Omega Omega Ω) (omega ω t ω omega omega ω) (piv \varpi t ϖ omega-pi omega-pi ϖ) (partial ∂ t ∂ [partial differential] [partial differential] ∂)

1.4 Hebrew

(alefsym \aleph t ℵ aleph aleph ℵ)

1.5 Dead languages

(ETH \DH{} nil Ð D Ð Ð) (eth \dh{} nil ð dh ð ð) (THORN \TH{} nil Þ TH Þ Þ) (thorn \th{} nil þ th þ þ)

2 Punctuation

2.1 Dots and Marks

(dots … nil … … … …) (hellip … nil … … … …) (middot \textperiodcentered{} nil · . · ·) (iexcl !` nil ¡ ! ¡ ¡) (iquest ?` nil ¿ ? ¿ ¿)

2.2 Dash-like

(shy ­ nil ­ ) (ndash – nil – - - –) (mdash — nil — – – —)

2.3 Quotations

(quot \textquotedbl{} nil &quot; " " ") (acute \textasciiacute{} nil &acute; ' ´ ´) (ldquo \textquotedblleft{} nil &ldquo; " " “) (rdquo \textquotedblright{} nil &rdquo; " " ”) (bdquo \quotedblbase{} nil &bdquo; " " „) (lsquo \textquoteleft{} nil &lsquo; ` ` ‘) (rsquo \textquoteright{} nil &rsquo; ' ' ’) (sbquo \quotesinglbase{} nil &sbquo; , , ‚) (laquo \guillemotleft{} nil &laquo; << « «) (raquo \guillemotright{} nil &raquo; >> » ») (lsaquo \guilsinglleft{} nil &lsaquo; < < ‹) (rsaquo \guilsinglright{} nil &rsaquo; > > ›)

3 Other

3.1 Misc. (often used)

(circ \nil nil &circ; ^ ^ ˆ) (vert | t &#124; | | |) (brvbar \textbrokenbar{} nil &brvbar; | ¦ ¦) (sect \S nil &sect; paragraph § §) (amp \& nil &amp; & & &) (lt \textless{} nil &lt; < < <) (gt \textgreater{} nil &gt; > > >) (tilde \~{} nil &tilde; ~ ~ ~) (slash / nil / / / /) (plus + nil + + + +) (under \_ nil _ _ _ _) (equal = nil = = = =) (asciicirc \textasciicircum{} nil ^ ^ ^ ^) (dagger \textdagger{} nil &dagger; [dagger] [dagger] †) (Dagger \textdaggerdbl{} nil &Dagger; [doubledagger] [doubledagger] ‡)

3.2 Whitespace

(nbsp ~ nil &nbsp; ) (ensp \hspace*{.5em} nil &ensp;  ) (emsp \hspace*{1em} nil &emsp;  ) (thinsp \hspace*{.2em} nil &thinsp;  )

3.3 Currency

(curren \textcurrency{} nil &curren; curr. ¤ ¤) (cent \textcent{} nil &cent; cent ¢ ¢) (pound \pounds{} nil &pound; pound £ £) (yen \textyen{} nil &yen; yen ¥ ¥) (euro \texteuro{} nil &euro; EUR EUR €) (EUR € nil &euro; EUR EUR €) (EURdig € nil &euro; EUR EUR €) (EURhv € nil &euro; EUR EUR €) (EURcr € nil &euro; EUR EUR €) (EURtm € nil &euro; EUR EUR €)

3.4 Property Marks

(copy \textcopyright{} nil &copy; (c) © ©) (reg \textregistered{} nil &reg; (r) ® ®) (trade \texttrademark{} nil &trade; TM TM ™)

3.5 Science et al.

(minus − t &minus; - - −) (pm \textpm{} nil &plusmn; +- ± ±) (plusmn \textpm{} nil &plusmn; +- ± ±) (times \texttimes{} nil &times; * × ×) (frasl / nil &frasl; / / ⁄) (div \textdiv{} nil &divide; / ÷ ÷) (frac12 \textonehalf{} nil &frac12; 1/2 ½ ½) (frac14 \textonequarter{} nil &frac14; 1/4 ¼ ¼) (frac34 \textthreequarters{} nil &frac34; 3/4 ¾ ¾) (permil \textperthousand{} nil &permil; per thousand per thousand ‰) (sup1 \textonesuperior{} nil &sup1; ^1 ¹ ¹) (sup2 \texttwosuperior{} nil &sup2; ^2 ² ²) (sup3 \textthreesuperior{} nil &sup3; ^3 ³ ³) (radic \sqrt{\,} t &radic; [square root] [square root] √) (sum ∑ t &sum; [sum] [sum] ∑) (prod ∏ t &prod; [product] [n-ary product] ∏) (micro \textmu{} nil &micro; micro µ µ) (macr \textasciimacron{} nil &macr; [macron] ¯ ¯) (deg \textdegree{} nil &deg; degree ° °) (prime ′ t &prime; ' ' ′) (Prime ′′ t &Prime; '' '' ″) (infin \propto t &infin; [infinity] [infinity] ∞) (infty ∞ t &infin; [infinity] [infinity] ∞) (prop \propto t &prop; [proportional to] [proportional to] ∝) (proptp \propto t &prop; [proportional to] [proportional to] ∝) (not \textlnot{} nil &not; [angled dash] ¬ ¬) (neg ¬ t &not; [angled dash] ¬ ¬) (land ∧ t &and; [logical and] [logical and] ∧) (wedge ∧ t &and; [logical and] [logical and] ∧) (lor ∨ t &or; [logical or] [logical or] ∨) (vee ∨ t &or; [logical or] [logical or] ∨) (cap ∩ t &cap; [intersection] [intersection] ∩) (cup ∪ t &cup; [union] [union] ∪) (int ∫ t &int; [integral] [integral] ∫) (there4 \therefore t &there4; [therefore] [therefore] ∴) (sim ∼ t &sim; ~ ~ ∼) (cong ≅ t &cong; [approx. equal to] [approx. equal to] ≅) (simeq ≅ t &cong; [approx. equal to] [approx. equal to] ≅) (asymp ≈ t &asymp; [almost equal to] [almost equal to] ≈) (approx ≈ t &asymp; [almost equal to] [almost equal to] ≈) (ne ≠ t &ne; [not equal to] [not equal to] ≠) (neq ≠ t &ne; [not equal to] [not equal to] ≠) (equiv ≡ t &equiv; [identical to] [identical to] ≡) (le ≤ t &le; <= <= ≤) (ge ≥ t &ge; >= >= ≥) (sub ⊂ t &sub; [subset of] [subset of] ⊂) (subset ⊂ t &sub; [subset of] [subset of] ⊂) (sup ⊃ t &sup; [superset of] [superset of] ⊃) (supset ⊃ t &sup; [superset of] [superset of] ⊃) (nsub ¬⊂ t &nsub; [not a subset of] [not a subset of ⊄) (sube \subseteq t &sube; [subset of or equal to] [subset of or equal to] ⊆) (nsup ¬⊃ t &nsup; [not a superset of] [not a superset of] ⊅) (supe \supseteq t &supe; [superset of or equal to] [superset of or equal to] ⊇) (forall ∀ t &forall; [for all] [for all] ∀) (exist ∃ t &exist; [there exists] [there exists] ∃) (exists ∃ t &exist; [there exists] [there exists] ∃) (empty ∅ t &empty; [empty set] [empty set] ∅) (emptyset ∅ t &empty; [empty set] [empty set] ∅) (isin ∈ t &isin; [element of] [element of] ∈) (in ∈ t &isin; [element of] [element of] ∈) (notin ∉ t &notin; [not an element of] [not an element of] ∉) (ni ∋ t &ni; [contains as member] [contains as member] ∋) (nabla ∇ t &nabla; [nabla] [nabla] ∇) (ang ∠ t &ang; [angle] [angle] ∠) (angle ∠ t &ang; [angle] [angle] ∠) (perp ⊥ t &perp; [up tack] [up tack] ⊥) (sdot ⋅ t &sdot; [dot] [dot] ⋅) (cdot ⋅ t &sdot; [dot] [dot] ⋅) (lceil ⌈ t &lceil; [left ceiling] [left ceiling] ⌈) (rceil ⌉ t &rceil; [right ceiling] [right ceiling] ⌉) (lfloor ⌊ t &lfloor; [left floor] [left floor] ⌊) (rfloor ⌋ t &rfloor; [right floor] [right floor] ⌋) (lang \langle t &lang; < < ⟨) (rang \rangle t &rang; > > ⟩) (hbar ℏ t &#8463; hbar hbar ℏ)

3.6 Arrows

(larr ← t &larr; <- <- ←) (leftarrow ← t &larr; <- <- ←) (gets ← t &larr; <- <- ←) (lArr ⇐ t &lArr; <= <= ⇐) (Leftarrow ⇐ t &lArr; <= <= ⇐) (uarr ↑ t &uarr; [uparrow] [uparrow] ↑) (uparrow ↑ t &uarr; [uparrow] [uparrow] ↑) (uArr ⇑ t &uArr; [dbluparrow] [dbluparrow] ⇑) (Uparrow ⇑ t &uArr; [dbluparrow] [dbluparrow] ⇑) (rarr → t &rarr; -> -> →) (to → t &rarr; -> -> →) (rightarrow → t &rarr; -> -> →) (rArr ⇒ t &rArr; => => ⇒) (Rightarrow ⇒ t &rArr; => => ⇒) (darr ↓ t &darr; [downarrow] [downarrow] ↓) (downarrow ↓ t &darr; [downarrow] [downarrow] ↓) (dArr ⇓ t &dArr; [dbldownarrow] [dbldownarrow] ⇓) (Downarrow ⇓ t &dArr; [dbldownarrow] [dbldownarrow] ⇓) (harr ↔ t &harr; <-> <-> ↔) (leftrightarrow ↔ t &harr; <-> <-> ↔) (hArr ⇔ t &hArr; <=> <=> ⇔) (Leftrightarrow ⇔ t &hArr; <=> <=> ⇔) (crarr ↵ t &crarr; <-' <-' ↵) (hookleftarrow ↵ t &crarr; <-' <-' ↵)

3.7 Function names

(arccos arccos t arccos arccos arccos arccos) (arcsin arcsin t arcsin arcsin arcsin arcsin) (arctan arctan t arctan arctan arctan arctan) (arg arg t arg arg arg arg) (cos cos t cos cos cos cos) (cosh cosh t cosh cosh cosh cosh) (cot cot t cot cot cot cot) (coth coth t coth coth coth coth) (csc csc t csc csc csc csc) (deg ° t &deg; deg deg deg) (det det t det det det det) (dim dim t dim dim dim dim) (exp exp t exp exp exp exp) (gcd gcd t gcd gcd gcd gcd) (hom hom t hom hom hom hom) (inf inf t inf inf inf inf) (ker ker t ker ker ker ker) (lg lg t lg lg lg lg) (lim lim t lim lim lim lim) (liminf liminf t liminf liminf liminf liminf) (limsup limsup t limsup limsup limsup limsup) (ln ln t ln ln ln ln) (log log t log log log log) (max max t max max max max) (min min t min min min min) (Pr Pr t Pr Pr Pr Pr) (sec sec t sec sec sec sec) (sin sin t sin sin sin sin) (sinh sinh t sinh sinh sinh sinh) (sup ⊃ t &sup; sup sup sup) (tan tan t tan tan tan tan) (tanh tanh t tanh tanh tanh tanh)

3.8 Signs & Symbols

(bull \textbullet{} nil &bull; * * •) (bullet \textbullet{} nil &bull; * * •) (star * t * * * ⋆) (lowast ∗ t &lowast; * * ∗) (ast ∗ t &lowast; * * *) (odot o t o [circled dot] [circled dot] ʘ) (oplus ⊕ t &oplus; [circled plus] [circled plus] ⊕) (otimes ⊗ t &otimes; [circled times] [circled times] ⊗) (checkmark ✓ t &#10003; [checkmark] [checkmark] ✓)

3.9 Miscellaneous (seldom used)

(para \P{} nil &para; [pilcrow] ¶ ¶) (ordf \textordfeminine{} nil &ordf; a ª ª) (ordm \textordmasculine{} nil &ordm; o º º) (cedil \c{} nil &cedil; [cedilla] ¸ ¸) (oline \overline{~} t &oline; [overline] ¯ ‾) (uml \textasciidieresis{} nil &uml; [diaeresis] ¨ ¨) (zwnj \/{} nil &zwnj; ‌) (zwj nil &zwj; ‍) (lrm nil &lrm; ‎) (rlm nil &rlm; ‏)

3.10 Smilies

(smile ☺ t &#9786; :-) :-) ⌣) (smiley ☺ nil &#9786; :-) :-) ☺) (blacksmile \blacksmiley{} nil &#9787; :-) :-) ☻) (sad \frownie{} nil &#9785; :-( :-( ☹)

3.11 Suits

(clubs ♣ t &clubs; [clubs] [clubs] ♣) (clubsuit ♣ t &clubs; [clubs] [clubs] ♣) (spades ♠ t &spades; [spades] [spades] ♠) (spadesuit ♠ t &spades; [spades] [spades] ♠) (hearts ♥ t &hearts; [hearts] [hearts] ♥) (heartsuit ♥ t &heartsuit; [hearts] [hearts] ♥) (diams ♦ t &diams; [diamonds] [diamonds] ♦) (diamondsuit ♦ t &diams; [diamonds] [diamonds] ♦) (Diamond \diamond t &diamond; [diamond] [diamond] ⋄) (loz \diamond t &loz; [lozenge] [lozenge] ◊)

4 Summary.

Wow, there are a lot of commands ☺. We just need to use them. For example, I can write Grüneisen, and it finally renders the way it should!

Copyright (C) 2013 by John Kitchin. See the License for information about copying.

org-mode source

Discuss on Twitter

Attaching code blocks to a pdf file during export

| categories: org-mode | tags:

This post is a further exploration of using the export filters to modify construction of content exported from org-mode. In this post we look at some code that will save all of the code-blocks in an org-buffer to systematically named files, and then attach the files to an exported pdf file. We will use the attachfileLaTeX package to attach the scripts. We will build off of this postfor the filters.

First, let us put in a gratuitous code block. In the rendered pdf, this script will be embedded in the pdf. I am not quite ready to build a filter that supports multiple backends, so in this post we just modify the latex export.

name = 'John'
print 'Hello {0}'.format(name)
Hello John

We are only going to attach the python code blocks in this example, and ignore all the other blocks. We will basically use the same kind strategy we have used before. We will initially parse the buffer to get a list of all the code blocks. Then we create a filter for the src-blocks that keeps a counter of src-blocks, and depending on the type of the nth src-block, we will save the file, and modify the text for that block. Here is our code for the list of code blocks.

(setq src-block-list 
      (org-element-map (org-element-parse-buffer) 'src-block 
        (lambda (src-block) src-block)))

Now we create the filter.

(defun ox-mrkup-filter-src-block (text back-end info)
  (catch 'return text)
  (let ((src-block (nth counter src-block-list)))
    (if (string= (org-element-property :language src-block) "python")
        (progn 
          (setq scriptname (format "py-%d.py" counter))
          ;; save code block
          (with-temp-buffer
            (insert (org-element-property :value src-block))
            (write-region (point-min) (point-max) scriptname ))
         
          (setq output (format "%s\n\\attachfile{%s} Double click me to open" text scriptname)))
      ;; else
      (setq output text)))
  ;; increment counter no matter what so next block is processed
  (setq counter (+ counter 1))
  ;; return output
  output)

Finally, we export the document to LaTeX, and run pdflatex on it to generate the pdf.

(let ((counter 0)
      ;; these packages are loaded in the latex file
      (org-latex-default-packages-alist 
       '(("utf8" "inputenc" nil)
         ("T1" "fontenc" nil)
         ("" "fixltx2e" nil)
         ("" "lmodern" nil)
         ("" "minted" nil) ;; for code syntax highlighting
         ;; customize how pdf links look
         ("linktocpage,
           pdfstartview=FitH,
           colorlinks,
           linkcolor=blue,
           anchorcolor=blue,
           citecolor=blue,
           filecolor=blue,
           menucolor=blue,
           urlcolor=blue" "hyperref" nil)))
      (org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
      (async nil)
      (subtreep nil)
      (visible-only nil)
      (body-only nil)
      (ext-plist '()))
  (org-latex-export-to-pdf async subtreep visible-only body-only ext-plist))

Check out the result: attaching-code-blocks-to-a-pdf.pdf. This text won't show up in the pdf. I had some difficulty including the link via org-links. The export engine wanted to embed it as a pdf in itself! That does not seem to work.

Copyright (C) 2013 by John Kitchin. See the License for information about copying.

org-mode source

Discuss on Twitter

Notice anything different

| categories: uncategorized | tags:

Based on the last few posts on making links to external files work in the blog, and customizing code block export in HTML, I have rewritten blogofile.elto more cleanly support the use of images and data files in my blog posts. Now, I should be able to include a data file (like this one) in a post and you should be able to click on the link to open it after I publish the post in the usual way by pressing F10. That should process the post, construct URLs for all the links, including images, copy the relevant files to the blog directory, and generate the HTML file for blogofile to build. This is a little more robust than it used to be, as all files are stored in a directory named based on the post title, so there is less concern of using duplicate filenames for images and datafiles.

Here is a gratuitous image, just to see if it works ;)

Figure 1: test image

Hopefully, there is nothing different on the outside! URLs to images are now in a different place, but that should not be apparent unless you read source code. The real difference is that now there are working links to data files! And it is easier for me to write my posts including them, with simple publishing.

Copyright (C) 2013 by John Kitchin. See the License for information about copying.

org-mode source

Discuss on Twitter

Changing links to files so they work in a blog

| categories: org-mode | tags:

1 Getting links to files in a post to point to the actual file

Occasionally I use a data file in post. In the local org-file buffer I can make a link to the file, and it opens just fine. However, when I publish the post, there is not a mechanism to copy the file to the blog directory, and rewrite the link in exported html so it opens the file.

1.1 link one

This is an explicit file link news.org

1.2 link two

Here we just use a path inside [[]] news.org

1.3 link three

Here we use a decorated link. Org-file containing news

1.4 Checkout all the links

Let us now check out the links. We will use org-element-map to parse the elements and print the link number, the link type, and the :path. For fun, let us put an emacs-lisp link in:

elisp:(princ "test")
and a link to an image:

And finally, I want a link to a file in another directory: fabfile.py

(let ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'link
    (lambda (link) 
      (let ((type (nth 0 link))
            (plist (nth 1 link))
            (content (nth 2 link)))
        (princ (format "%s %s: %s %s\n" counter (plist-get plist ':type) (plist-get plist :path) content))
        (setq counter (+ counter 1))))))
0 file: news.org nil
1 file: ./news.org nil
2 file: ./news.org Org-file containing news
3 elisp: (princ "test") nil
4 file: ../img/tooltip-emacs.png nil
5 file: ../fabfile.py nil
6 http: //orgmode.org/worg/exporters/filter-markup.html nil
7 https: //github.com/jkitchin/jmax/blob/prelude/blogofile.el blogofile.el

So we can see all the links this way, and we will have to differentiate what kind of links they are. What needs to happen when exporting is that the files we want to refer to in the post must be copied to the media folder of the blog, and a url to that file needs to be put in the exported html in the place of the link. We are going to try the following strategy:

  1. Parse the org-file.
  2. For each file link we will copy the file to the blogofile media folder and make a list of URLS for each link
  3. Write a link filter that gets the nth value of our list of urls and replace the link text with a url to the file. If the link had any content, use that for the URL text.

Ok. Now let us write a block of code that does all those things. I want to avoid overwriting files with the same name from other posts, so we will copy all these files to a directory based on the blog post title: "Changing-links-to-files-so-they work-in-a-blog". We construct URLS for each link type that we want and store them in a list in the variable url-list.

(setq url-list (let ((parsetree (org-element-parse-buffer))
                     (counter 0))
                 (org-element-map parsetree 'link
                   (lambda (link) 
                     (let* ((type (nth 0 link))
                            (plist (nth 1 link))
                            (content (nth 2 link))
                            (path (plist-get plist :path))
                            (type (plist-get plist ':type))
                            (fname (car (last (split-string path "/"))))
                            )
                       ;; make directory if it does not exist
                       (make-directory "../media/Changing-links-to-files-so-they work-in-a-blog" t)
                       (cond
                        ;; image
                        ((and (string= type "file")
                              (string-match "png" (file-name-extension fname))) 
                         (progn 
                           (copy-file path (concat "../media/Changing-links-to-files-so-they work-in-a-blog/" fname) t)
                           (format "<img src=\"/media/Changing-links-to-files-so-they work-in-a-blog/%s\">" fname)))
                        ;; regular file with content
                        ((and (string= type "file")  content)
                         (progn (copy-file path (concat "../media/Changing-links-to-files-so-they work-in-a-blog/" fname) t)
                                (format "<a href=\"/media/Changing-links-to-files-so-they work-in-a-blog/%s\">%s</a>" fname content)))
                        ;; regular file with no content
                        ((and (string= type "file"))
                         (progn (copy-file path (concat "../media/Changing-links-to-files-so-they work-in-a-blog/" fname) t)
                                (format "<a href=\"/media/Changing-links-to-files-so-they work-in-a-blog/%s\">%s</a>" fname fname)))
                        ;; URLS
                        ((string-match "http" type) (format "<a href=\"%s\">%s</a>" 
                          (plist-get plist :raw-link) (plist-get plist :raw-link)))
                        ;; all other links will be formatted as <pre> blocks on the raw link
                        (t (format "<pre>%s</pre>" (plist-get plist :raw-link)))))))))
(princ url-list)
;(princ (org-element-parse-buffer))
(<a href="/media/Changing-links-to-files-so-they work-in-a-blog/news.org">news.org</a> <a href="/media/Changing-links-to-files-so-they work-in-a-blog/news.org">news.org</a> <a href="/media/Changing-links-to-files-so-they work-in-a-blog/news.org">Org-file containing news</a> <pre>elisp:(princ "test")</pre> <img src="/media/Changing-links-to-files-so-they work-in-a-blog/tooltip-emacs.png"> <a href="/media/Changing-links-to-files-so-they work-in-a-blog/fabfile.py">fabfile.py</a> <a href="http://orgmode.org/worg/exporters/filter-markup.html">http://orgmode.org/worg/exporters/filter-markup.html</a> <a href="https://github.com/jkitchin/jmax/blob/prelude/blogofile.el">https://github.com/jkitchin/jmax/blob/prelude/blogofile.el</a>)

Now, we are ready to consider the filter. Again, building off of http://orgmode.org/worg/exporters/filter-markup.htmlwe are going to setup a counter, increment the counter in the filter, and get the nth url from the list we constructed above, and replace the text if the URL is not nil.

(let ((counter 0))

  (defun ox-mrkup-filter-link (text back-end info)
    (let ((url (nth counter url-list)))
      (if (not (string= url "nil")) (setq output   (format "%s" url))
        (setq output (format "%s" text)))
      (setq counter (+ counter 1))
      output))

  (let ((org-export-filter-link-functions '(ox-mrkup-filter-link))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    ; now get the output into the org output
    (switch-to-buffer "*Org HTML Export*")

    (setq HTML (buffer-string))
    (setq YAML "---
title: Changing links to files so they work in a blog
date: 2013/09/28 17:49:00
updated: 2013/09/29 08:14:00
categories: org-mode
---



")
  (with-temp-buffer
(insert YAML)
(insert HTML)
(write-region (point-min) (point-max) "../_posts/2013-09-28-Changing-links-to-files-so-they-work-in-a-blog.html")))
(princ "Done.")
Done.

You can see from this that we were able to process the org-buffer to get all the links, identify which of those links were external files that were not images (png files), and copy those files to our blog directory. Finally, we used a filter to modify the link text to point towards the blog url for publishing to HTML. I don't have this fully automated in https://github.com/jkitchin/jmax/blob/prelude/blogofile.el, but now all the pieces are done to enable that one day!

Discuss on Twitter

Customizing export of code blocks in HTML

| categories: org-mode | tags:

1 Customizing the export of org-mode elements

In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in.

I used a simple method with a filter to do all that in Latex, but when I looked into doing the same thing for HTML, I realized the simple method would not work. It worked in latex because the rendered text block was the literal code wrapped in the minted latex environment. So, I could just strip off the first and last line of the rendered text and save it to a file. Code rendered in HTML has lots of tags in it, and it is not practical to strip these out. So, we have to go back to the drawing board.

This is just one code block for example.

for i in range(3):
    print i**2
Open the code
0
1
4

1.1 Parsing the org-file

One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.

Here is some code in emacs-lisp that does that.

(let ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (lambda (src-block) 
      (setq language (org-element-property :language src-block))
      (setq code (org-element-property :value src-block))
      (setq filename (cond ((string= language "python") (format "../media/py-%d.py" counter))
                           ((string= language "emacs-lisp") (format "../media/el-%d.elisp" counter))))
      (setq counter (+ counter 1))
      (with-temp-buffer
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format "Wrote %s\n" filename)))))
Open the code
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp

Now, we have saved each code block into a file. The next step is inserting links to the files in published HTML files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code.

It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file, and finally returning the url where that file will be served from. Code files will be served from /media in the blog.

Here is a function to save the nth code block and get its url.

(defun save-nth-src-block (nth parsetree)
"write out the nth codeblock to a file. returns the filename written to."
(catch 'url
  (let ((counter 0))
    (org-element-map parsetree 'src-block 
      (lambda (src-block) 
        (if (= nth counter)
            (progn
              (setq language (org-element-property :language src-block))
              (setq code (org-element-property :value src-block))
              (setq filename (cond ((string= language "python") (format "py-%d.py" counter))
                                   ((string= language "emacs-lisp") (format "el-%d.elisp" counter))))
              
              (with-temp-buffer
                (insert code)
                (write-region (point-min) (point-max) (concat "../media/" filename)))
              (throw 'url (concat "/media/" filename)))
        ;; update counter              
        (setq counter (+ counter 1))))))))
Open the code

Let us test that out.

(princ (save-nth-src-block 2 (org-element-parse-buffer)))
Open the code
/media/el-2.elisp

It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See http://orgmode.org/worg/exporters/filter-markup.html for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is HTML. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link.

So our filter will use the global counter to call our save-nth-src-block function. That function will return the URL that will serve the file, so we can add it to the text rendered for the code block. Here is the code to define the filter function, and export the HTML. We return the HTML from this code block to see where the modifcations occurred.

(let ((counter 0))
  (defun ox-mrkup-filter-src-block (text back-end info)
    (let* ((url (save-nth-src-block counter (plist-get info :parse-tree)))
          (output   (format "%s 

<a href=\"%s\">Open the code</a> 

" text url)))
      (setq counter (+ counter 1))
      output))

  (let ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    ; now get the output into the org output
    (switch-to-buffer "*Org HTML Export*")
    (buffer-string))
Open the code
<div id="table-of-contents">
<h2>Table of Contents</h2>
<div id="text-table-of-contents">
<ul>
<li><a href="#sec-1">1. Customizing the export of org-mode elements</a>
<ul>
<li><a href="#sec-1-1">1.1. Parsing the org-file</a></li>
</ul>
</li>
</ul>
</div>
</div>
<div id="outline-container-sec-1" class="outline-2">
<h2 id="sec-1"><span class="section-number-2">1</span> Customizing the export of org-mode elements</h2>
<div class="outline-text-2" id="text-1">
<p>
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
</p>

<p>
I used a simple method with a filter to do all that in Latex, but when I looked into doing the same thing for HTML, I realized the simple method would not work. It worked in latex because the rendered text block was the literal code wrapped in the minted latex environment. So, I could just strip off the first and last line of the rendered text and save it to a file. Code rendered in HTML has lots of tags in it, and it is not practical to strip these out. So, we have to go back to the drawing board.
</p>

<p>
This is just one code block for example. 
</p>

<div class="org-src-container">

<pre class="src src-python"><span style="color: #8b0000;">for</span> i <span style="color: #8b0000;">in</span> <span style="color: #cd0000;">range</span>(3):
    <span style="color: #8b0000;">print</span> i**2
</pre>
</div>

 

<a href="/media/py-0.py">Open the code</a> 

<pre class="example">
0
1
4
</pre>
</div>


<div id="outline-container-sec-1-1" class="outline-3">
<h3 id="sec-1-1"><span class="section-number-3">1.1</span> Parsing the org-file</h3>
<div class="outline-text-3" id="text-1-1">
<p>
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
</p>

<p>
Here is some code in emacs-lisp that does that.
</p>

<div class="org-src-container">

<pre class="src src-emacs-lisp">(<span style="color: #8b0000;">let</span> ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (<span style="color: #8b0000;">lambda</span> (src-block) 
      (setq language (org-element-property <span style="color: #cd0000;">:language</span> src-block))
      (setq code (org-element-property <span style="color: #cd0000;">:value</span> src-block))
      (setq filename (<span style="color: #8b0000;">cond</span> ((string= language <span style="color: #228b22;">"python"</span>) (format <span style="color: #228b22;">"../media/py-%d.py"</span> counter))
                           ((string= language <span style="color: #228b22;">"emacs-lisp"</span>) (format <span style="color: #228b22;">"../media/el-%d.elisp"</span> counter))))
      (setq counter (+ counter 1))
      (<span style="color: #8b0000;">with-temp-buffer</span>
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format <span style="color: #228b22;">"Wrote %s\n"</span> filename)))))
</pre>
</div>


 

<a href="/media/el-1.elisp">Open the code</a> 

<pre class="example">
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
</pre>

<p>
Now, we have saved each code block into a file. The next step is inserting links to the files in published HTML files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
</p>

<p>
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file, and finally returning the url where that file will be served from. Code files will be served from /media in the blog.
</p>

<p>
Here is a function to save the nth code block and get its url.
</p>

<div class="org-src-container">

<pre class="src src-emacs-lisp">(<span style="color: #8b0000;">defun</span> <span style="color: #8b2323;">save-nth-src-block</span> (nth parsetree)
<span style="color: #228b22;">"write out the nth codeblock to a file. returns the filename written to."</span>
(<span style="color: #8b0000;">catch</span> '<span style="color: #cd0000;">url</span>
  (<span style="color: #8b0000;">let</span> ((counter 0))
    (org-element-map parsetree 'src-block 
      (<span style="color: #8b0000;">lambda</span> (src-block) 
        (<span style="color: #8b0000;">if</span> (= nth counter)
            (<span style="color: #8b0000;">progn</span>
              (setq language (org-element-property <span style="color: #cd0000;">:language</span> src-block))
              (setq code (org-element-property <span style="color: #cd0000;">:value</span> src-block))
              (setq filename (<span style="color: #8b0000;">cond</span> ((string= language <span style="color: #228b22;">"python"</span>) (format <span style="color: #228b22;">"py-%d.py"</span> counter))
                                   ((string= language <span style="color: #228b22;">"emacs-lisp"</span>) (format <span style="color: #228b22;">"el-%d.elisp"</span> counter))))
              
              (<span style="color: #8b0000;">with-temp-buffer</span>
                (insert code)
                (write-region (point-min) (point-max) (concat <span style="color: #228b22;">"../media/"</span> filename)))
              (<span style="color: #8b0000;">throw</span> '<span style="color: #cd0000;">url</span> (concat <span style="color: #228b22;">"/media/"</span> filename)))
        <span style="color: #ff0000; font-weight: bold;">;; </span><span style="color: #ff0000; font-weight: bold;">update counter              </span>
        (setq counter (+ counter 1))))))))
</pre>
</div>

 

<a href="/media/el-2.elisp">Open the code</a> 

<p>
Let us test that out.
</p>

<div class="org-src-container">

<pre class="src src-emacs-lisp">(princ (save-nth-src-block 2 (org-element-parse-buffer)))
</pre>
</div>

 

<a href="/media/el-3.elisp">Open the code</a> 

<pre class="example">
/media/el-2.elisp
</pre>

<p>
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See <a href="http://orgmode.org/worg/exporters/filter-markup.html">http://orgmode.org/worg/exporters/filter-markup.html</a> for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is HTML. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
</p>

<p>
So our filter will use the global counter to call our save-nth-src-block function. That function will return the URL that will serve the file, so we can add it to the text rendered for the code block. Here is the code to define the filter function, and export the HTML. We return the HTML from this code block to see where the modifcations occurred.
</p>

<div class="org-src-container">

<pre class="src src-emacs-lisp">(<span style="color: #8b0000;">let</span> ((counter 0))
  (<span style="color: #8b0000;">defun</span> <span style="color: #8b2323;">ox-mrkup-filter-src-block</span> (text back-end info)
    (<span style="color: #8b0000;">let*</span> ((url (save-nth-src-block counter (plist-get info <span style="color: #cd0000;">:parse-tree</span>)))
          (output   (format <span style="color: #228b22;">"%s </span>

<span style="color: #228b22;">&lt;a href=\"%s\"&gt;Open the code&lt;/a&gt; </span>

<span style="color: #228b22;">"</span> text url)))
      (setq counter (+ counter 1))
      output))

  (<span style="color: #8b0000;">let</span> ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    <span style="color: #ff0000; font-weight: bold;">; </span><span style="color: #ff0000; font-weight: bold;">now get the output into the org output</span>
    (switch-to-buffer <span style="color: #228b22;">"*Org HTML Export*"</span>)
    (buffer-string))
</pre>
</div>

 

<a href="/media/el-4.elisp">Open the code</a> 

<pre class="example">
&lt;div id="table-of-contents"&gt;
&lt;h2&gt;Table of Contents&lt;/h2&gt;
&lt;div id="text-table-of-contents"&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#sec-1"&gt;1. Customizing the export of org-mode elements&lt;/a&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href="#sec-1-1"&gt;1.1. Parsing the org-file&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;div id="outline-container-sec-1" class="outline-2"&gt;
&lt;h2 id="sec-1"&gt;&lt;span class="section-number-2"&gt;1&lt;/span&gt; Customizing the export of org-mode elements&lt;/h2&gt;
&lt;div class="outline-text-2" id="text-1"&gt;
&lt;p&gt;
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-python"&gt;&lt;span style="color: #8b0000;"&gt;for&lt;/span&gt; i &lt;span style="color: #8b0000;"&gt;in&lt;/span&gt; &lt;span style="color: #cd0000;"&gt;range&lt;/span&gt;(3):
    &lt;span style="color: #8b0000;"&gt;print&lt;/span&gt; i**2
&lt;/pre&gt;
&lt;/div&gt;

 

&lt;a href="/media/py-0.py"&gt;Open the code&lt;/a&gt; 

&lt;pre class="example"&gt;
0
1
4
&lt;/pre&gt;
&lt;/div&gt;


&lt;div id="outline-container-sec-1-1" class="outline-3"&gt;
&lt;h3 id="sec-1-1"&gt;&lt;span class="section-number-3"&gt;1.1&lt;/span&gt; Parsing the org-file&lt;/h3&gt;
&lt;div class="outline-text-3" id="text-1-1"&gt;
&lt;p&gt;
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (&lt;span style="color: #8b0000;"&gt;lambda&lt;/span&gt; (src-block) 
      (setq language (org-element-property &lt;span style="color: #cd0000;"&gt;:language&lt;/span&gt; src-block))
      (setq code (org-element-property &lt;span style="color: #cd0000;"&gt;:value&lt;/span&gt; src-block))
      (setq filename (&lt;span style="color: #8b0000;"&gt;cond&lt;/span&gt; ((string= language &lt;span style="color: #228b22;"&gt;"python"&lt;/span&gt;) (format &lt;span style="color: #228b22;"&gt;"../media/py-%d.py"&lt;/span&gt; counter))
                           ((string= language &lt;span style="color: #228b22;"&gt;"emacs-lisp"&lt;/span&gt;) (format &lt;span style="color: #228b22;"&gt;"../media/el-%d.elisp"&lt;/span&gt; counter))))
      (setq counter (+ counter 1))
      (&lt;span style="color: #8b0000;"&gt;with-temp-buffer&lt;/span&gt;
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format &lt;span style="color: #228b22;"&gt;"Wrote %s\n"&lt;/span&gt; filename)))))
&lt;/pre&gt;
&lt;/div&gt;


 

&lt;a href="/media/el-1.elisp"&gt;Open the code&lt;/a&gt; 

&lt;pre class="example"&gt;
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
&lt;/pre&gt;

&lt;p&gt;
Now, we have saved each code block into a file. The next step is inserting links to the files in published html files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
&lt;/p&gt;

&lt;p&gt;
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file. Then, as we go through the 
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;defun&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;save-nth-src-block&lt;/span&gt; (nth parsetree)
&lt;span style="color: #228b22;"&gt;"write out the nth codeblock to a file. returns the filename written to."&lt;/span&gt;
(&lt;span style="color: #8b0000;"&gt;catch&lt;/span&gt; '&lt;span style="color: #cd0000;"&gt;url&lt;/span&gt;
  (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((counter 0))
    (org-element-map parsetree 'src-block 
      (&lt;span style="color: #8b0000;"&gt;lambda&lt;/span&gt; (src-block) 
        (&lt;span style="color: #8b0000;"&gt;if&lt;/span&gt; (= nth counter)
            (&lt;span style="color: #8b0000;"&gt;progn&lt;/span&gt;
              (setq language (org-element-property &lt;span style="color: #cd0000;"&gt;:language&lt;/span&gt; src-block))
              (setq code (org-element-property &lt;span style="color: #cd0000;"&gt;:value&lt;/span&gt; src-block))
              (setq filename (&lt;span style="color: #8b0000;"&gt;cond&lt;/span&gt; ((string= language &lt;span style="color: #228b22;"&gt;"python"&lt;/span&gt;) (format &lt;span style="color: #228b22;"&gt;"py-%d.py"&lt;/span&gt; counter))
                                   ((string= language &lt;span style="color: #228b22;"&gt;"emacs-lisp"&lt;/span&gt;) (format &lt;span style="color: #228b22;"&gt;"el-%d.elisp"&lt;/span&gt; counter))))
              
              (&lt;span style="color: #8b0000;"&gt;with-temp-buffer&lt;/span&gt;
                (insert code)
                (write-region (point-min) (point-max) (concat &lt;span style="color: #228b22;"&gt;"../media/"&lt;/span&gt; filename)))
              (&lt;span style="color: #8b0000;"&gt;throw&lt;/span&gt; '&lt;span style="color: #cd0000;"&gt;url&lt;/span&gt; (concat &lt;span style="color: #228b22;"&gt;"/media/"&lt;/span&gt; filename)))
        &lt;span style="color: #ff0000; font-weight: bold;"&gt;;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;update counter              &lt;/span&gt;
        (setq counter (+ counter 1))))))))
&lt;/pre&gt;
&lt;/div&gt;

 

&lt;a href="/media/el-2.elisp"&gt;Open the code&lt;/a&gt; 

&lt;p&gt;
Let us test that out.
&lt;/p&gt;

&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(princ (save-nth-src-block 2 (org-element-parse-buffer)))
&lt;/pre&gt;
&lt;/div&gt;

 

&lt;a href="/media/el-3.elisp"&gt;Open the code&lt;/a&gt; 

&lt;pre class="example"&gt;
/media/el-2.elisp
&lt;/pre&gt;

&lt;p&gt;
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See &lt;a href="http://orgmode.org/worg/exporters/filter-markup.html"&gt;http://orgmode.org/worg/exporters/filter-markup.html&lt;/a&gt; for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is html. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
&lt;/p&gt;

&lt;p&gt;
So our filter will use the global counter to call our save-nth-src-block function. That function will return the filename the codeblock was saved to. That is not the same as the path on the blog, 
&lt;/p&gt;



&lt;div class="org-src-container"&gt;

&lt;pre class="src src-emacs-lisp"&gt;(&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((counter 0))
  (&lt;span style="color: #8b0000;"&gt;defun&lt;/span&gt; &lt;span style="color: #8b2323;"&gt;ox-mrkup-filter-src-block&lt;/span&gt; (text back-end info)
    (&lt;span style="color: #8b0000;"&gt;let*&lt;/span&gt; ((url (save-nth-src-block counter (plist-get info &lt;span style="color: #cd0000;"&gt;:parse-tree&lt;/span&gt;)))
          (output   (format &lt;span style="color: #228b22;"&gt;"%s &lt;/span&gt;

&lt;span style="color: #228b22;"&gt;&amp;lt;a href=\"%s\"&amp;gt;Open the code&amp;lt;/a&amp;gt; &lt;/span&gt;

&lt;span style="color: #228b22;"&gt;"&lt;/span&gt; text url)))
      (setq counter (+ counter 1))
      output))

  (&lt;span style="color: #8b0000;"&gt;let&lt;/span&gt; ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    &lt;span style="color: #ff0000; font-weight: bold;"&gt;; &lt;/span&gt;&lt;span style="color: #ff0000; font-weight: bold;"&gt;now get the output into the org output&lt;/span&gt;
    (switch-to-buffer &lt;span style="color: #228b22;"&gt;"*Org HTML Export*"&lt;/span&gt;)
    (buffer-string))
&lt;/pre&gt;
&lt;/div&gt;

 

&lt;a href="/media/el-4.elisp"&gt;Open the code&lt;/a&gt; 

&lt;pre class="example"&gt;
&amp;lt;div id="table-of-contents"&amp;gt;
&amp;lt;h2&amp;gt;Table of Contents&amp;lt;/h2&amp;gt;
&amp;lt;div id="text-table-of-contents"&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href="#sec-1"&amp;gt;1. Customizing the export of org-mode elements&amp;lt;/a&amp;gt;
&amp;lt;ul&amp;gt;
&amp;lt;li&amp;gt;&amp;lt;a href="#sec-1-1"&amp;gt;1.1. Parsing the org-file&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/li&amp;gt;
&amp;lt;/ul&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;div id="outline-container-sec-1" class="outline-2"&amp;gt;
&amp;lt;h2 id="sec-1"&amp;gt;&amp;lt;span class="section-number-2"&amp;gt;1&amp;lt;/span&amp;gt; Customizing the export of org-mode elements&amp;lt;/h2&amp;gt;
&amp;lt;div class="outline-text-2" id="text-1"&amp;gt;
&amp;lt;p&amp;gt;
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
&amp;lt;/p&amp;gt;

&amp;lt;div class="org-src-container"&amp;gt;

&amp;lt;pre class="src src-python"&amp;gt;&amp;lt;span style="color: #8b0000;"&amp;gt;for&amp;lt;/span&amp;gt; i &amp;lt;span style="color: #8b0000;"&amp;gt;in&amp;lt;/span&amp;gt; &amp;lt;span style="color: #cd0000;"&amp;gt;range&amp;lt;/span&amp;gt;(3):
    &amp;lt;span style="color: #8b0000;"&amp;gt;print&amp;lt;/span&amp;gt; i**2
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;

 

&amp;lt;a href="/media/py-0.py"&amp;gt;Open the code&amp;lt;/a&amp;gt; 

&amp;lt;pre class="example"&amp;gt;
0
1
4
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;


&amp;lt;div id="outline-container-sec-1-1" class="outline-3"&amp;gt;
&amp;lt;h3 id="sec-1-1"&amp;gt;&amp;lt;span class="section-number-3"&amp;gt;1.1&amp;lt;/span&amp;gt; Parsing the org-file&amp;lt;/h3&amp;gt;
&amp;lt;div class="outline-text-3" id="text-1-1"&amp;gt;
&amp;lt;p&amp;gt;
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
&amp;lt;/p&amp;gt;

&amp;lt;div class="org-src-container"&amp;gt;

&amp;lt;pre class="src src-emacs-lisp"&amp;gt;(&amp;lt;span style="color: #8b0000;"&amp;gt;let&amp;lt;/span&amp;gt; ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (&amp;lt;span style="color: #8b0000;"&amp;gt;lambda&amp;lt;/span&amp;gt; (src-block) 
      (setq language (org-element-property &amp;lt;span style="color: #cd0000;"&amp;gt;:language&amp;lt;/span&amp;gt; src-block))
      (setq code (org-element-property &amp;lt;span style="color: #cd0000;"&amp;gt;:value&amp;lt;/span&amp;gt; src-block))
      (setq filename (&amp;lt;span style="color: #8b0000;"&amp;gt;cond&amp;lt;/span&amp;gt; ((string= language &amp;lt;span style="color: #228b22;"&amp;gt;"python"&amp;lt;/span&amp;gt;) (format &amp;lt;span style="color: #228b22;"&amp;gt;"../media/py-%d.py"&amp;lt;/span&amp;gt; counter))
                           ((string= language &amp;lt;span style="color: #228b22;"&amp;gt;"emacs-lisp"&amp;lt;/span&amp;gt;) (format &amp;lt;span style="color: #228b22;"&amp;gt;"../media/el-%d.elisp"&amp;lt;/span&amp;gt; counter))))
      (setq counter (+ counter 1))
      (&amp;lt;span style="color: #8b0000;"&amp;gt;with-temp-buffer&amp;lt;/span&amp;gt;
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format &amp;lt;span style="color: #228b22;"&amp;gt;"Wrote %s\n"&amp;lt;/span&amp;gt; filename)))))
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;


 

&amp;lt;a href="/media/el-1.elisp"&amp;gt;Open the code&amp;lt;/a&amp;gt; 

&amp;lt;pre class="example"&amp;gt;
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
Now, we have saved each code block into a file. The next step is inserting links to the files in published html files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file. Then, as we go through the 
&amp;lt;/p&amp;gt;

&amp;lt;div class="org-src-container"&amp;gt;

&amp;lt;pre class="src src-emacs-lisp"&amp;gt;(&amp;lt;span style="color: #8b0000;"&amp;gt;defun&amp;lt;/span&amp;gt; &amp;lt;span style="color: #8b2323;"&amp;gt;save-nth-src-block&amp;lt;/span&amp;gt; (nth parsetree)
&amp;lt;span style="color: #228b22;"&amp;gt;"write out the nth codeblock to a file. returns the filename written to."&amp;lt;/span&amp;gt;
(&amp;lt;span style="color: #8b0000;"&amp;gt;catch&amp;lt;/span&amp;gt; '&amp;lt;span style="color: #cd0000;"&amp;gt;url&amp;lt;/span&amp;gt;
  (&amp;lt;span style="color: #8b0000;"&amp;gt;let&amp;lt;/span&amp;gt; ((counter 0))
    (org-element-map parsetree 'src-block 
      (&amp;lt;span style="color: #8b0000;"&amp;gt;lambda&amp;lt;/span&amp;gt; (src-block) 
        (&amp;lt;span style="color: #8b0000;"&amp;gt;if&amp;lt;/span&amp;gt; (= nth counter)
            (&amp;lt;span style="color: #8b0000;"&amp;gt;progn&amp;lt;/span&amp;gt;
              (setq language (org-element-property &amp;lt;span style="color: #cd0000;"&amp;gt;:language&amp;lt;/span&amp;gt; src-block))
              (setq code (org-element-property &amp;lt;span style="color: #cd0000;"&amp;gt;:value&amp;lt;/span&amp;gt; src-block))
              (setq filename (&amp;lt;span style="color: #8b0000;"&amp;gt;cond&amp;lt;/span&amp;gt; ((string= language &amp;lt;span style="color: #228b22;"&amp;gt;"python"&amp;lt;/span&amp;gt;) (format &amp;lt;span style="color: #228b22;"&amp;gt;"py-%d.py"&amp;lt;/span&amp;gt; counter))
                                   ((string= language &amp;lt;span style="color: #228b22;"&amp;gt;"emacs-lisp"&amp;lt;/span&amp;gt;) (format &amp;lt;span style="color: #228b22;"&amp;gt;"el-%d.elisp"&amp;lt;/span&amp;gt; counter))))
              
              (&amp;lt;span style="color: #8b0000;"&amp;gt;with-temp-buffer&amp;lt;/span&amp;gt;
                (insert code)
                (write-region (point-min) (point-max) (concat &amp;lt;span style="color: #228b22;"&amp;gt;"../media/"&amp;lt;/span&amp;gt; filename)))
              (&amp;lt;span style="color: #8b0000;"&amp;gt;throw&amp;lt;/span&amp;gt; '&amp;lt;span style="color: #cd0000;"&amp;gt;url&amp;lt;/span&amp;gt; (concat &amp;lt;span style="color: #228b22;"&amp;gt;"/media/"&amp;lt;/span&amp;gt; filename)))
        &amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;gt;;; &amp;lt;/span&amp;gt;&amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;gt;update counter              &amp;lt;/span&amp;gt;
        (setq counter (+ counter 1))))))))
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;

 

&amp;lt;a href="/media/el-2.elisp"&amp;gt;Open the code&amp;lt;/a&amp;gt; 

&amp;lt;p&amp;gt;
Let us test that out.
&amp;lt;/p&amp;gt;

&amp;lt;div class="org-src-container"&amp;gt;

&amp;lt;pre class="src src-emacs-lisp"&amp;gt;(princ (save-nth-src-block 2 (org-element-parse-buffer)))
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;

 

&amp;lt;a href="/media/el-3.elisp"&amp;gt;Open the code&amp;lt;/a&amp;gt; 

&amp;lt;pre class="example"&amp;gt;
/media/el-2.elisp
&amp;lt;/pre&amp;gt;

&amp;lt;p&amp;gt;
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See &amp;lt;a href="http://orgmode.org/worg/exporters/filter-markup.html"&amp;gt;http://orgmode.org/worg/exporters/filter-markup.html&amp;lt;/a&amp;gt; for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is html. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
&amp;lt;/p&amp;gt;

&amp;lt;p&amp;gt;
So our filter will use the global counter to call our save-nth-src-block function. That function will return the filename the codeblock was saved to. That is not the same as the path on the blog, 
&amp;lt;/p&amp;gt;



&amp;lt;div class="org-src-container"&amp;gt;

&amp;lt;pre class="src src-emacs-lisp"&amp;gt;(&amp;lt;span style="color: #8b0000;"&amp;gt;let&amp;lt;/span&amp;gt; ((counter 0))
  (&amp;lt;span style="color: #8b0000;"&amp;gt;defun&amp;lt;/span&amp;gt; &amp;lt;span style="color: #8b2323;"&amp;gt;ox-mrkup-filter-src-block&amp;lt;/span&amp;gt; (text back-end info)
    (&amp;lt;span style="color: #8b0000;"&amp;gt;let*&amp;lt;/span&amp;gt; ((url (save-nth-src-block counter (plist-get info &amp;lt;span style="color: #cd0000;"&amp;gt;:parse-tree&amp;lt;/span&amp;gt;)))
          (output   (format &amp;lt;span style="color: #228b22;"&amp;gt;"%s &amp;lt;/span&amp;gt;

&amp;lt;span style="color: #228b22;"&amp;gt;&amp;amp;lt;a href=\"%s\"&amp;amp;gt;Open the code&amp;amp;lt;/a&amp;amp;gt; &amp;lt;/span&amp;gt;

&amp;lt;span style="color: #228b22;"&amp;gt;"&amp;lt;/span&amp;gt; text url)))
      (setq counter (+ counter 1))
      output))

  (&amp;lt;span style="color: #8b0000;"&amp;gt;let&amp;lt;/span&amp;gt; ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    &amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;gt;; &amp;lt;/span&amp;gt;&amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;gt;now get the output into the org output&amp;lt;/span&amp;gt;
    (switch-to-buffer &amp;lt;span style="color: #228b22;"&amp;gt;"*Org HTML Export*"&amp;lt;/span&amp;gt;)
    (buffer-string))
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;

 

&amp;lt;a href="/media/el-4.elisp"&amp;gt;Open the code&amp;lt;/a&amp;gt; 

&amp;lt;pre class="example"&amp;gt;
&amp;amp;lt;div id="table-of-contents"&amp;amp;gt;
&amp;amp;lt;h2&amp;amp;gt;Table of Contents&amp;amp;lt;/h2&amp;amp;gt;
&amp;amp;lt;div id="text-table-of-contents"&amp;amp;gt;
&amp;amp;lt;ul&amp;amp;gt;
&amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;a href="#sec-1"&amp;amp;gt;1. Customizing the export of org-mode elements&amp;amp;lt;/a&amp;amp;gt;
&amp;amp;lt;ul&amp;amp;gt;
&amp;amp;lt;li&amp;amp;gt;&amp;amp;lt;a href="#sec-1-1"&amp;amp;gt;1.1. Parsing the org-file&amp;amp;lt;/a&amp;amp;gt;&amp;amp;lt;/li&amp;amp;gt;
&amp;amp;lt;/ul&amp;amp;gt;
&amp;amp;lt;/li&amp;amp;gt;
&amp;amp;lt;/ul&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;
&amp;amp;lt;div id="outline-container-sec-1" class="outline-2"&amp;amp;gt;
&amp;amp;lt;h2 id="sec-1"&amp;amp;gt;&amp;amp;lt;span class="section-number-2"&amp;amp;gt;1&amp;amp;lt;/span&amp;amp;gt; Customizing the export of org-mode elements&amp;amp;lt;/h2&amp;amp;gt;
&amp;amp;lt;div class="outline-text-2" id="text-1"&amp;amp;gt;
&amp;amp;lt;p&amp;amp;gt;
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
&amp;amp;lt;/p&amp;amp;gt;

&amp;amp;lt;div class="org-src-container"&amp;amp;gt;

&amp;amp;lt;pre class="src src-python"&amp;amp;gt;&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;for&amp;amp;lt;/span&amp;amp;gt; i &amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;in&amp;amp;lt;/span&amp;amp;gt; &amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;range&amp;amp;lt;/span&amp;amp;gt;(3):
    &amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;print&amp;amp;lt;/span&amp;amp;gt; i**2
&amp;amp;lt;/pre&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;

 

&amp;amp;lt;a href="py-0.py"&amp;amp;gt;Open the code&amp;amp;lt;/a&amp;amp;gt; 

&amp;amp;lt;pre class="example"&amp;amp;gt;
0
1
4
&amp;amp;lt;/pre&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;


&amp;amp;lt;div id="outline-container-sec-1-1" class="outline-3"&amp;amp;gt;
&amp;amp;lt;h3 id="sec-1-1"&amp;amp;gt;&amp;amp;lt;span class="section-number-3"&amp;amp;gt;1.1&amp;amp;lt;/span&amp;amp;gt; Parsing the org-file&amp;amp;lt;/h3&amp;amp;gt;
&amp;amp;lt;div class="outline-text-3" id="text-1-1"&amp;amp;gt;
&amp;amp;lt;p&amp;amp;gt;
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
&amp;amp;lt;/p&amp;amp;gt;

&amp;amp;lt;div class="org-src-container"&amp;amp;gt;

&amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;gt;(&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;let&amp;amp;lt;/span&amp;amp;gt; ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;lambda&amp;amp;lt;/span&amp;amp;gt; (src-block) 
      (setq language (org-element-property &amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;:language&amp;amp;lt;/span&amp;amp;gt; src-block))
      (setq code (org-element-property &amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;:value&amp;amp;lt;/span&amp;amp;gt; src-block))
      (setq filename (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;cond&amp;amp;lt;/span&amp;amp;gt; ((string= language &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"python"&amp;amp;lt;/span&amp;amp;gt;) (format &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"../media/py-%d.py"&amp;amp;lt;/span&amp;amp;gt; counter))
                           ((string= language &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"emacs-lisp"&amp;amp;lt;/span&amp;amp;gt;) (format &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"../media/el-%d.elisp"&amp;amp;lt;/span&amp;amp;gt; counter))))
      (setq counter (+ counter 1))
      (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;with-temp-buffer&amp;amp;lt;/span&amp;amp;gt;
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"Wrote %s\n"&amp;amp;lt;/span&amp;amp;gt; filename)))))
&amp;amp;lt;/pre&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;


 

&amp;amp;lt;a href="el-1.elisp"&amp;amp;gt;Open the code&amp;amp;lt;/a&amp;amp;gt; 

&amp;amp;lt;pre class="example"&amp;amp;gt;
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
&amp;amp;lt;/pre&amp;amp;gt;

&amp;amp;lt;p&amp;amp;gt;
Now, we have saved each code block into a file. The next step is inserting links to the files in published html files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
&amp;amp;lt;/p&amp;amp;gt;

&amp;amp;lt;p&amp;amp;gt;
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file. Then, as we go through the 
&amp;amp;lt;/p&amp;amp;gt;

&amp;amp;lt;div class="org-src-container"&amp;amp;gt;

&amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;gt;(&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;defun&amp;amp;lt;/span&amp;amp;gt; &amp;amp;lt;span style="color: #8b2323;"&amp;amp;gt;save-nth-src-block&amp;amp;lt;/span&amp;amp;gt; (nth parsetree)
&amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"write out the nth codeblock to a file. returns the filename written to."&amp;amp;lt;/span&amp;amp;gt;
(&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;catch&amp;amp;lt;/span&amp;amp;gt; '&amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;url&amp;amp;lt;/span&amp;amp;gt;
  (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;let&amp;amp;lt;/span&amp;amp;gt; ((counter 0))
    (org-element-map parsetree 'src-block 
      (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;lambda&amp;amp;lt;/span&amp;amp;gt; (src-block) 
        (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;if&amp;amp;lt;/span&amp;amp;gt; (= nth counter)
            (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;progn&amp;amp;lt;/span&amp;amp;gt;
              (setq language (org-element-property &amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;:language&amp;amp;lt;/span&amp;amp;gt; src-block))
              (setq code (org-element-property &amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;:value&amp;amp;lt;/span&amp;amp;gt; src-block))
              (setq filename (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;cond&amp;amp;lt;/span&amp;amp;gt; ((string= language &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"python"&amp;amp;lt;/span&amp;amp;gt;) (format &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"py-%d.py"&amp;amp;lt;/span&amp;amp;gt; counter))
                                   ((string= language &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"emacs-lisp"&amp;amp;lt;/span&amp;amp;gt;) (format &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"el-%d.elisp"&amp;amp;lt;/span&amp;amp;gt; counter))))
              
              (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;with-temp-buffer&amp;amp;lt;/span&amp;amp;gt;
                (insert code)
                (write-region (point-min) (point-max) (concat &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"../media/"&amp;amp;lt;/span&amp;amp;gt; filename)))
              (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;throw&amp;amp;lt;/span&amp;amp;gt; '&amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;url&amp;amp;lt;/span&amp;amp;gt; (concat &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"/media/"&amp;amp;lt;/span&amp;amp;gt; filename)))
        &amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;gt;;; &amp;amp;lt;/span&amp;amp;gt;&amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;gt;update counter              &amp;amp;lt;/span&amp;amp;gt;
        (setq counter (+ counter 1))))))))
&amp;amp;lt;/pre&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;

 

&amp;amp;lt;a href="el-2.elisp"&amp;amp;gt;Open the code&amp;amp;lt;/a&amp;amp;gt; 

&amp;amp;lt;p&amp;amp;gt;
Let us test that out.
&amp;amp;lt;/p&amp;amp;gt;

&amp;amp;lt;div class="org-src-container"&amp;amp;gt;

&amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;gt;(princ (save-nth-src-block 2 (org-element-parse-buffer)))
&amp;amp;lt;/pre&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;

 

&amp;amp;lt;a href="el-3.elisp"&amp;amp;gt;Open the code&amp;amp;lt;/a&amp;amp;gt; 

&amp;amp;lt;pre class="example"&amp;amp;gt;
/media/el-2.elisp
&amp;amp;lt;/pre&amp;amp;gt;

&amp;amp;lt;p&amp;amp;gt;
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See &amp;amp;lt;a href="http://orgmode.org/worg/exporters/filter-markup.html"&amp;amp;gt;http://orgmode.org/worg/exporters/filter-markup.html&amp;amp;lt;/a&amp;amp;gt; for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is html. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
&amp;amp;lt;/p&amp;amp;gt;

&amp;amp;lt;p&amp;amp;gt;
So our filter will use the global counter to call our save-nth-src-block function. That function will return the filename the codeblock was saved to. That is not the same as the path on the blog, 
&amp;amp;lt;/p&amp;amp;gt;



&amp;amp;lt;div class="org-src-container"&amp;amp;gt;

&amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;gt;(&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;let&amp;amp;lt;/span&amp;amp;gt; ((counter 0))
  (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;defun&amp;amp;lt;/span&amp;amp;gt; &amp;amp;lt;span style="color: #8b2323;"&amp;amp;gt;ox-mrkup-filter-src-block&amp;amp;lt;/span&amp;amp;gt; (text back-end info)
    (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;let&amp;amp;lt;/span&amp;amp;gt; ((filename (save-nth-src-block counter (plist-get info &amp;amp;lt;span style="color: #cd0000;"&amp;amp;gt;:parse-tree&amp;amp;lt;/span&amp;amp;gt;)))
          (output   (format &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"%s &amp;amp;lt;/span&amp;amp;gt;

&amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;&amp;amp;amp;lt;a href=\"%s\"&amp;amp;amp;gt;Open the code&amp;amp;amp;lt;/a&amp;amp;amp;gt; &amp;amp;lt;/span&amp;amp;gt;

&amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"&amp;amp;lt;/span&amp;amp;gt; text filename)))
      (setq counter (+ counter 1))
      output))

  (&amp;amp;lt;span style="color: #8b0000;"&amp;amp;gt;let&amp;amp;lt;/span&amp;amp;gt; ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    &amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;gt;; &amp;amp;lt;/span&amp;amp;gt;&amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;gt;now get the output into the org output&amp;amp;lt;/span&amp;amp;gt;
    (switch-to-buffer &amp;amp;lt;span style="color: #228b22;"&amp;amp;gt;"*Org HTML Export*"&amp;amp;lt;/span&amp;amp;gt;)
    (buffer-string))
&amp;amp;lt;/pre&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;

 

&amp;amp;lt;a href="el-4.elisp"&amp;amp;gt;Open the code&amp;amp;lt;/a&amp;amp;gt; 

&amp;amp;lt;pre class="example"&amp;amp;gt;
&amp;amp;amp;lt;div id="table-of-contents"&amp;amp;amp;gt;
&amp;amp;amp;lt;h2&amp;amp;amp;gt;Table of Contents&amp;amp;amp;lt;/h2&amp;amp;amp;gt;
&amp;amp;amp;lt;div id="text-table-of-contents"&amp;amp;amp;gt;
&amp;amp;amp;lt;ul&amp;amp;amp;gt;
&amp;amp;amp;lt;li&amp;amp;amp;gt;&amp;amp;amp;lt;a href="#sec-1"&amp;amp;amp;gt;1. Customizing the export of org-mode elements&amp;amp;amp;lt;/a&amp;amp;amp;gt;
&amp;amp;amp;lt;ul&amp;amp;amp;gt;
&amp;amp;amp;lt;li&amp;amp;amp;gt;&amp;amp;amp;lt;a href="#sec-1-1"&amp;amp;amp;gt;1.1. Parsing the org-file&amp;amp;amp;lt;/a&amp;amp;amp;gt;&amp;amp;amp;lt;/li&amp;amp;amp;gt;
&amp;amp;amp;lt;/ul&amp;amp;amp;gt;
&amp;amp;amp;lt;/li&amp;amp;amp;gt;
&amp;amp;amp;lt;/ul&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;
&amp;amp;amp;lt;div id="outline-container-sec-1" class="outline-2"&amp;amp;amp;gt;
&amp;amp;amp;lt;h2 id="sec-1"&amp;amp;amp;gt;&amp;amp;amp;lt;span class="section-number-2"&amp;amp;amp;gt;1&amp;amp;amp;lt;/span&amp;amp;amp;gt; Customizing the export of org-mode elements&amp;amp;amp;lt;/h2&amp;amp;amp;gt;
&amp;amp;amp;lt;div class="outline-text-2" id="text-1"&amp;amp;amp;gt;
&amp;amp;amp;lt;p&amp;amp;amp;gt;
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
&amp;amp;amp;lt;/p&amp;amp;amp;gt;

&amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;gt;

&amp;amp;amp;lt;pre class="src src-python"&amp;amp;amp;gt;&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;for&amp;amp;amp;lt;/span&amp;amp;amp;gt; i &amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;in&amp;amp;amp;lt;/span&amp;amp;amp;gt; &amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;gt;range&amp;amp;amp;lt;/span&amp;amp;amp;gt;(3):
    &amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;print&amp;amp;amp;lt;/span&amp;amp;amp;gt; i**2
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;

 

&amp;amp;amp;lt;a href="py-0.py"&amp;amp;amp;gt;Open the code&amp;amp;amp;lt;/a&amp;amp;amp;gt; 

&amp;amp;amp;lt;pre class="example"&amp;amp;amp;gt;
0
1
4
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;


&amp;amp;amp;lt;div id="outline-container-sec-1-1" class="outline-3"&amp;amp;amp;gt;
&amp;amp;amp;lt;h3 id="sec-1-1"&amp;amp;amp;gt;&amp;amp;amp;lt;span class="section-number-3"&amp;amp;amp;gt;1.1&amp;amp;amp;lt;/span&amp;amp;amp;gt; Parsing the org-file&amp;amp;amp;lt;/h3&amp;amp;amp;gt;
&amp;amp;amp;lt;div class="outline-text-3" id="text-1-1"&amp;amp;amp;gt;
&amp;amp;amp;lt;p&amp;amp;amp;gt;
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
&amp;amp;amp;lt;/p&amp;amp;amp;gt;

&amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;gt;

&amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;gt;(&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;let&amp;amp;amp;lt;/span&amp;amp;amp;gt; ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;lambda&amp;amp;amp;lt;/span&amp;amp;amp;gt; (src-block) 
      (setq language (org-element-property &amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;gt;:language&amp;amp;amp;lt;/span&amp;amp;amp;gt; src-block))
      (setq code (org-element-property &amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;gt;:value&amp;amp;amp;lt;/span&amp;amp;amp;gt; src-block))
      (setq filename (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;cond&amp;amp;amp;lt;/span&amp;amp;amp;gt; ((string= language &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"python"&amp;amp;amp;lt;/span&amp;amp;amp;gt;) (format &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"../media/py-%d.py"&amp;amp;amp;lt;/span&amp;amp;amp;gt; counter))
                           ((string= language &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;lt;/span&amp;amp;amp;gt;) (format &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"../media/el-%d.elisp"&amp;amp;amp;lt;/span&amp;amp;amp;gt; counter))))
      (setq counter (+ counter 1))
      (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;lt;/span&amp;amp;amp;gt;
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;lt;/span&amp;amp;amp;gt; filename)))))
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;


 

&amp;amp;amp;lt;a href="el-1.elisp"&amp;amp;amp;gt;Open the code&amp;amp;amp;lt;/a&amp;amp;amp;gt; 

&amp;amp;amp;lt;pre class="example"&amp;amp;amp;gt;
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;

&amp;amp;amp;lt;p&amp;amp;amp;gt;
Now, we have saved each code block into a file. The next step is inserting links to the files in published html files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
&amp;amp;amp;lt;/p&amp;amp;amp;gt;

&amp;amp;amp;lt;p&amp;amp;amp;gt;
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file. Then, as we go through the 
&amp;amp;amp;lt;/p&amp;amp;amp;gt;

&amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;gt;

&amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;gt;(&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;defun&amp;amp;amp;lt;/span&amp;amp;amp;gt; &amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;gt;save-nth-src-block&amp;amp;amp;lt;/span&amp;amp;amp;gt; (nth parsetree)
&amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"write out the nth codeblock to a file. returns the filename written to."&amp;amp;amp;lt;/span&amp;amp;amp;gt;
  (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;let&amp;amp;amp;lt;/span&amp;amp;amp;gt; ((counter 0))
    (org-element-map parsetree 'src-block 
      (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;lambda&amp;amp;amp;lt;/span&amp;amp;amp;gt; (src-block) 
        (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;if&amp;amp;amp;lt;/span&amp;amp;amp;gt; (= nth counter)
            (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;progn&amp;amp;amp;lt;/span&amp;amp;amp;gt;
              (setq language (org-element-property &amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;gt;:language&amp;amp;amp;lt;/span&amp;amp;amp;gt; src-block))
              (setq code (org-element-property &amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;gt;:value&amp;amp;amp;lt;/span&amp;amp;amp;gt; src-block))
              (setq filename (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;cond&amp;amp;amp;lt;/span&amp;amp;amp;gt; ((string= language &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"python"&amp;amp;amp;lt;/span&amp;amp;amp;gt;) (format &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"py-%d.py"&amp;amp;amp;lt;/span&amp;amp;amp;gt; counter))
                                   ((string= language &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;lt;/span&amp;amp;amp;gt;) (format &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"el-%d.elisp"&amp;amp;amp;lt;/span&amp;amp;amp;gt; counter))))
              
              (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;lt;/span&amp;amp;amp;gt;
                (insert code)
                (write-region (point-min) (point-max) (concat &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"../media/"&amp;amp;amp;lt;/span&amp;amp;amp;gt; filename)))
              (princ (format &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;lt;/span&amp;amp;amp;gt; filename))))
        (setq counter (+ counter 1)))))
    &amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;gt;;; &amp;amp;amp;lt;/span&amp;amp;amp;gt;&amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;gt;return the URL the file will be served at&amp;amp;amp;lt;/span&amp;amp;amp;gt;
    (concat &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"/media/"&amp;amp;amp;lt;/span&amp;amp;amp;gt; filename))
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;

 

&amp;amp;amp;lt;a href="el-2.elisp"&amp;amp;amp;gt;Open the code&amp;amp;amp;lt;/a&amp;amp;amp;gt; 

&amp;amp;amp;lt;p&amp;amp;amp;gt;
Let us test that out.
&amp;amp;amp;lt;/p&amp;amp;amp;gt;

&amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;gt;

&amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;gt;(save-nth-src-block 2 (org-element-parse-buffer))
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;

 

&amp;amp;amp;lt;a href="el-3.elisp"&amp;amp;amp;gt;Open the code&amp;amp;amp;lt;/a&amp;amp;amp;gt; 

&amp;amp;amp;lt;pre class="example"&amp;amp;amp;gt;
/media/el-2.elisp
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;

&amp;amp;amp;lt;p&amp;amp;amp;gt;
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See &amp;amp;amp;lt;a href="http://orgmode.org/worg/exporters/filter-markup.html"&amp;amp;amp;gt;http://orgmode.org/worg/exporters/filter-markup.html&amp;amp;amp;lt;/a&amp;amp;amp;gt; for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is html. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
&amp;amp;amp;lt;/p&amp;amp;amp;gt;

&amp;amp;amp;lt;p&amp;amp;amp;gt;
So our filter will use the global counter to call our save-nth-src-block function. That function will return the filename the codeblock was saved to. That is not the same as the path on the blog, 
&amp;amp;amp;lt;/p&amp;amp;amp;gt;



&amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;gt;

&amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;gt;(&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;let&amp;amp;amp;lt;/span&amp;amp;amp;gt; ((counter 0))
  (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;defun&amp;amp;amp;lt;/span&amp;amp;amp;gt; &amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;gt;ox-mrkup-filter-src-block&amp;amp;amp;lt;/span&amp;amp;amp;gt; (text back-end info)
    (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;let&amp;amp;amp;lt;/span&amp;amp;amp;gt; ((filename (save-nth-src-block counter (plist-get info &amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;gt;:parse-tree&amp;amp;amp;lt;/span&amp;amp;amp;gt;)))
          (output   (format &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"%s &amp;amp;amp;lt;/span&amp;amp;amp;gt;

&amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href=\"%s\"&amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; &amp;amp;amp;lt;/span&amp;amp;amp;gt;

&amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"&amp;amp;amp;lt;/span&amp;amp;amp;gt; text filename)))
      (setq counter (+ counter 1))
      output))

  (&amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;gt;let&amp;amp;amp;lt;/span&amp;amp;amp;gt; ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    &amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;gt;; &amp;amp;amp;lt;/span&amp;amp;amp;gt;&amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;gt;now get the output into the org output&amp;amp;amp;lt;/span&amp;amp;amp;gt;
    (switch-to-buffer &amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;gt;"*Org HTML Export*"&amp;amp;amp;lt;/span&amp;amp;amp;gt;)
    (buffer-string))
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;

 

&amp;amp;amp;lt;a href="el-4.elisp"&amp;amp;amp;gt;Open the code&amp;amp;amp;lt;/a&amp;amp;amp;gt; 

&amp;amp;amp;lt;pre class="example"&amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div id="table-of-contents"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;h2&amp;amp;amp;amp;gt;Table of Contents&amp;amp;amp;amp;lt;/h2&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div id="text-table-of-contents"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href="#sec-1"&amp;amp;amp;amp;gt;1. Customizing the export of org-mode elements&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;li&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;a href="#sec-1-1"&amp;amp;amp;amp;gt;1.1. Parsing the org-file&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div id="outline-container-sec-1" class="outline-2"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;h2 id="sec-1"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span class="section-number-2"&amp;amp;amp;amp;gt;1&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; Customizing the export of org-mode elements&amp;amp;amp;amp;lt;/h2&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="outline-text-2" id="text-1"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;pre class="src src-python"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;for&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; i &amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;in&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; &amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;gt;range&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;(3):
    &amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;print&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; i**2
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;lt;a href="py-0.py"&amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;gt;
0
1
4
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;


&amp;amp;amp;amp;lt;div id="outline-container-sec-1-1" class="outline-3"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;h3 id="sec-1-1"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span class="section-number-3"&amp;amp;amp;amp;gt;1.1&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; Parsing the org-file&amp;amp;amp;amp;lt;/h3&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;div class="outline-text-3" id="text-1-1"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;gt;(&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;let&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;lambda&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; (src-block) 
      (setq language (org-element-property &amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;gt;:language&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; src-block))
      (setq code (org-element-property &amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;gt;:value&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; src-block))
      (setq filename (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;cond&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; ((string= language &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"python"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"../media/py-%d.py"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; counter))
                           ((string= language &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"../media/el-%d.elisp"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; counter))))
      (setq counter (+ counter 1))
      (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; filename)))))
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;


 

&amp;amp;amp;amp;lt;a href="el-1.elisp"&amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;gt;
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;
Now, we have saved each code block into a file. The next step is inserting links to the files in published html files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file. Then, as we go through the 
&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;gt;(&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;defun&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; &amp;amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;amp;gt;save-nth-src-block&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; (nth parsetree)
&amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"write out the nth codeblock to a file. returns the filename written to."&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
  (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;let&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; ((counter 0))
    (org-element-map parsetree 'src-block 
      (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;lambda&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; (src-block) 
        (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;if&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; (= nth counter)
            (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;progn&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
              (setq language (org-element-property &amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;gt;:language&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; src-block))
              (setq code (org-element-property &amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;gt;:value&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; src-block))
              (setq filename (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;cond&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; ((string= language &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"python"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"py-%d.py"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; counter))
                                   ((string= language &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"el-%d.elisp"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; counter))))
              
              (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
                (insert code)
                (write-region (point-min) (point-max) (concat &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"../media/"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; filename)))
              (princ (format &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; filename))))
        (setq counter (+ counter 1)))))
    &amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;gt;;; &amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;gt;return the URL the file will be served at&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
    (concat &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"/media/"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; filename))
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;lt;a href="el-2.elisp"&amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;
Let us test that out.
&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;gt;(save-nth-src-block 2 (org-element-parse-buffer))
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;lt;a href="el-3.elisp"&amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;gt;
/media/el-2.elisp
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See &amp;amp;amp;amp;lt;a href="http://orgmode.org/worg/exporters/filter-markup.html"&amp;amp;amp;amp;gt;http://orgmode.org/worg/exporters/filter-markup.html&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is html. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;p&amp;amp;amp;amp;gt;
So our filter will use the global counter to call our save-nth-src-block function. That function will return the filename the codeblock was saved to. That is not the same as the path on the blog, 
&amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;gt;



&amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;gt;(&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;let&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; ((counter 0))
  (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;defun&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; &amp;amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;amp;gt;ox-mrkup-filter-src-block&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; (text back-end info)
    (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;let&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; ((filename (save-nth-src-block counter (plist-get info &amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;gt;:parse-tree&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;)))
          (output   (format &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"%s &amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;a href=\"%s\"&amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;

&amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; text filename)))
      (setq counter (+ counter 1))
      output))

  (&amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;gt;let&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt; ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    &amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;gt;; &amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;&amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;gt;now get the output into the org output&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;
    (switch-to-buffer &amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;gt;"*Org HTML Export*"&amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;gt;)
    (buffer-string))
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;lt;a href="el-4.elisp"&amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;div id="table-of-contents"&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;h2&amp;amp;amp;amp;amp;gt;Table of Contents&amp;amp;amp;amp;amp;lt;/h2&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;div id="text-table-of-contents"&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;li&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;a href="#sec-1"&amp;amp;amp;amp;amp;gt;1. Customizing the export of org-mode elements&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;li&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;a href="#sec-1-1"&amp;amp;amp;amp;amp;gt;1.1. Parsing the org-file&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;div id="outline-container-sec-1" class="outline-2"&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;h2 id="sec-1"&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;span class="section-number-2"&amp;amp;amp;amp;amp;gt;1&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; Customizing the export of org-mode elements&amp;amp;amp;amp;amp;lt;/h2&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;div class="outline-text-2" id="text-1"&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;gt;
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
&amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;pre class="src src-python"&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;for&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; i &amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;in&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;gt;range&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;(3):
    &amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;print&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; i**2
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;lt;a href="py-0.py"&amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;gt;
0
1
4
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;


&amp;amp;amp;amp;amp;lt;div id="outline-container-sec-1-1" class="outline-3"&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;h3 id="sec-1-1"&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;span class="section-number-3"&amp;amp;amp;amp;amp;gt;1.1&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; Parsing the org-file&amp;amp;amp;amp;amp;lt;/h3&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;div class="outline-text-3" id="text-1-1"&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;gt;
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
&amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;gt;(&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;lambda&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; (src-block) 
      (setq language (org-element-property &amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;gt;:language&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; src-block))
      (setq code (org-element-property &amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;gt;:value&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; src-block))
      (setq filename (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;cond&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; ((string= language &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"python"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"../media/py-%d.py"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; counter))
                           ((string= language &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"../media/el-%d.elisp"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; counter))))
      (setq counter (+ counter 1))
      (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; filename)))))
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;


 

&amp;amp;amp;amp;amp;lt;a href="el-1.elisp"&amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;gt;
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;gt;
Now, we have saved each code block into a file. The next step is inserting links to the files in published html files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
&amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;gt;
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file. Then, as we go through the 
&amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;gt;(&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;defun&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;amp;amp;gt;save-nth-src-block&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; (nth parsetree)
&amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"write out the nth codeblock to a file. returns the filename written to."&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;
  (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; ((counter 0))
    (org-element-map parsetree 'src-block 
      (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;lambda&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; (src-block) 
        (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;if&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; (= nth counter)
            (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;progn&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;
              (setq language (org-element-property &amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;gt;:language&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; src-block))
              (setq code (org-element-property &amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;gt;:value&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; src-block))
              (setq filename (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;cond&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; ((string= language &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"python"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"py-%d.py"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; counter))
                                   ((string= language &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"el-%d.elisp"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; counter))))
              
              (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;
                (insert code)
                (write-region (point-min) (point-max) (concat &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"../media/"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; filename)))
              (princ (format &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; filename))))
        (setq counter (+ counter 1)))))
(concat &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"/media/"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; filename))
        &amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;gt;;; &amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;gt;return the URL the file will be served at&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;
        ))))
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;lt;a href="el-2.elisp"&amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;gt;
Let us test that out.
&amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;gt;(save-nth-src-block 2 (org-element-parse-buffer))
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;lt;a href="el-3.elisp"&amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;gt;
Wrote el-2.elisp
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;gt;
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See &amp;amp;amp;amp;amp;lt;a href="http://orgmode.org/worg/exporters/filter-markup.html"&amp;amp;amp;amp;amp;gt;http://orgmode.org/worg/exporters/filter-markup.html&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is html. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
&amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;gt;
So our filter will use the global counter to call our save-nth-src-block function. That function will return the filename the codeblock was saved to. That is not the same as the path on the blog, 
&amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;gt;



&amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;gt;(&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; ((counter 0))
  (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;defun&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;amp;amp;gt;ox-mrkup-filter-src-block&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; (text back-end info)
    (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; ((filename (save-nth-src-block counter (plist-get info &amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;gt;:parse-tree&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;)))
          (output   (format &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"%s &amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;a href=\"%s\"&amp;amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; text filename)))
      (setq counter (+ counter 1))
      output))

  (&amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt; ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    &amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;gt;; &amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;gt;now get the output into the org output&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;
    (switch-to-buffer &amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;gt;"*Org HTML Export*"&amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;gt;)
    (buffer-string))
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;lt;a href="el-4.elisp"&amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;div id="table-of-contents"&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;h2&amp;amp;amp;amp;amp;amp;gt;Table of Contents&amp;amp;amp;amp;amp;amp;lt;/h2&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;div id="text-table-of-contents"&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;li&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;a href="#sec-1"&amp;amp;amp;amp;amp;amp;gt;1. Customizing the export of org-mode elements&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;ul&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;li&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;a href="#sec-1-1"&amp;amp;amp;amp;amp;amp;gt;1.1. Parsing the org-file&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/li&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/ul&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;div id="outline-container-sec-1" class="outline-2"&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;h2 id="sec-1"&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;span class="section-number-2"&amp;amp;amp;amp;amp;amp;gt;1&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; Customizing the export of org-mode elements&amp;amp;amp;amp;amp;amp;lt;/h2&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;div class="outline-text-2" id="text-1"&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;gt;
In a recent project I have used org-mode to create a document with lots of src blocks describing how to use python. I wanted to find a way to modify the export process so that each code block would be saved to a file, and a link to that file would be put in the exported pdf. I could have used tangling to do that, but I did not want the burden of having to name the code blocks. I wanted them named sequentially, and I often rearrange the blocks or add new ones in. 
&amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;pre class="src src-python"&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;for&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; i &amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;in&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;amp;gt;range&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;(3):
    &amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;print&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; i**2
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;amp;lt;a href="py-0.py"&amp;amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;amp;gt;
0
1
4
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;


&amp;amp;amp;amp;amp;amp;lt;div id="outline-container-sec-1-1" class="outline-3"&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;h3 id="sec-1-1"&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;span class="section-number-3"&amp;amp;amp;amp;amp;amp;gt;1.1&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; Parsing the org-file&amp;amp;amp;amp;amp;amp;lt;/h3&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;div class="outline-text-3" id="text-1-1"&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;gt;
One strategy we can use is to parse the org-buffer, and for each code block get the language, construct a filename that is appropriate, and write the file to where we want it. I am going to write the files to a location in my blogofile root so I can later link to them in the blog.
&amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;amp;gt;(&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; ((parsetree (org-element-parse-buffer))
      (counter 0))
  (org-element-map parsetree 'src-block 
    (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;lambda&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; (src-block) 
      (setq language (org-element-property &amp;amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;amp;gt;:language&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; src-block))
      (setq code (org-element-property &amp;amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;amp;gt;:value&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; src-block))
      (setq filename (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;cond&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; ((string= language &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"python"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"../media/py-%d.py"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; counter))
                           ((string= language &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"../media/el-%d.elisp"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; counter))))
      (setq counter (+ counter 1))
      (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;
        (insert code)
        (write-region (point-min) (point-max) filename))
      (princ (format &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; filename)))))
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;


 

&amp;amp;amp;amp;amp;amp;lt;a href="el-1.elisp"&amp;amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;amp;gt;
Wrote ../media/py-0.py
Wrote ../media/el-1.elisp
Wrote ../media/el-2.elisp
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;gt;
Now, we have saved each code block into a file. The next step is inserting links to the files in published html files. For this, we need to create a filter for src-blocks. In the blog, the files will be served at /media/filename. We are going to insert a hyperlink to that file after the HTML rendered code. 
&amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;gt;
It turned out that I needed a new way to think about connecting the export filter to the code blocks. We need to keep track of which code block is being exported, so we can know which file to link to. I decided a way to do that is to create a function that gets the nth code block and write it to a file. Then, as we go through the 
&amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;amp;gt;(&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;defun&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;amp;amp;amp;gt;save-nth-src-block&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; (nth parsetree)
&amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"write out the nth codeblock to a file. returns the filename written to."&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;
  (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; ((counter 0))
    (org-element-map parsetree 'src-block 
      (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;lambda&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; (src-block) 
        (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;if&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; (= nth counter)
            (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;progn&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;
              (setq language (org-element-property &amp;amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;amp;gt;:language&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; src-block))
              (setq code (org-element-property &amp;amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;amp;gt;:value&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; src-block))
              (setq filename (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;cond&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; ((string= language &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"python"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"py-%d.py"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; counter))
                                   ((string= language &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"emacs-lisp"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;) (format &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"el-%d.elisp"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; counter))))
              
              (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;with-temp-buffer&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;
                (insert code)
                (write-region (point-min) (point-max) (concat &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"../media/"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; filename)))
              (princ (format &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"Wrote %s\n"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; filename))))
        (setq counter (+ counter 1))
        &amp;amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;amp;gt;;; &amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;amp;gt;return the URL the file will be served at&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;
        (concat &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"/media/"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; filename)))))
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;amp;lt;a href="el-2.elisp"&amp;amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;gt;
Let us test that out.
&amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;amp;gt;(save-nth-src-block 2 (org-element-parse-buffer))
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;amp;lt;a href="el-3.elisp"&amp;amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;amp;gt;
Wrote el-2.elisp
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;gt;
It looks like that is working. During the export process, the parse tree is actually stored in an object called info that is passed to the export filters, so we can use that. See &amp;amp;amp;amp;amp;amp;lt;a href="http://orgmode.org/worg/exporters/filter-markup.html"&amp;amp;amp;amp;amp;amp;gt;http://orgmode.org/worg/exporters/filter-markup.html&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt; for more details about setting up filters for export. What follows here is built off of that. We are going to define a filter for src-blocks that saves the src-block, and adds a hyperlink after the text. The text that is handed to the filter has already been rendered by the backend, in this case that is html. All we have to do is add our hyperlink. We will keep a global counter that is updated by the filter, so we know which codeblock we are in for creating the link. 
&amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;p&amp;amp;amp;amp;amp;amp;gt;
So our filter will use the global counter to call our save-nth-src-block function. That function will return the filename the codeblock was saved to. That is not the same as the path on the blog, 
&amp;amp;amp;amp;amp;amp;lt;/p&amp;amp;amp;amp;amp;amp;gt;



&amp;amp;amp;amp;amp;amp;lt;div class="org-src-container"&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;pre class="src src-emacs-lisp"&amp;amp;amp;amp;amp;amp;gt;(&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; ((counter 0))
  (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;defun&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;lt;span style="color: #8b2323;"&amp;amp;amp;amp;amp;amp;gt;ox-mrkup-filter-src-block&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; (text back-end info)
    (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; ((filename (save-nth-src-block counter (plist-get info &amp;amp;amp;amp;amp;amp;lt;span style="color: #cd0000;"&amp;amp;amp;amp;amp;amp;gt;:parse-tree&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;)))
          (output   (format &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"%s &amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;amp;lt;a href=\"%s\"&amp;amp;amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;amp;gt; &amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;

&amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; text filename)))
      (setq counter (+ counter 1))
      output))

  (&amp;amp;amp;amp;amp;amp;lt;span style="color: #8b0000;"&amp;amp;amp;amp;amp;amp;gt;let&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt; ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    &amp;amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;amp;gt;; &amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;&amp;amp;amp;amp;amp;amp;lt;span style="color: #ff0000; font-weight: bold;"&amp;amp;amp;amp;amp;amp;gt;now get the output into the org output&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;
    (switch-to-buffer &amp;amp;amp;amp;amp;amp;lt;span style="color: #228b22;"&amp;amp;amp;amp;amp;amp;gt;"*Org HTML Export*"&amp;amp;amp;amp;amp;amp;lt;/span&amp;amp;amp;amp;amp;amp;gt;)
    (buffer-string))
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;

 

&amp;amp;amp;amp;amp;amp;lt;a href="el-4.elisp"&amp;amp;amp;amp;amp;amp;gt;Open the code&amp;amp;amp;amp;amp;amp;lt;/a&amp;amp;amp;amp;amp;amp;gt; 

&amp;amp;amp;amp;amp;amp;lt;pre class="example"&amp;amp;amp;amp;amp;amp;gt;
Wrote py-0.py
Wrote el-1.elisp
Wrote el-2.elisp
Wrote el-3.elisp
Wrote el-4.elisp
&amp;amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/pre&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;amp;lt;/div&amp;amp;amp;amp;gt;
&amp;amp;amp;lt;/pre&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;
&amp;amp;amp;lt;/div&amp;amp;amp;gt;
&amp;amp;lt;/pre&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;
&amp;amp;lt;/div&amp;amp;gt;
&amp;lt;/pre&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/pre&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
</pre>

<p>
Finally, lets put it all together and save the html to blogofile:
</p>

<div class="org-src-container">

<pre class="src src-emacs-lisp">(<span style="color: #8b0000;">let</span> ((counter 0))
  (<span style="color: #8b0000;">defun</span> <span style="color: #8b2323;">ox-mrkup-filter-src-block</span> (text back-end info)
    (<span style="color: #8b0000;">let*</span> ((url (save-nth-src-block counter (plist-get info <span style="color: #cd0000;">:parse-tree</span>)))
          (output   (format <span style="color: #228b22;">"%s </span>

<span style="color: #228b22;">&lt;a href=\"%s\"&gt;Open the code&lt;/a&gt; </span>

<span style="color: #228b22;">"</span> text url)))
      (setq counter (+ counter 1))
      output))

  (<span style="color: #8b0000;">let</span> ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    <span style="color: #ff0000; font-weight: bold;">; </span><span style="color: #ff0000; font-weight: bold;">now get the output into the org output</span>
    (switch-to-buffer <span style="color: #228b22;">"*Org HTML Export*"</span>)
    (setq HTML (buffer-string)))
    (setq YAML <span style="color: #228b22;">"---</span>
<span style="color: #228b22;">title: Customizing export of code blocks in HTML</span>
<span style="color: #228b22;">date: 2013/09/28 10:45:00</span>
<span style="color: #228b22;">categories: org-mode</span>
<span style="color: #228b22;">---</span>



<span style="color: #228b22;">"</span>)
  (<span style="color: #8b0000;">with-temp-buffer</span>
(insert YAML)
(insert HTML)
(write-region (point-min) (point-max) <span style="color: #228b22;">"../_posts/2013-09-28-Customizing-export-of-code-blocks-in-HTML.html"</span>))
</pre>
</div>

 

<a href="/media/el-5.elisp">Open the code</a> 

<p>
Now, I should be able to build blogofile, and the code files I saved will be in the media folder, and the links to those files will work in a browser!
</p>
</div>
</div>
</div>

Finally, lets put it all together and save the html to blogofile. This requires us to build the YAML header, and save the file in a formatted filename in _posts of the blogofile directory.

(let ((counter 0))
  (defun ox-mrkup-filter-src-block (text back-end info)
    (let* ((url (save-nth-src-block counter (plist-get info :parse-tree)))
          (output   (format "%s 

<a href=\"%s\">Open the code</a> 

" text url)))
      (setq counter (+ counter 1))
      output))

  (let ((org-export-filter-src-block-functions '(ox-mrkup-filter-src-block))
        (async nil)
        (subtreep nil)
        (visible-only nil)
        (body-only t)
        (ext-plist '()))
    (org-html-export-as-html async subtreep visible-only body-only ext-plist))

    ; now get the output into the org output
    (switch-to-buffer "*Org HTML Export*")
    (setq HTML (buffer-string)))
    (setq YAML "---
title: Customizing export of code blocks in HTML
date: 2013/09/28 10:45:00
categories: org-mode
---



")
  (with-temp-buffer
(insert YAML)
(insert HTML)
(write-region (point-min) (point-max) "../_posts/2013-09-28-Customizing-export-of-code-blocks-in-HTML.html"))
(princ "Done.")
Open the code

Now, I should be able to build blogofile, and the code files I saved will be in the media folder, and the links to those files will work in a browser!

Discuss on Twitter
« Previous Page -- Next Page »