emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* How to customize the org-mode's BEGIN_SRC HTML output
@ 2010-08-23  9:41 卓强 Will Zhuo
  2010-08-23 19:03 ` Erik Iverson
  0 siblings, 1 reply; 10+ messages in thread
From: 卓强 Will Zhuo @ 2010-08-23  9:41 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1567 bytes --]

Hi,

Recently I start blogging using org-mode, it works pretty well, except that
I would like to customize its output somehow and don't know how to make it
work.

Here's one of the cusomization requirement:

when putting source code in blog, I would like to use the plugin:
http://alexgorbatchev.com/SyntaxHighlighter/ which is basicly a javascript
frontend engine turning the following HTML to a nice looking code snippet on
the web brower.

<pre class="brush: js">
    /**
     * SyntaxHighlighter
     */
    function foo()
    {
        if (counter <= 10)
            return;
        // it works!
    }
</pre>

I would like to make my code snippets in the org files exporting exactly
what SyntaxHighlighter required (with a <pre class="brush: %(lanangue name)>
tag and plain source code in it only escape the special char)

However, I could not find a way:
using #+BEGIN_EXAMPLE give me a <pre> tag with class="example" and some
<span> tag

using #+BEGIN_SRC give me a <pre> tag with class="src src-%(lanangue name)"
and a bunch of <span> in the code itself.

using #+BEGIN_HTML give me nothing and it does not escape special chars like
"<>" either.

And there seems no custmized variable which could control the "class" of the
<pre> tag.

My questions are:
Is there some way to achive this?
If it need hack , where should I modify el files to be able to get the
BEGIN_SRC works as my expected?
Generally, how should one do this kind of customization and how to even add
another export format, is there any tutorial to startup ?

thanks for your help,
ZHUO,Qiang

[-- Attachment #1.2: Type: text/html, Size: 1923 bytes --]

[-- Attachment #2: Type: text/plain, Size: 201 bytes --]

_______________________________________________
Emacs-orgmode mailing list
Please use `Reply All' to send replies to the list.
Emacs-orgmode@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
  2010-08-23  9:41 How to customize the org-mode's BEGIN_SRC HTML output 卓强 Will Zhuo
@ 2010-08-23 19:03 ` Erik Iverson
  2010-08-26  3:22   ` Andrei Jirnyi
  0 siblings, 1 reply; 10+ messages in thread
From: Erik Iverson @ 2010-08-23 19:03 UTC (permalink / raw)
  To: 卓强 Will Zhuo; +Cc: emacs-orgmode

Hello Will,

I assume you don't want the htmlize.el functionality, which
syntax highlights the source code according to how you
have it in Emacs?

The way I ultimately ended up doing something similar was to
post-process the html file generated by the export process,
(there is probably a hook so you can do this automatically):

My wants required:

    (while (re-search-forward "<pre " nil t)
          (replace-match
           "<pre style=\"background-color:#FFFFE5; font-size:8pt\" "
           t nil))

You might also have to set org-export-htmlize-output-type to
something other than 'inline-css, but I'm not sure.

Hope this either helps, or that someone has a better way :).

--Erik


卓强 Will Zhuo wrote:
> Hi,
> 
> Recently I start blogging using org-mode, it works pretty well, except 
> that I would like to customize its output somehow and don't know how to 
> make it work.
> 
> Here's one of the cusomization requirement:
> 
> when putting source code in blog, I would like to use the plugin: 
> http://alexgorbatchev.com/SyntaxHighlighter/ which is basicly a 
> javascript frontend engine turning the following HTML to a nice looking 
> code snippet on the web brower.
> 
> <pre class="brush: js">
>     /**
>      * SyntaxHighlighter
>      */
>     function foo()
>     {
>         if (counter <= 10)
>             return;
>         // it works!
>     }
> </pre>
>  
> I would like to make my code snippets in the org files exporting exactly 
> what SyntaxHighlighter required (with a <pre class="brush: %(lanangue 
> name)> tag and plain source code in it only escape the special char)
> 
> However, I could not find a way:
> using #+BEGIN_EXAMPLE give me a <pre> tag with class="example" and some 
> <span> tag
> 
> using #+BEGIN_SRC give me a <pre> tag with class="src src-%(lanangue 
> name)" and a bunch of <span> in the code itself.
> 
> using #+BEGIN_HTML give me nothing and it does not escape special chars 
> like "<>" either.
> 
> And there seems no custmized variable which could control the "class" of 
> the <pre> tag.
> 
> My questions are:
> Is there some way to achive this?
> If it need hack , where should I modify el files to be able to get the 
> BEGIN_SRC works as my expected?
> Generally, how should one do this kind of customization and how to even 
> add another export format, is there any tutorial to startup ?
> 
> thanks for your help,
> ZHUO,Qiang
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Emacs-orgmode mailing list
> Please use `Reply All' to send replies to the list.
> Emacs-orgmode@gnu.org
> http://lists.gnu.org/mailman/listinfo/emacs-orgmode

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
@ 2010-08-24 13:39 Benjamin Beckwith
  2010-08-25  2:36 ` Rafael
  0 siblings, 1 reply; 10+ messages in thread
From: Benjamin Beckwith @ 2010-08-24 13:39 UTC (permalink / raw)
  To: emacs-orgmode

Hi, I also was interested in posting these blocks (through org2blog in
wordpress).  The code I posted below is added to
'org-export-preprocess-hooks' where it looks for BEGIN_SRC blocks as
well as ':' blocks of code.

In the case of BEGIN_SRC blocks, I add a header option, :syntaxhl where
I can pass in additional settings to the syntaxhighlighter code.

The code below uses Wordpress shortcodes, but I am sure that you can
adapt for your own purposes.

------------------------------------------------------------
(defun bnb/org2blog-src-blocks-to-wp-syntaxhighlighter ()
  "Export #+BEGIN_SRC blocks as Wordpress Syntaxhighlighter
tags. There is a special header option, :syntaxhl that contains
the options to pass to syntaxhighlighter.

This is intended to be added to `org-export-preprocess-hooks'"
  (interactive)
  (save-window-excursion
    (let ((case-fold-search t)
	  (colon-re "^[ \t]*:\\([ \t]\\|$\\)")
	  lang body headers syntaxhl
	  beg)
      (goto-char (point-min))
      (while (re-search-forward colon-re nil t)
	(replace-match (match-string 1))
	(beginning-of-line 1)
	(insert "[text light=\"true\"]\n")
	(setq beg (point))
	(while (looking-at colon-re)
	  (replace-match (match-string 1))
	  (end-of-line 1)
	  (or (eobp) (forward-char 1)))
	(end-of-line 1)
	(add-text-properties beg
			     (if (bolp)
				 (1- (point))
			       (point))
			     '(org-protected t))
	(insert "\n[/text]"))
      (unless (boundp 'org-babel-src-block-regexp)
	(require 'ob))
      (while (re-search-forward
	      (concat "\\(" org-babel-src-block-regexp
		      "\\|" org-babel-inline-src-block-regexp
		      "\\)")
	      nil t)
	(setq lang (match-string-no-properties 3))
	(if (string-match "-" lang)
	    (error "SyntaxHighlighter does not support languages with '-' in
the names"))
	(setq headers (match-string-no-properties 5))
	(setq body (match-string-no-properties 6))
	(save-match-data
	  (setq syntaxhl
		(if (string-match ":syntaxhl[ ]+\\([^ ]+\\)" headers)
		    (concat " " (replace-regexp-in-string "\;" " " (match-string 1
headers))))))
	(replace-match
	 (concat "\n\n[" lang syntaxhl "]\n" body "[/" lang "]\n")
	 nil t)))))

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
  2010-08-24 13:39 Benjamin Beckwith
@ 2010-08-25  2:36 ` Rafael
  0 siblings, 0 replies; 10+ messages in thread
From: Rafael @ 2010-08-25  2:36 UTC (permalink / raw)
  To: emacs-orgmode

Benjamin Beckwith <bnbeckwith@gmail.com> writes:

> Hi, I also was interested in posting these blocks (through org2blog in
> wordpress).  The code I posted below is added to
> 'org-export-preprocess-hooks' where it looks for BEGIN_SRC blocks as
> well as ':' blocks of code.
>
> In the case of BEGIN_SRC blocks, I add a header option, :syntaxhl where
> I can pass in additional settings to the syntaxhighlighter code.
>
> The code below uses Wordpress shortcodes, but I am sure that you can
> adapt for your own purposes.

Thanks! that seems to almost work for me, if I replace your penultimate
line:

> (concat "\n\n[" lang syntaxhl "]\n" body "[/" lang "]\n")

by

(concat "\n\n[sourcecode language=\"" lang syntaxhl "\"]\n"
                                          body "[/sourcecode]\n")

but then the line breaks are lost, and the latex exporter tries to be
too smart and replace the code.. Please see

http://rvftestblog.wordpress.com/2010/08/24/code-blocks-again/

which was intended as a result of posting the following:

#+POSTID: 17
#+DATE: [2010-08-24 Tue 20:43]
#+OPTIONS: toc:nil num:nil todo:nil pri:nil tags:nil ^:{}
#+DESCRIPTION: Testing
#+KEYWORDS: test
#+TITLE: Code blocks again

A perl example:

#+BEGIN_SRC perl 
   for (my $i = 0; $i != 10; ++i) {
        print "hello, world!\n";
   }
#+END_SRC

another

#+BEGIN_SRC latex
\begin{theorem}
  \label{theorem:1}
  {\normalfont (Augmentation Theorem)} Let $M=(S,I)$ be a matroid, and
  $X,Y\subseteq I$ with $|X|<|Y|$. Then there is $Z\subseteq
  Y\setminus X$ such that $|X\cup Z|=|Y|$ and $X\cup Z\in I$.
\end{theorem}
#+END_SRC

update 8: let's see if it works now...

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
@ 2010-08-25  3:32 Benjamin Beckwith
  2010-08-25  8:30 ` Eric S Fraga
  0 siblings, 1 reply; 10+ messages in thread
From: Benjamin Beckwith @ 2010-08-25  3:32 UTC (permalink / raw)
  To: emacs-orgmode

Rafael,

I had my shortcodes setup to accept the language directly. Your change
should work as you indicated.  I have an additional fix to my code that
should behave better for you.  I add a property, org-protected, that
prevents processing of the text.

I tried it on your code blocks by calling org-export directly (C-c C-e)
and then choosing 'H' to see the html in a buffer.  This really helps
with debug.

Fixed code follows
----------------------------------------------------
(defun bnb/org2blog-src-blocks-to-wp-syntaxhighlighter ()
  "Export #+BEGIN_SRC blocks as Wordpress Syntaxhighlighter
tags. There is a special header option, :syntaxhl that contains
the options to pass to syntaxhighlighter.

This is intended to be added to `org-export-preprocess-hooks'"
  (interactive)
  (save-window-excursion
    (let ((case-fold-search t)
	  (colon-re "^[ \t]*:\\([ \t]\\|$\\)")
	  lang body headers syntaxhl block
	  beg)
      (goto-char (point-min))
      (while (re-search-forward colon-re nil t)
	(replace-match (match-string 1))
	(beginning-of-line 1)
	(insert "[text light=\"true\"]\n")
	(setq beg (point))
	(while (looking-at colon-re)
	  (replace-match (match-string 1))
	  (end-of-line 1)
	  (or (eobp) (forward-char 1)))
	(end-of-line 1)
	(add-text-properties beg
			     (if (bolp)
				 (1- (point))
			       (point))
			     '(org-protected t))
	(insert "\n[/text]"))
      (unless (boundp 'org-babel-src-block-regexp)
	(require 'ob))
      (while (re-search-forward
	      (concat "\\(" org-babel-src-block-regexp
		      "\\|" org-babel-inline-src-block-regexp
		      "\\)")
	      nil t)
	(setq lang (match-string-no-properties 3))
	(if (string-match "-" lang)
	    (error "SyntaxHighlighter does not support languages with '-' in
the names"))
	(setq headers (match-string-no-properties 5))
	(setq body (match-string-no-properties 6))
	(save-match-data
	  (setq syntaxhl
		(if (string-match ":syntaxhl[ ]+\\([^ ]+\\)" headers)
		    (concat " " (replace-regexp-in-string "\;" " " (match-string 1
headers))))))
	(setq block (concat "\n\n[" lang syntaxhl "]\n" body "[/" lang "]\n"))
	(add-text-properties 0 (length block) '(org-protected t) block)
	(replace-match
	 block
	 nil t)))))
----------------------------------

I also remembered that I made the following setting as well, but I do
not know if I still need it.

(setq org-export-preprocess-hook
      (list
       'bnb/org2blog-src-blocks-to-wp-syntaxhighlighter
       'org-export-blocks-preprocess))

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
  2010-08-25  8:30 ` Eric S Fraga
@ 2010-08-25 12:23   ` Rafael
  2010-08-25 12:51     ` Puneeth
  0 siblings, 1 reply; 10+ messages in thread
From: Rafael @ 2010-08-25 12:23 UTC (permalink / raw)
  To: emacs-orgmode

Eric S Fraga <ucecesf@ucl.ac.uk> writes:

> On Tue, 24 Aug 2010 23:32:01 -0400, Benjamin Beckwith <bnbeckwith@gmail.com> wrote:
>> 
>> Rafael,
>> 
>> I had my shortcodes setup to accept the language directly. Your change
>> should work as you indicated.  I have an additional fix to my code that
>> should behave better for you.  I add a property, org-protected, that
>> prevents processing of the text.
>> 
>> I tried it on your code blocks by calling org-export directly (C-c C-e)
>> and then choosing 'H' to see the html in a buffer.  This really helps
>> with debug.
>> 
>> Fixed code follows
>
> [...]
>
> Benjamin,
>
> can you show us a simple example blog entry that works with this?  I
> have tried and, with your version, I do not get the source code plugin
> in Wordpress invoked.  If I use Rafael's change, the plugin does get
> used.  Also, both your code and Rafael's cause all linebreaks in the
> code to disappear.  
>
> For instance, with some simple =MATLAB= code:
>
> #+srcname: matlabblock
> #+begin_src matlab
> function y = f(x)
>   x = 1989;
>   y = (2010-x)^2;
> #+end_src
>
> your most recent code generates
>
> ,----
> | <p>    [matlab] function y = f(x)   x = 1989;   y = (2010-x)^2; [/matlab]
> `----
>
> which is not something wordpress seems to understand?
>

I think Benjamin means that he has customized this

http://en.support.wordpress.com/shortcodes/

so that his code works directly. But it is not clear to me which changes
he had to do for that, nor if that is available to free wordpress
users...

Oh, and by the way, with Org-mode version 7.01h I concur with Eric in
that I unfortunately still lose the linebreaks and get LaTeX code
modified with Benjamin's new version.. 

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
  2010-08-25 12:51     ` Puneeth
@ 2010-08-26  1:54       ` Rafael
  2010-08-29  9:37       ` Puneeth
  1 sibling, 0 replies; 10+ messages in thread
From: Rafael @ 2010-08-26  1:54 UTC (permalink / raw)
  To: emacs-orgmode

Puneeth <punchagan@gmail.com> writes:

> The line breaks being stripped off is due to code in org2blog. It has
> nothing to do with org-mode's export. Wordpress does not ignore
> linebreaks in the content, which looks very ugly for normal posts.
> Code in org2blog strips off the line breaks from the html generated by
> org-export-as-html. It checks for <pre> and <blockquote> tags and
> leaves out the newlines within those tags. This is (most) probably
> what is causing trouble.
>
> I'll only be able to look into it in the weekend. Anybody is free to
> beat me to that. :)

This is a workaround, all based on work by Puneeth and Benjamin, that
seems to work for me. I did not know what to do with Benjamin's advice
stuff (Just evaluating it changed nothing in the output). 

;; Benjamin's stuff, one line changed
(defun bnb/org2blog-src-blocks-to-wp-syntaxhighlighter ()
  "Export #+BEGIN_SRC blocks as Wordpress Syntaxhighlighter
tags. There is a special header option, :syntaxhl that contains
the options to pass to syntaxhighlighter.
This is intended to be added to `org-export-preprocess-hooks'"
  (interactive)
  (save-window-excursion
    (let ((case-fold-search t)
	  (colon-re "^[ \t]*:\\([ \t]\\|$\\)")
	  lang body headers syntaxhl
	  beg)
      (goto-char (point-min))
      (while (re-search-forward colon-re nil t)
	(replace-match (match-string 1))
	(beginning-of-line 1)
	(insert "[text light=\"true\"]\n")
	(setq beg (point))
	(while (looking-at colon-re)
	  (replace-match (match-string 1))
	  (end-of-line 1)
	  (or (eobp) (forward-char 1)))
	(end-of-line 1)
	(add-text-properties beg
			     (if (bolp)
				 (1- (point))
			       (point))
			     '(org-protected t))
	(insert "\n[/text]"))
      (unless (boundp 'org-babel-src-block-regexp)
	(require 'ob))
      (while (re-search-forward
	      (concat "\\(" org-babel-src-block-regexp
		      "\\|" org-babel-inline-src-block-regexp
		      "\\)")
	      nil t)
	(setq lang (match-string-no-properties 3))
	(if (string-match "-" lang)
	    (error "SyntaxHighlighter does not support languages with '-' in the names"))
	(setq headers (match-string-no-properties 5))
	(setq body (match-string-no-properties 6))
	(save-match-data
	  (setq syntaxhl
		(if (string-match ":syntaxhl[ ]+\\([^ ]+\\)" headers)
		    (concat " " (replace-regexp-in-string "\;" " " (match-string 1 headers))))))
	(replace-match
         ;(concat "\n\n[" lang syntaxhl "]\n" body "[/" lang "]\n")
	 (concat "\n\n[sourcecode language=\"" lang syntaxhl "\"]\n" body "[/sourcecode]\n")
	 nil t)))))

(add-hook 'org-export-preprocess-hook 'bnb/org2blog-src-blocks-to-wp-syntaxhighlighter)

;; searching for [sourcecode ... ] ... [/sourcecode] so that newlines
;; are not removed
;; Puneeth's stuff, two lines changed
(defun org2blog-strip-new-lines (html)
  "Strip the new lines from the html, except in pre and blockquote tags."
  (save-excursion
    (with-temp-buffer
      (let* (start-pos end-pos)
        (insert html)
        (setq start-pos (point-min))
        (goto-char start-pos)
        (while (re-search-forward "\\[sourcecode.*" nil t 1)
          (setq end-pos (match-beginning 0))
          (replace-regexp "\\\n" " " nil start-pos end-pos)
          (re-search-forward "\\[/sourcecode.*" nil t 1)
          (setq start-pos (match-end 0))
          (goto-char start-pos))
        (setq end-pos (point-max))
        (replace-regexp "\\\n" " " nil start-pos end-pos)
        (buffer-substring-no-properties (point-min) (point-max))))))

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
  2010-08-23 19:03 ` Erik Iverson
@ 2010-08-26  3:22   ` Andrei Jirnyi
  0 siblings, 0 replies; 10+ messages in thread
From: Andrei Jirnyi @ 2010-08-26  3:22 UTC (permalink / raw)
  To: emacs-orgmode

On Mon, 23 Aug 2010 14:03:23 -0500, Erik Iverson wrote:

> post-process the html file generated by the export process, (there is
> probably a hook so you can do this automatically):

apparently it's org-export-html-final-hook

--aj

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
  2010-08-29  9:37       ` Puneeth
@ 2010-08-29 18:18         ` Rafael
  2010-09-01 10:42           ` Puneeth
  0 siblings, 1 reply; 10+ messages in thread
From: Rafael @ 2010-08-29 18:18 UTC (permalink / raw)
  To: emacs-orgmode

Puneeth <punchagan@gmail.com> writes:

> This has been fixed. Thanks to a patch from Benjamin Beckwith. You can
> now use any WP shortcode blocks in your org2blog posts, without losing
> line breaks.
>
> Also, org2blog now directly posts babel src blocks as WP's sourcecode
> blocks, without modifying the src blocks in your org file. Just like
> with Benjamin's hook function, :syntaxhl header option can still be
> used to pass additional arguments to WP's syntaxhighlighter.

That is just great, Puneeth, many thanks! I just checked and works as
intended. 

Now, in case you wanted to have some more feature requests, ;-), here
they are:

1. Making an option to disable the inclusion of ':light="true"'.

2. Note that Wordpress also includes support for inline math expressions
written in LaTeX, like this: $latex E=mc^2$, and displayed expressions,
like this: $latex \displaystyle E=mc^2$
(http://rvftestblog.wordpress.com/2010/08/24/code-blocks-again/ to see
what I mean). So it would be great if, the
exporter could translate $....$ into $latex ....$ and 

\begin{equation}
.......
\end{equation}

into

$latex \displaystyle ...... $.

Best regards,

Rafael

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

* Re: How to customize the org-mode's BEGIN_SRC HTML output
  2010-09-01 10:42           ` Puneeth
@ 2010-09-01 14:38             ` Rafael
  0 siblings, 0 replies; 10+ messages in thread
From: Rafael @ 2010-09-01 14:38 UTC (permalink / raw)
  To: emacs-orgmode

Puneeth <punchagan@gmail.com> writes:

>> On Sun, Aug 29, 2010 at 11:48 PM, Rafael <rvf0068@gmail.com> wrote:

>>> 2. Note that Wordpress also includes support for inline math expressions
>>> written in LaTeX, like this: $latex E=mc^2$, and displayed expressions,
>>> like this: $latex \displaystyle E=mc^2$
>>> (http://rvftestblog.wordpress.com/2010/08/24/code-blocks-again/ to see
>>> what I mean). 
>
> This has been Done.
>

Hi Puneeth and all,

Thanks a lot!
I could observe one bug and one "feature".

LaTeX expressions of one letter are not touched, for example $a$, $b$
etc.

Also, since by default stuff like \alpha and \cup is converted to &alpha
and &cup, you might want to include

(setq org-export-with-TeX-macros nil)

when exporting LaTeX.

Rafael

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

end of thread, other threads:[~2010-09-01 14:38 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2010-08-23  9:41 How to customize the org-mode's BEGIN_SRC HTML output 卓强 Will Zhuo
2010-08-23 19:03 ` Erik Iverson
2010-08-26  3:22   ` Andrei Jirnyi
  -- strict thread matches above, loose matches on Subject: below --
2010-08-24 13:39 Benjamin Beckwith
2010-08-25  2:36 ` Rafael
2010-08-25  3:32 Benjamin Beckwith
2010-08-25  8:30 ` Eric S Fraga
2010-08-25 12:23   ` Rafael
2010-08-25 12:51     ` Puneeth
2010-08-26  1:54       ` Rafael
2010-08-29  9:37       ` Puneeth
2010-08-29 18:18         ` Rafael
2010-09-01 10:42           ` Puneeth
2010-09-01 14:38             ` Rafael

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