From: ······@gmail.com
Subject: elisp: turn current paragraph into html table
Date: 
Message-ID: <4cedb296-36c2-4e53-86a3-adf5ed156e74@n1g2000prb.googlegroups.com>
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/

☄

From: Peter Hildebrandt
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <4816e390$0$90268$14726298@news.sunsite.dk>
······@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
From: Pascal J. Bourguignon
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <7cabjc4mw3.fsf@pbourguignon.anevia.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...


-- 
__Pascal Bourguignon__
From: John Thingstad
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <op.uaduzqgxut4oq5@pandora.alfanett.no>
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
From: David Combs
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <g0nutn$2jm$1@reader2.panix.com>
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?
From: harven
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <02d44e13-728e-48bc-8190-c4b5cbe784ca@m36g2000hse.googlegroups.com>
As a sidenote, you could also use
M-x table-capture
M-x table-generate-source
to build html or latex tables fast.
From: ······@gmail.com
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <88bde988-b8be-4f73-88dc-fcebee1a72e3@k10g2000prm.googlegroups.com>
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/

☄
From: harven
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <76a8a5f9-f501-400b-a6ed-3ff9c517a756@2g2000hsn.googlegroups.com>
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
From: ······@gmail.com
Subject: Re: elisp: turn current paragraph into html table
Date: 
Message-ID: <f5821ae6-d937-477f-b859-57b675c39f55@c19g2000prf.googlegroups.com>
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/

☄