package html2text import ( "bytes" "fmt" "io/ioutil" "path" "regexp" "strings" "testing" ) const ( destPath = "testdata" ) func TestParseUTF8(t *testing.T) { htmlFiles := []struct { file string keywordShouldNotExist string keywordShouldExist string }{ { "utf8.html", "学习之道:美国公认学习第一书title", "次世界冠军赛上,我几近疯狂", }, { "utf8_with_bom.xhtml", "1892年波兰文版序言title", "种新的波兰文本已成为必要", }, } for _, htmlFile := range htmlFiles { bs, err := ioutil.ReadFile(path.Join(destPath, htmlFile.file)) if err != nil { t.Fatal(err) } text, err := FromReader(bytes.NewReader(bs)) if err != nil { t.Fatal(err) } if !strings.Contains(text, htmlFile.keywordShouldExist) { t.Fatalf("keyword %s should exists in file %s", htmlFile.keywordShouldExist, htmlFile.file) } if strings.Contains(text, htmlFile.keywordShouldNotExist) { t.Fatalf("keyword %s should not exists in file %s", htmlFile.keywordShouldNotExist, htmlFile.file) } } } func TestStrippingWhitespace(t *testing.T) { testCases := []struct { input string output string }{ { "test text", "test text", }, { " \ttext\ntext\n", "text text", }, { " \na \n\t \n \n a \t", "a a", }, { "test text", "test text", }, { "test    text ", "test    text", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestParagraphsAndBreaks(t *testing.T) { testCases := []struct { input string output string }{ { "Test text", "Test text", }, { "Test text
", "Test text", }, { "Test text
Test", "Test text\nTest", }, { "

Test text

", "Test text", }, { "

Test text

Test text

", "Test text\n\nTest text", }, { "\n

Test text

\n\n\n\t

Test text

\n", "Test text\n\nTest text", }, { "\n

Test text
Test text

\n", "Test text\nTest text", }, { "\n

Test text
\tTest text

\n", "Test text\nTest text", }, { "Test text

Test text", "Test text\n\nTest text", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestTables(t *testing.T) { testCases := []struct { input string output string }{ { "
", "", }, { "
cell1cell2
", "cell1 cell2", }, { "
row1
row2
", "row1\nrow2", }, { `
cell1-1cell1-2
cell2-1cell2-2
`, "cell1-1 cell1-2\ncell2-1 cell2-2", }, { "_
cell
_", "_\n\ncell\n\n_", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestStrippingLists(t *testing.T) { testCases := []struct { input string output string }{ { "", "", }, { "_", "* item\n\n_", }, { "
  • item 1
  • item 2
  • \n_", "* item 1\n* item 2\n_", }, { "
  • item 1
  • \t\n
  • item 2
  • item 3
  • \n_", "* item 1\n* item 2\n* item 3\n_", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestLinks(t *testing.T) { testCases := []struct { input string output string }{ { ``, ``, }, { ``, ``, }, { ``, `( http://example.com/ )`, }, { `Link`, `Link`, }, { `Link`, `Link ( http://example.com/ )`, }, { `Link`, `Link ( http://example.com/ )`, }, { "\n\tLink\n\t", `Link ( http://example.com/ )`, }, { "Contact Us", `Contact Us ( contact@example.org )`, }, { "Link", `Link ( http://example.com:80/~user?aaa=bb&c=d,e,f#foo )`, }, { "Link", `Link ( http://example.com/ )`, }, { " Link ", `Link ( http://example.com/ )`, }, { "Link A Link B", `Link A ( http://example.com/a/ ) Link B ( http://example.com/b/ )`, }, { "Link", `Link ( %%LINK%% )`, }, { "Link", `Link ( [LINK] )`, }, { "Link", `Link ( {LINK} )`, }, { "Link", `Link ( [[!unsubscribe]] )`, }, { "

    This is link1 and link2 is next.

    ", `This is link1 ( http://www.google.com ) and link2 ( http://www.google.com ) is next.`, }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestImageAltTags(t *testing.T) { testCases := []struct { input string output string }{ { ``, ``, }, { ``, ``, }, { `Example`, ``, }, { `Example`, ``, }, // Images do matter if they are in a link { `Example`, `Example ( http://example.com/ )`, }, { `Example`, `Example ( http://example.com/ )`, }, { `Example`, `Example ( http://example.com/ )`, }, { `Example`, `Example ( http://example.com/ )`, }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestHeadings(t *testing.T) { testCases := []struct { input string output string }{ { "

    Test

    ", "****\nTest\n****", }, { "\t

    \nTest

    ", "****\nTest\n****", }, { "\t

    \nTest line 1
    Test 2

    ", "***********\nTest line 1\nTest 2\n***********", }, { "

    Test

    Test

    ", "****\nTest\n****\n\n****\nTest\n****", }, { "

    Test

    ", "----\nTest\n----", }, { "

    Test

    ", "****************************\nTest ( http://example.com/ )\n****************************", }, { "

    Test

    ", "Test\n----", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestBold(t *testing.T) { testCases := []struct { input string output string }{ { "Test", "*Test*", }, { "\tTest ", "*Test*", }, { "\tTest line 1
    Test 2
    ", "*Test line 1\nTest 2*", }, { "Test Test", "*Test* *Test*", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestDiv(t *testing.T) { testCases := []struct { input string output string }{ { "
    Test
    ", "Test", }, { "\t
    Test
    ", "Test", }, { "
    Test line 1
    Test 2
    ", "Test line 1\nTest 2", }, { "Test 1
    Test 2
    Test 3
    Test 4", "Test 1\nTest 2\nTest 3\nTest 4", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestBlockquotes(t *testing.T) { testCases := []struct { input string output string }{ { "
    level 0
    level 1
    level 2
    level 1
    level 0
    ", "level 0\n> \n> level 1\n> \n>> level 2\n> \n> level 1\n\nlevel 0", }, { "
    Test
    Test", "> \n> Test\n\nTest", }, { "\t
    \nTest
    ", "> \n> Test\n>", }, { "\t
    \nTest line 1
    Test 2
    ", "> \n> Test line 1\n> Test 2", }, { "
    Test
    Test
    Other Test", "> \n> Test\n\n> \n> Test\n\nOther Test", }, { "
    Lorem ipsum Commodo id consectetur pariatur ea occaecat minim aliqua ad sit consequat quis ex commodo Duis incididunt eu mollit consectetur fugiat voluptate dolore in pariatur in commodo occaecat Ut occaecat velit esse labore aute quis commodo non sit dolore officia Excepteur cillum amet cupidatat culpa velit labore ullamco dolore mollit elit in aliqua dolor irure do
    ", "> \n> Lorem ipsum Commodo id consectetur pariatur ea occaecat minim aliqua ad\n> sit consequat quis ex commodo Duis incididunt eu mollit consectetur fugiat\n> voluptate dolore in pariatur in commodo occaecat Ut occaecat velit esse\n> labore aute quis commodo non sit dolore officia Excepteur cillum amet\n> cupidatat culpa velit labore ullamco dolore mollit elit in aliqua dolor\n> irure do", }, { "
    LoremipsumCommodoidconsecteturpariatureaoccaecatminimaliquaadsitconsequatquisexcommodoDuisincididunteumollitconsecteturfugiatvoluptatedoloreinpariaturincommodooccaecatUtoccaecatvelitesselaboreautequiscommodononsitdoloreofficiaExcepteurcillumametcupidatatculpavelitlaboreullamcodoloremollitelitinaliquadoloriruredo
    ", "> \n> Lorem *ipsum* *Commodo* *id* *consectetur* *pariatur* *ea* *occaecat* *minim*\n> *aliqua* *ad* *sit* *consequat* *quis* *ex* *commodo* *Duis* *incididunt* *eu*\n> *mollit* *consectetur* *fugiat* *voluptate* *dolore* *in* *pariatur* *in* *commodo*\n> *occaecat* *Ut* *occaecat* *velit* *esse* *labore* *aute* *quis* *commodo*\n> *non* *sit* *dolore* *officia* *Excepteur* *cillum* *amet* *cupidatat* *culpa*\n> *velit* *labore* *ullamco* *dolore* *mollit* *elit* *in* *aliqua* *dolor* *irure*\n> *do*", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestIgnoreStylesScriptsHead(t *testing.T) { testCases := []struct { input string output string }{ { "", "", }, { "", "", }, { "", "", }, { "", "", }, { "", "", }, { "", "", }, { "", "", }, { "", "", }, { "", "", }, { `Title`, "", }, } for _, testCase := range testCases { assertString(t, testCase.input, testCase.output) } } func TestText(t *testing.T) { testCases := []struct { input string expr string }{ { `
  • New repository
  • `, `\* New repository \( /new \)`, }, { `hi
    hello google

    test

    List:

    `, `hi hello google \( https://google.com \) test List: \* Foo \( foo \) \* Barsoap \( http://www.microshwhat.com/bar/soapy \) \* Baz`, }, // Malformed input html. { `hi hello google test

    List:

    `, `hi hello google \( https://google.com \) test List: \* Foo \( foo \) \* Bar \( /\n[ \t]+bar/baz \) \* Baz`, }, } for _, testCase := range testCases { assertRegexp(t, testCase.input, testCase.expr) } } type StringMatcher interface { MatchString(string) bool String() string } type RegexpStringMatcher string func (m RegexpStringMatcher) MatchString(str string) bool { return regexp.MustCompile(string(m)).MatchString(str) } func (m RegexpStringMatcher) String() string { return string(m) } type ExactStringMatcher string func (m ExactStringMatcher) MatchString(str string) bool { return string(m) == str } func (m ExactStringMatcher) String() string { return string(m) } func assertRegexp(t *testing.T, input string, outputRE string) { assertPlaintext(t, input, RegexpStringMatcher(outputRE)) } func assertString(t *testing.T, input string, output string) { assertPlaintext(t, input, ExactStringMatcher(output)) } func assertPlaintext(t *testing.T, input string, matcher StringMatcher) { text, err := FromString(input) if err != nil { t.Error(err) } if !matcher.MatchString(text) { t.Errorf("Input did not match expression\n"+ "Input:\n>>>>\n%s\n<<<<\n\n"+ "Output:\n>>>>\n%s\n<<<<\n\n"+ "Expected output:\n>>>>\n%s\n<<<<\n\n", input, text, matcher.String()) } else { t.Logf("input:\n\n%s\n\n\n\noutput:\n\n%s\n", input, text) } } func Example() { inputHtml := ` My Mega Service

    Welcome to your new account on my service!

    Here is some more information:

    ` text, err := FromString(inputHtml) if err != nil { panic(err) } fmt.Println(text) // Output: // Mega Service ( http://mymegaservice.com/ ) // // ****************************************** // Welcome to your new account on my service! // ****************************************** // // Here is some more information: // // * Link 1: Example.com ( https://example.com ) // * Link 2: Example2.com ( https://example2.com ) // * Something else }