Skip to content

Instantly share code, notes, and snippets.

@marcoimmel
Last active January 15, 2018 20:18
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save marcoimmel/79b7a58685d9da9f74ebbbd6aea2e42a to your computer and use it in GitHub Desktop.
Save marcoimmel/79b7a58685d9da9f74ebbbd6aea2e42a to your computer and use it in GitHub Desktop.
org2blog: Wordpress crayon-plugin Backend for Org Export Engine
;;; ox-wp-crayon.el - Wordpress crayon-plugin Backend for Org Export Engine
;;; overrides org-wp-src-block function of org2blog / ox-wp package, https://github.com/punchagan/org2blog
;;; Copyright (C) 2017 marco.immel@gmail.com
;;; Version: 20170110-1
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
(require 'ox-wp)
(defvar ox-wp-caption-format-str
'"style=\"text-align:center; font-family:Verdana,'Lucida Sans Unicode',sans-serif; font-style:italic; font-size:small;\""
"Styles for caption"
)
;; Supported cranyon attributes:
;; - lang: programming language
;; - title: set a title, e.g. #+ATTR_WP: :title "best code ever"
;; - mark: highlight lines, e.g. #+ATTR_WP: :markhl 2-5
;; https://github.com/aramk/crayon-syntax-highlighter
(defun org-wp-src-block-crayon (src-block contents info)
"Transcode a SRC-BLOCK element from Org to WP Cranyon HTML.
CONTENTS holds the contents of the item. INFO is a plist holding
contextual information."
(let ((lang (org-element-property :language src-block))
(caption (let ((cp (org-export-get-caption src-block)))
(if cp (format "<figcaption %s>%s</figcaption>" (or ox-wp-caption-format-str "") cp))))
;; Crayon doesn't supported captions well, there is a <p/> tag between code and caption and
;; I have not found a way arround that. So I recommend not to use captions actually.
(langs-map (plist-get info :wp-shortcode-langs-map))
(markhl (let ((ml (org-export-read-attribute :attr_wp src-block :markhl)))
(if ml (concat " mark:" ml))))
(blktitle (let ((tl (org-export-read-attribute :attr_wp src-block :title)))
(if tl (concat " title=\"" tl "\"")))))
(format "<figure><pre class=\"lang:%s%s decode:true \" %s>%s</pre>%s</figure>"
(or (cdr (assoc lang langs-map)) (when lang (downcase lang)) "text")
(or markhl "")
(or blktitle "")
(org-export-format-code-default src-block info)
(or caption "")
)))
(advice-add 'org-wp-src-block :override #'org-wp-src-block-crayon)
(provide 'ox-wp-crayon)
@priyadarshan
Copy link

priyadarshan commented Aug 24, 2017

Thank you for ox-wp-crayon.
It works as expected, only one needs to make sure proper Crayon settings are enabled on WP side.

@priyadarshan
Copy link

It seems the examples cited in the source are not working on my side:

#+ATTR_WP: title:"best code ever"
#+ATTR_WP: markhl:2-5
#+BEGIN_SRC lisp
(defun open-in-largest-window()
  "Open current buffer in largest window"
  (interactive)
  (let ((oldbuf (current-buffer)))
  (select-window (get-largest-window))
  (switch-to-buffer oldbuf))
)
#+END_SRC

Do not have any effect on the final HTML, i.e. title and highlighted lines are not there.

@priyadarshan
Copy link

priyadarshan commented Aug 24, 2017

I got it. The syntax is:

#+ATTR_WP: :title Best code ever
#+ATTR_WP: :markhl 2-5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment