emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[O] Adding Google Code Prettify support to exported code blocks


From: Tory S. Anderson
Subject: [O] Adding Google Code Prettify support to exported code blocks
Date: Sat, 07 Feb 2015 07:21:22 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

I've started using Google Code Prettify on my blog and needed to add better 
support in the exports I'm getting out of org-mode. In particular, on my block 
prettify seems to do a poor job of guessing the language; so I've edited 
org-html-src-block to add the prettify tags (which are similar to the already 
exported src tags). But, ever the lisp learner, I'm open to suggestions for 
improvements. Following are code and an example. 

--8<---------------cut here---------------start------------->8---
;; Exporting code blocks suitable for google code prettify 
(https://code.google.com/p/google-code-prettify/)
(defun org-html-src-block (src-block contents info)
  "Transcode a SRC-BLOCK element from Org to HTML.
CONTENTS holds the contents of the item.  INFO is a plist holding
contextual information.
Modified to work with google code prettify (if installed on site)"
  (if (org-export-read-attribute :attr_html src-block :textarea)
      (org-html--textarea-block src-block)
    (let ((lang (org-element-property :language src-block))
          (caption (org-export-get-caption src-block))
          (code (org-html-format-code src-block info))
          (label (let ((lbl (org-element-property :name src-block)))
                   (if (not lbl) ""
                     (format " id=\"%s\""
                             (org-export-solidify-link-text lbl))))))
      (if (not lang) (format "<pre class=\"example\"%s>\n%s</pre>" label code)
        (format
         "<div class=\"org-src-container\">\n%s%s\n</div>"
         (if (not caption) ""
           (format "<label class=\"org-src-name\">%s</label>"
                   (org-export-data caption info)))
         ;; add prettyprint and lang- tags to exports
         (format "\n<pre class=\"prettyprint lang-%s src src-%s\"%s>%s</pre>" 
lang lang label code))))))
--8<---------------cut here---------------end--------------->8---

This allows this org block: 
--8<---------------cut here---------------start------------->8---
    #+BEGIN_SRC lisp
    ;; saving views
    (setq org-agenda-custom-commands
          '(("c" . "Weekly class agendas 2015-S")
            ("c6" "LMC 6215"
             ((agenda "" ((org-agenda-span 7) (org-agenda-start-on-weekday nil) 
(org-agenda-tag-filter-preset '("+LMC_6215" "-SCHEDULE"))))))
            ("c8" "CS 8803"
             ((agenda "" ((org-agenda-span 7) (org-agenda-start-on-weekday nil) 
(org-agenda-tag-filter-preset '("+CS_8803" "-SCHEDULE"))))))))
    #+END_SRC
--8<---------------cut here---------------end--------------->8---

to produce this (prettifiable) output: 

--8<---------------cut here---------------start------------->8---
<div class="org-src-container">
<pre class="prettyprint lang-lisp src src-lisp">
<!-- you get the idea; code ommitted -->
</pre>
</div>
--8<---------------cut here---------------end--------------->8---

Upon pasting, it looks good on my blog. 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]