# Copyright (C) 2003-2006 Kouichirou Eto, All rights reserved. # This is free software with ABSOLUTELY NO WARRANTY. # You can redistribute it and/or modify it under the terms of the GNU GPL 2. $LOAD_PATH << 'compat' unless $LOAD_PATH.include? 'compat' require 'htree' $LOAD_PATH.unshift '..' unless $LOAD_PATH.include? '..' require 'qwik/htree-to-wabisabi' require 'qwik/wabisabi-template' require 'qwik/util-string' module Qwik class HtmlToWabisabi def self.parse(str) html = ""+str+"" html = html.normalize_newline htree = HTree(html) first_child = htree.children[0] wabisabi = first_child.to_wabisabi wabisabi = wabisabi.inside return wabisabi end end end if $0 == __FILE__ require 'qwik/testunit' $test = true end if defined?($test) && $test class TestHtmlToWabisabi < Test::Unit::TestCase def ok(e, s) ok_eq(e, Qwik::HtmlToWabisabi.parse(s)) end def test_ref #ok(["\240"], " ") #ok(["?"], " ") ok(["<"], "<") ok([">"], ">") end def test_html_parser ok([], '') ok(['a'], 'a') # Div element can contain another div element. ok([[:div, 't1', [:div, 't2']]], "
t1
t2") # Span element can not contain div element. ok([[:span, 't1'], [:div, 't2']], "t1
t2") # Fix the order. ok([[:b, [:i, 't']]], "t") # You can use incomplete html also. ok([[:ul, [:li, 't1'], [:li, 't2']]], "
  • t1
  • t2") # test_long_html html = <<'EOT'

    書式一覧簡易版

    詳細な説明はTextFormatをごらんください。

    見出し2

    見出し3

    見出し4
    見出し5
    • 箇条書レベル1
      • 箇条書レベル2
        • 箇条書レベル3
    1. 順序リスト1
      1. 順序リスト2
        1. 順序リスト3
    整形済みテキスト。

    引用。

    Wiki
    書き込み可能なWebページ
    QuickML
    簡単に作れるメーリングリストシステム
    項目1-1 項目1-2 項目1-3
    項目2-1 項目2-2 項目2-3

    強調さらに強調取り消し線 new FrontPage Yahoo!

    EOT result = [[:h2, "書式一覧簡易版"], "\n", [:p, "詳細な説明は", [:a, {:href=>'TextFormat.html'}, 'TextFormat'], "をごらんください。"], "\n", [:h3, "見出し2"], "\n", [:h4, "見出し3"], "\n", [:h5, "見出し4"], "\n", [:h6, "見出し5"], "\n", [:ul, "\n", [:li, "箇条書レベル1\n", [:ul, "\n", [:li, "箇条書レベル2\n", [:ul, "\n", [:li, "箇条書レベル3"]]]]]], "\n", [:ol, "\n", [:li, "順序リスト1\n", [:ol, "\n", [:li, "順序リスト2\n", [:ol, "\n", [:li, "順序リスト3"]]]]]], [:pre, "整形済みテキスト。"], "\n", [:blockquote, "\n", [:p, "引用。"]], "\n", [:dl, "\n", [:dt, "Wiki\n"], [:dd, '書き込み可能なWebページ'+"\n"], [:dt, "QuickML\n"], [:dd, "簡単に作れるメーリングリストシステム"]], "\n", [:table, "\n", [:tbody, "\n", [:tr, "\n", [:td, "項目1-1"], "\n", [:td, "項目1-2"], "\n", [:td, "項目1-3"]], "\n", [:tr, "\n", [:td, "項目2-1"], "\n", [:td, "項目2-2"], "\n", [:td, "項目2-3"]]]], "\n", [:p, [:em, "強調"], "、", [:strong, "さらに強調"], "、", [:del, "取り消し線"], ' ', [:img, {:alt=>'new', :src=>'http://example.com/.theme/new.png'}], ' ', [:a, {:href=>'FrontPage.html'}, 'FrontPage'], ' ', [:a, {:href=>'http://www.yahoo.co.jp/'}, "Yahoo!"]], [:plugin, {:method=>'recent', :param=>'1'}], "\n"] ok(result, html) end end end