Elisp Lesson: Writing make-html-table
Xah Lee, 2008-04-28
This page shows a example of writing a emacs lisp function that turns
the current block of text into a HTML table.
The Problem
I want to write a function, such that, when called, the current block
of text the cursor is on, becomes a HTML table. Suppose the block of
text is this:
a b c
1 2 3
this and that
after, pressing a button, it should become:
<table border="1" cellpadding="5" cellspacing="0">
<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>this</td><td>and</td><td>that</td></tr>
</table>
Here's elisp code:
(defun make-html-table-string (textblock delim)
"Turn a text string into a HTML table.
See make-html-table."
(let ()
(setq textblock (replace-regexp-in-string delim "</td><td>"
textblock))
(setq textblock (replace-regexp-in-string "\n" "</td></tr>
\n<tr><td>" textblock))
(setq textblock (substring textblock 0 -8)) ;; delet the beginning
“<tr><td>” in last line
(concat "<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">
\n<tr><td>" textblock "</table>")
))
(defun make-html-table (sep)
"Turn the current paragraph into a HTML table.
The “current paragraph” is defined as having empty lines before and
after the block of text the curson is on.
For example:
a•b•c
1•2•3
this•and•that
with “•” as separator, becomes
<table border=\"1\" cellpadding=\"5\" cellspacing=\"0\">
<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>this</td><td>and</td><td>that</td></tr>
</table>"
(interactive "sEnter string pattern for column separation:")
(let (bds p1 p2 myStr)
(setq bds (bounds-of-thing-at-point 'paragraph))
(setq p1 (+ (car bds) 1))
(setq p2 (cdr bds))
(setq myStr (buffer-substring-no-properties p1 p2))
(delete-region p1 p2)
(insert (make-html-table-string myStr sep) "\n")
))
Full explanation is here:
http://xahlee.org/emacs/elisp_make-html-table.html
Xah
···@xahlee.org
∑ http://xahlee.org/
☄
······@gmail.com wrote:
> The Problem
>
> a b c
> 1 2 3
>
> after, pressing a button, it should become:
>
> <table border="1" cellpadding="5" cellspacing="0">
> <tr><td>a</td><td>b</td><td>c</td></tr>
> <tr><td>1</td><td>2</td><td>3</td></tr>
> <tr><td>this</td><td>and</td><td>that</td></tr>
> </table>
Indeed, I see the problem.
Who is responsible for html again?
CNR,
Peter
Peter Hildebrandt <·················@gmail.com> writes:
> ······@gmail.com wrote:
>> The Problem
>> a b c
>> 1 2 3
>> after, pressing a button, it should become:
>> <table border="1" cellpadding="5" cellspacing="0">
>> <tr><td>a</td><td>b</td><td>c</td></tr>
>> <tr><td>1</td><td>2</td><td>3</td></tr>
>> <tr><td>this</td><td>and</td><td>that</td></tr>
>> </table>
>
> Indeed, I see the problem.
>
> Who is responsible for html again?
Tim Berners-Lee.
Responsible of having designed a SGML DTD without thinking about how
it would be used. This started from a good movement, since SGML has
good properties for perennial document management, but unfortunately,
the HTML DTD had some defects, and overall, people started to write
HTML themselves instead of leaving it to SGML editors and toolchains.
Well, a case of not applying the worse-is-better rule, I guess, and
now see the results...
--
__Pascal Bourguignon__
P� Tue, 29 Apr 2008 19:00:12 +0200, skrev Pascal J. Bourguignon
<···@informatimago.com>:
> Peter Hildebrandt <·················@gmail.com> writes:
>
>> ······@gmail.com wrote:
>>> The Problem
>>> a b c
>>> 1 2 3
>>> after, pressing a button, it should become:
>>> <table border="1" cellpadding="5" cellspacing="0">
>>> <tr><td>a</td><td>b</td><td>c</td></tr>
>>> <tr><td>1</td><td>2</td><td>3</td></tr>
>>> <tr><td>this</td><td>and</td><td>that</td></tr>
>>> </table>
>>
>> Indeed, I see the problem.
>>
>> Who is responsible for html again?
>
> Tim Berners-Lee.
>
> Responsible of having designed a SGML DTD without thinking about how
> it would be used. This started from a good movement, since SGML has
> good properties for perennial document management, but unfortunately,
> the HTML DTD had some defects, and overall, people started to write
> HTML themselves instead of leaving it to SGML editors and toolchains.
> Well, a case of not applying the worse-is-better rule, I guess, and
> now see the results...
>
>
Sidenote:
In an amazing turn of events IE8 is more or less standards compliant per
default.
CSS (W3C) and JavaScript (ECMA) is consistent with standards and other
browsers.
Netscape and Opera already are so in a way it will soon be a lot simpler..
--------------
John Thingstad
In article <·················@pandora.alfanett.no>,
John Thingstad <·······@online.no> wrote:
...
>
>Sidenote:
>
>In an amazing turn of events IE8 is more or less standards compliant per
>default.
>CSS (W3C) and JavaScript (ECMA) is consistent with standards and other
>browsers.
>Netscape and Opera already are so in a way it will soon be a lot simpler..
>
>--------------
>John Thingstad
Firefox?
On May 2, 6:50 am, harven <······@free.fr> wrote:
> As a sidenote, you could also use
> M-x table-capture
> M-x table-generate-source
> to build html or latex tables fast.
interesting. table-capture does ascii-art styled table.
table-generate-source seems to be able to generate html table
according to its online doc, but how do i use it? it's online doc is
rather not clear. It just give me error: "if: Table not found here".
Thanks.
Xah
···@xahlee.org
∑ http://xahlee.org/
☄
On May 2, 4:41 pm, ·······@gmail.com" <······@gmail.com> wrote:
> On May 2, 6:50 am, harven <······@free.fr> wrote:
>
> > As a sidenote, you could also use
> > M-x table-capture
> > M-x table-generate-source
> > to build html or latex tables fast.
>
> interesting. table-capture does ascii-art styled table.
>
> table-generate-source seems to be able to generate html table
> according to its online doc, but how do i use it? it's online doc is
> rather not clear. It just give me error: "if: Table not found here".
>
> Thanks.
>
> Xah
> ····@xahlee.org
> ∑http://xahlee.org/
>
> ☄
Have a look at the emacs wiki for a worked-out example.
http://www.emacswiki.org/cgi-bin/emacs-en/TableMode
harven <······@free.fr> wrote:
«
As a sidenote, you could also use
M-x table-capture
M-x table-generate-source
to build html or latex tables fast.
»
Xah wrote:
«...but how do i use it? »
harven wrote:
«Have a look at the emacs wiki for a worked-out example.
http://www.emacswiki.org/cgi-bin/emacs-en/TableMode »
Thank you very much. That is superb.
Xah
···@xahlee.org
∑ http://xahlee.org/
☄