emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Inline code syntax highlighting
@ 2016-02-21 15:11 meditans
  2016-02-22  8:32 ` Nicolas Goaziou
  0 siblings, 1 reply; 2+ messages in thread
From: meditans @ 2016-02-21 15:11 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1988 bytes --]

Hi all,

I'm writing some Haskell code in a .org blog post which I will export to  
HTML using org-publish . I'd like to inline some haskell code in the text,  
and I can do that either using the marker ~...~ or the explicit block  
src_haskell[exports: code]{...} . I'd like to have the notational  
convenience of the former while retaining the syntax highlighting the  
latter provides. Here it is what I tried:


1 Tweaking the variable org-html-text-markup-alist


in particular the code correspondence, which by default is  
<code>%s</code> . This, while easy, doesn't get me syntax highlighting, and  
so it's not sufficiently good for my purposes.


2 Using org-export-before-processing-hook


to do some preprocessing, like:


(defun convert-code (backend)
   "Changes the code marked with =...= to code marked with  
src_haskell[:exports code]{...}"
   (replace-regexp "~\\([^~]+?\\)~" "src_haskell[:exports code]{\\1}"))

(add-hook 'org-export-before-processing-hook 'convert-code)


Unfortunately, this use of regexp feels hacky, messes up the formatting  
when I try to have some haskell code as the text for a link (for the  
presence of ] ), and gets exported as:


<code class="src src-haskell"><span style="color: #ba2f59; font-weight:  
bold;">stuff</span>
</code>


with a newline before </code> , which is read as an (unwanted) space in  
HTML.


3 Creating a custom exporter


This should work, but I got stuck working on it (I just began learning  
elisp). Here is a starting point, I'd appreciate some help:


(defun my-haskell-code (code contents info)
   "Changes the code marked with =...= to code marked with  
src_haskell[:exports code]{...}"
   (format "%s" (org-element-normalize-string  
(org-export-format-code-default code info))))

(org-export-define-derived-backend 'my-hask 'html
   :translate-alist '((code . my-haskell-code)))


In conclusion, do you have any tips on how to get syntax highlighting for  
~...~ blocks?

Best regards

Carlo


Carlo



[-- Attachment #2: Type: text/html, Size: 5160 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Inline code syntax highlighting
  2016-02-21 15:11 Inline code syntax highlighting meditans
@ 2016-02-22  8:32 ` Nicolas Goaziou
  0 siblings, 0 replies; 2+ messages in thread
From: Nicolas Goaziou @ 2016-02-22  8:32 UTC (permalink / raw)
  To: meditans; +Cc: emacs-orgmode

Hello,

meditans@gmail.com writes:

> In conclusion, do you have any tips on how to get syntax highlighting
> for ~...~ blocks?

You can use `org-export-filter-parse-tree' and replace all occurrences
of (code ...) into (inline-src-block ...) using a combination of
`org-element-map', and `org-element-set-element'.

Another option is indeed to create a new export back-end that replaces
code transcoder with a custom function, probably something ultimately
calling `org-html-inline-src-block', e.g.,

  (defun my-code-to-haskell (code contents info)
    (let ((haskell
           `(inline-src-block
             (:language "haskell" :value ,(org-element-property :value code)))))
      (org-html-inline-src-block haskell nil info)))

  (org-export-define-derived-backend 'haskell-html 'html
    :translate-alist '((code . my-code-to-haskell)))


Regards,

-- 
Nicolas Goaziou

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2016-02-22  8:30 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-02-21 15:11 Inline code syntax highlighting meditans
2016-02-22  8:32 ` Nicolas Goaziou

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).