2011-07-07

HTML Table Examples with colgroup and col

Perm url with updates: http://xahlee.org/js/html_table_colgroup.html

HTML Table Examples with colgroup and col

Xah Lee, 2011-07-07

HTML table has a “colgroup” tag. It is used to indicate that several columns are a group. It does not change the rendering of the table. However, it is convenient to use it so that you can use CSS on just one tag, instead of adding a class to every “th” tag. Example:

1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9
2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8 2,9

Here's the source code:

<table border="1">
<colgroup span="1" style="background-color:blue"></colgroup>
<colgroup span="3" style="background-color:pink"></colgroup>
<colgroup span="2" style="background-color:yellow"></colgroup>
<colgroup span="3" style="background-color:gray"></colgroup>
<tr>
<td>1,1</td>
<td>1,2</td>
<td>1,3</td>
<td>1,4</td>
<td>1,5</td>
<td>1,6</td>
<td>1,7</td>
<td>1,8</td>
<td>1,9</td>
</tr>

<tr>
<td>2,1</td>
<td>2,2</td>
<td>2,3</td>
<td>2,4</td>
<td>2,5</td>
<td>2,6</td>
<td>2,7</td>
<td>2,8</td>
<td>2,9</td>
</tr>
</table>

The “colgroup” tag must come before any {tr, thead, tbody, tfoot}.

Using “col” tag

Alternatively, you can also use the “col” tag instead of {colgroup with span}.

1,1 1,2 1,3 1,4 1,5 1,6 1,7 1,8 1,9
2,1 2,2 2,3 2,4 2,5 2,6 2,7 2,8 2,9

Here's the relevant source code:

<colgroup style="background-color:blue"><col span="1"></col></colgroup>
<colgroup style="background-color:pink"><col span="3"></col></colgroup>
<colgroup style="background-color:yellow"><col span="2"></col></colgroup>
<colgroup style="background-color:gray"><col span="3"></col></colgroup>

The “col” tag must always be used inside “colgroup”.

Alternatively, you can just repeat the “col” tag instead of using the “span” attribute. For example, write:

<colgroup><col></col><col></col></colgroup>

instead of:

<colgroup><col span="2"></col></colgroup>

Browser Support

The {colgroup, col} tags are supported in all major browsers as of 2011-07.

See also:

2011-07-04

HTML5 “ruby” Tag

Perm url with updates: http://xahlee.org/js/html5_ruby_tag.html

HTML5 “ruby” Tag

HTML5 has {ruby, rt, rp} tags. These are used for pronunciation markup for Asian languages (mostly Japanese, sometimes Chinese). This page show examples of how to use it.

Following is HTML code for ruby annotation of the chinese characters 漢字, with the japanese pronunciation {かん,じ}.

<ruby>漢<rt>かん</rt>字<rt>じ </rt></ruby>

Here's what your browser shows: かんじ 

If your browser supports ruby annotation, the pronunciation should be rendered in small font above the chinese characters.

The following are 2 more examples. The first one uses the pinyin pronunciation system, the second uses zhuyin. (See: Zhuyin (bopomofo), Pinyin, IPA, Comparison.)

<ruby>汉<rt>hàn</rt>字<rt>zì </rt></ruby>
<ruby>漢<rt>ㄏㄢˋ</rt>字<rt>ㄗˋ </rt></ruby>

what your browser shows: hàn, ㄏㄢˋㄗˋ 

The “rp” Tag

The “rp” tag is used to add parenthesis around the pronunciation symbols for browsers that does not support ruby.

  • If the browser does support ruby, then “rp” and its content is ignored.
  • If the browser does NOT understand any of the ruby tags, then normally it'll just ignore the tag but display the tag's text content, which results in showing pronunciation inside parenthesis.

Example:

<ruby>
漢 <rp>(</rp><rt>かん</rt><rp>)</rp>
字 <rp>(</rp><rt>じ</rt><rp>)</rp>
</ruby>

what your browser shows: (かん)()

Browser Support

As of today (2011-07), the browsers that support ruby are: {Google Chrome (v12), Safari (v5.0.5), IE9}. Firefox 5 and Opera 11.50 are fails.

Back to HTML5 Tags.

2011-07-03

Emacs Lisp: Processing HTML: Transform Tags to HTML5 “figure” and “figcaption” Tags

Perm url with updates: http://xahlee.org/emacs/elisp_batch_html5_tag_transform.html

Emacs Lisp: Processing HTML: Transform Tags to HTML5 “figure” and “figcaption” Tags

Xah Lee, 2011-07-03

Another triumph of using elisp for text processing over {perl,python}.

The Problem

Summary

I want batch transform the image tags in 5 thousand html files to use HTML5's new “figure” and “figcaption” tags.

I want to be able to view each change interactively, while optionally give it a “go ahead” to do the whole job in batch.

Interactive eye-ball verification on many cases lets me be reasonably sure the transform is done correctly. It also lets me see whether i want to push forward with this change.

Detail

HTML5 has the following new tags: “figure” and “figcaption”. They are used like this:

<figure>
<img src="cat.jpg" alt="my cat" width="167" height="106">
<figcaption>my cat!</figcaption>
</figure>

(For detail, see: HTML5 “figure” & “figurecaption” Tags Browser Support)

On my website, i used a similar structure. They look like this:

<div class="img">
<img src="cat.jpg" alt="my cat" width="167" height="106">
<p class="cpt">my cat!</p>
</div>

So, i want to replace them with the HTML5's new tags. This can be done with a regex. Here's the “find” regex:

<div class="img">
?<img src="http://xahlee.org/emacs/\([^"]+?\)" alt="\([^"]+?\)" width="\([0-9]+?\)" height="\([0-9]+?\)">?
<p class="cpt">\([^<]+?\)</p>
?</div>

Here's the replacement string:

<figure>
<img src="http://xahlee.org/emacs/\1" alt="\2" width="\3" height="\4">
<figcaption>\5</figcaption>
</figure>

Then, you can use “find-dired” and dired's “dired-do-query-replace-regexp” to work on your 5 thousand pages. Nice. (See: Emacs: Interactively Find & Replace String Patterns on Multiple Files.)

However, the problem here is more complicated. There may be more than one image per group. Also, the caption part may also contain complicated html. Here's some examples:

<div class="img">
<img src="cat1.jpg" alt="my cat" width="200" height="200">
<img src="turtle.jpg" alt="my turtle" width="200" height="200">
<p class="cpt">my cat and my turtle</p>
</div>
<div class="img">
<img src="jamie_cat.jpg" alt="jamie's cat" width="167" height="106">
<p class="cpt">jamie's cat! Her blog is <a href="http://example.com/jamie/">http://example.com/jamie/</a></p>
</div>

So, a solution by regex is out.

Solution

The solution is pretty simple. Here's the major steps:

  • Use “find-lisp-find-files” to traverse a dir.
  • For each file, open it.
  • Search for the string <div class="img">
  • Use “sgml-skip-tag-forward” to jump to its closing tag.
  • Save the positions of these tag begin/end positions.
  • Ask user if she wants to replace. If so, do it. (using “delete-region” and “insert”)
  • Repeat.

Here's the code:

;; -*- coding: utf-8 -*-
;; 2011-07-03
;; replace image tags to use html5's “figure”  and “figcaption” tags.

;; Example. This:
;; <div class="img">…</div>
;; should become this
;; <figure>…</figure>

;; do this for all files in a dir.

;; rough steps:
;; find the <div class="img">
;; use sgml-skip-tag-forward to move to the ending tag.
;; save their positions.
;; ask user whether to replace, if so, delete them and insert new string

(defun my-process-file (fpath)
  "process the file at fullpath FPATH ..."
  (let (mybuff p1 p2 p3 p4 )
    (setq mybuff (find-file fpath))

    (widen)
    (goto-char 0) ;; in case buffer already open

    (while (search-forward "<div class=\"img\">" nil t)
      (progn
        (setq p2 (point) )
        (backward-char 17) ; beginning of “div” tag
        (setq p1 (point) )

        (forward-char 1)
        (sgml-skip-tag-forward 1) ; move to the closing tag
        (setq p4 (point) )
        (backward-char 6) ; beginning of the closing div tag
        (setq p3 (point) )
        (narrow-to-region p1 p4) 

        (when (y-or-n-p "replace?")
          (progn 
            (delete-region p3 p4 )
            (goto-char p3)
            (insert "</figure>")

            (delete-region p1 p2 )
            (goto-char p1)
            (insert "<figure>")
            (widen) ) ) ) )

    (when (not (buffer-modified-p mybuff)) (kill-buffer mybuff) )

    ) )

(require 'find-lisp)


(let (outputBuffer)
  (setq outputBuffer "*xah img/figure replace output*" )
  (with-output-to-temp-buffer outputBuffer 
    (mapc 'my-process-file (find-lisp-find-files "~/web/xahlee_org/emacs/" "\\.html$"))
    (princ "Done deal!")
    ) )

Seems pretty simple right?

The “p1” and “p2” variables are the positions of start/end of <div class="img">. The “p3” and “p4” is the start/end of its closing tag </div>.

We also used a little trick with “widen” and “narrow-to-region”. It lets me see just the part that i'm interested. It narrows to the beginning/end of the div.img. This makes eye-balling a bit easier.

The real time-saver is the “sgml-skip-tag-forward” function from “html-mode”. Without that, one'd have to write a mini-parser to deal with html's nested ways to be able to locate the proper ending tag.

Using the above code, i can comfortably eye-ball and press “y” at the rate of about 5 per second. That makes 300 replacements per minute. I have 5000+ files. If we assume there are 6k replacement to be made, then at 5 per second means 20 minutes sitting there pressing “y”. Quite tiresome.

So, now, the next step is simply to remove the asking (y-or-n-p "replace?"). Or, if i'm absolutely paranoid, i can make emacs write into a log buffer for every replacement it makes (together with the file path). When the batch replacement is done (probably takes 1 or 2 minutes), i can simply scan thru the log to see if any replacement went wrong. For a example of that, see: Emacs Lisp: Multi-Pair String Replacement with Report.

Also note that i left each changed file unsaved in emacs. If i decided i didn't want to commit the changes, i can exit emacs without saving. Or, i can go to “ibuffer” and press 3 keys to save and close them all 【*uS】. But if you want them saved with elisp, you can just add (save-buffer). Note that emacs automatically makes a backup~ of the original files if you haven't turned that off.

But what about replacing <p class="cpt">…</p> with <figcaption>…</figcaption>?

I simply copy-pasted the above code into a new file, and make changes in 4 places. So, the replacing figcaption part is done in a separete second batch job. Of course, one could spend extra hour to make the code do them both in one pass, but that extra time of thinking & coding isn't worthwhile for this one-time job.

I ♥ Emacs, do you?

Change in Current Buffer

Here's the code that changes both {div.img, p.cpt} to {figure, figcaption} in one shot, on the current buffer. It output the changes to a temp buffer, so you can scan it.

(defun xah-fix-wrap-img-figure ()
  "Change current buffer's <div class=\"img\"> to <figure> and <p class=\"cpt\"> to <figcaption>."
  (interactive)

  (save-excursion 
    (let (p1 p2 p3 p4 
             mystr
             ξchanges
             (changedItems '())
             (mybuff (current-buffer))
             )

      (goto-char (point-min)) ;; in case buffer already open
      (while (search-forward "<div class=\"img\">" nil t)
        (progn
          (setq p2 (point) )
          (backward-char 17)
          (setq p1 (point) )

          (forward-char 1)
          (sgml-skip-tag-forward 1)
          (setq p4 (point) )
          (backward-char 6)
          (setq p3 (point) )

          (when t
            (setq mystr (buffer-substring-no-properties p1 p4))
            (setq changedItems (cons mystr changedItems ) )
            
            (progn 
              (delete-region p3 p4 )
              (goto-char p3)
              (insert "</figure>")

              (delete-region p1 p2 )
              (goto-char p1)
              (insert "<figure>")
              (widen) )
            ) ) )

      (goto-char (point-min)) ;; in case buffer already open
      (while (search-forward "<p class=\"cpt\">" nil t)
        (progn
          (setq p2 (point) )
          (backward-char 15)
          (setq p1 (point) )

          (forward-char 1)
          (sgml-skip-tag-forward 1)
          (setq p4 (point) )
          (backward-char 4)
          (setq p3 (point) )

          (when t
            (setq mystr (buffer-substring-no-properties p1 p4))
            (setq changedItems (cons mystr changedItems ) )
            
            (progn 
              (delete-region p3 p4 )
              (goto-char p3)
              (insert "</figcaption>")

              (delete-region p1 p2 )
              (goto-char p1)
              (insert "<figcaption>")
              (widen) )
            ) ) )

      (with-output-to-temp-buffer "*changed items*" 
        (mapc (lambda ( ξchanges) (princ ξchanges) (princ "\n\n") ) changedItems)
        (set-buffer "*changed items*")
        (funcall 'html-mode)
        (set-buffer mybuff)
        ) )) )

Are You Intelligence Enough to Understand HTML5?

Perm url with updates: http://xahlee.org/UnixResource_dir/writ/html5_vs_intelligence.html

Are You Intelligent Enough to Understand HTML5?

Xah Lee, 2011-07-03

Are you intelligent?

Check. I have SAT score of {600/verbal, 660/math} to prove it. Was a member of Mensa in ~1992. (I exited Mensa because i didn't want to pay annual membership fee.)

Do you have years of experience working with HTML?

Check. My website xahlee.org is 5 thousand pages of hand-crafted html, started in 1997. More than three thousand of those pages are written, typed, word by word, tag by tag, in a text editor. They are strictly correct, passing W3C's HTML validator.

Do you have good understanding of mathematical logic?

Check. I have studied math for over a decade, and am especially interested in formal logic. I've written several articles about it. By Luck, I've also done a stint as a lecturer for graduate math students on math visualization programing. (See: Math Notations, Computer Languages, and the “Form” in Formalism.)

So, you are reading HTML5 spec — the EZ edition for web authors. For example, there's this subsection on content models: HTML5 Content models @ dev.w3.org. Do you understand it?

No.

html5 content models categories diagram

HTML5 content models categories diagram

Do you read Slashdot, Reddit, Hacker News regularly, and you are well acquainted with bleeding-edge practices of programing, and your peers respectfully refer you as a hacker? You often help others with stern advices of HTML semantics vs representation, about big O of algorithms and Turing-complete of languages, about proper software engineering practices such as {design patterns, unit testing, eXtreme Programing}, and encourage others to read docs such as RFC and wisdoms such as the unix philosophy, for a world of better software?

No, YOU are half-assed moron, the mud in a puddle, the foam in a pisspot, the shit in cesspit. You are a scumbag — a bag that gathers scum, and won't stop at that.

Go shove your unit testing, your “patterns”, your UML, your eXtreme Programing, your big O turing-complete verbiage into ya ass. Shove your perl sigil more than one way. Pythonic your face. Unix philosophy your pipe. Insert your lisp cons in your colon, tail recursion your hindquarters. Roll your emacs vi command-line interface gospels in your scrotum. Go spit your drivels in your mom's pussy. Fuck you. That is: “F” “U” “C” “K” “Y” “O” “U” — Fuck YOU!

HTML5 “time” Tag

Perm url with updates: http://xahlee.org/js/html5_time_tag.html

HTML5 “time” Tag

Xah Lee, 2011-07-03

The “time” tag is used to represent date/time combination. Examples:

<footer>Published <time pubdate datetime="2011-07-03">07/03</time>.</footer>
I need this <time datetime="2011-07-03T12:28:57-07:00">now</time>!
Captain's log, date <time>2011-07-03T12:51:02-07:00</time>.

Note: if you do not include the “datetime” attribute, then your “time” tag's content must follow the same format used by the “datetime” attribute.

“datetime” Attribute

“datetime” attribute encodes the precise date or date/time. The format must be exact, and must include at least “yyyy-mm-dd”. (time info is optional) Examples:

… mom's birthday <time datetime="2011-07-03">July 3rd</time> …
… had piano lesson at <time datetime="2011-07-03T13:00">1 pm</time> …
… the bomb went off at <time datetime="2011-07-03T12:46:03-07:00">12:46:03 PST</time> …

The “datetime” attribute is optional. But if not present, then the “time” tag's content must use the the same fomat used by “datetime” attribute. e.g. yyyy-mm-ddThh:mm:ss-hh:mm.

Sample Correct Formats

  • 2011-07-03
  • 2011-07-03T12:58
  • 2011-07-03T12:58:46

global datetime stamp (with UTC offset)

  • 2011-07-03T12:58-07:00
  • 2011-07-03T12:58:46-07:00
  • 2011-07-03T12:58:46.31-07:00

Wrong Examplez

  • Wrong! <time>today</time>
  • Wrong! <time>July 3</time>
  • Wrong! <time>07/03/11</time>
  • Wrong! <time>Sun Jul 03 13:20:16 2011</time> (incorrect format)
  • Wrong! <time>12:50</time> (requires “yyyy-mm-dd” at least)

“pubdate” Attribute

“pubdate” boolean attribute can be included. If present, that means your time tag represent the time your article is published. Example:

<time pubdate>2011-07-03T13:05:28-07:00</time>
<time pubdate datetime="2011-07-03T13:05:28-07:00">July 03</time>

Note that “pubdate” does not take any values. It is wrong to say pubdate="…".

Back to HTML5 Tags.

July4th Flag Chicks

Gisele Bundchen flag 2

Gisele Bundchen

Tomorrow is July 4th. Remember to take photos of any flag-clad cuties, and any flag things. Send it to me. See: 〈Banners & Damsels & Mores〉 @ http://xahlee.org/Periodic_dosage_dir/lanci/lanci.html

2011-07-02

Young Girls, Feet and Makeup, Sexual Mores

Perm url with updates: http://xahlee.org/sex/feet_and_makeup.html

Young Girls, Feet and Makeup, Sexual Mores

Xah Lee, 2011-07-02

Cute Girl Showing Off her feet

“My Barefoot Demi Pointe Work 2010” by Ally Olsen (aka allolsen).

Yeah, showing off, but oohh, she's soo cute, and such a pretty feet.

Well, she's just innocent, and wanted to do video of her ballet study. It's not really showing off. But then, you wonder, why you don't see ballet boys doing such videos? Of course, the answer has to do with the fundamental nature of sex. It's the same reason that syncronized swimming and cheerleaders are done by women, nay, nubile girls, while such performances by men went nowhere.

Chatty Girl on Makeup

“TAG! My Makeup Story” by juicystar07

Not sure what to say.

It seems to me, American Girls has this repressed cult thing about sexuality. Like, she has to defend makeup. While, Latin America (Spanish), French, cultures, of which i had some contact, are quite open and natural about sex. French or Spanish girls EXUDES feminity. You'll see children at age 10 or so wearing makup and toe nail paints, talking about boyfriends, and with parent's help. On the other hand, Asian girls (e.g. Taiwan, Japan), you don't fucking wear makup or nail paints utill you are way moved out of your parent's house. (yet, Asian sexual culture has some weird fetishes, as can be seen in Japanese animation (aka anime). I suppose it came from repressed sexuality.)

Medusa: eyes up here buddy

Update: 〈Xah's Arts Blog〉 http://xahlee.org/arts/blog.html (not posted here or linked due to Google adsense restrictions)

2011-07-01

HTML5 “address” Tag

Perm url with updates: http://xahlee.org/js/html5_address_tag.html

HTML5 “address” Tag

The “address” tag is for contact info of the article author or page's owner. Use it like this:

<address>
Contact <a href="../mary_lee.html">Mary Lee</a>.
</address>

The “address” tag can contain email or street address. For example:

<address>
Write to Mary at <a href="mailto:mary@example.com">mary@example.com</a>.
</address>

The main thing is that it marks up a contact info of the article. There's no strict structure of what content inside “address” tag should be. Also, the “address” tag is often conveniently placed inside “header” or “footer” tags. (See: HTML5 Page Article Tag and Page Structure Tags.)

Note: the “address” tag should NOT be used for arbitrary address that has nothing to do with the author or page owner.

Here's how your browser renders it:

Contact Xah Lee.

dev.w3.org

Back to HTML5 Tags.

HTML5 Page Article Tag and Page Structure Tags

New: HTML5 Page Article Tag and Page Structure Tags (tutorial)