emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: torys.anderson@gmail.com (Tory S. Anderson)
To: orgmode list <emacs-orgmode@gnu.org>
Subject: Adding Google Code Prettify support to exported code blocks
Date: Sat, 07 Feb 2015 07:21:22 -0500	[thread overview]
Message-ID: <871tm1syil.fsf@gmail.com> (raw)

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	other threads:[~2015-02-07 12:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871tm1syil.fsf@gmail.com \
    --to=torys.anderson@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).