项目作者: jinjor

项目描述 :
Parse HTML in Elm!
高级语言: Elm
项目地址: git://github.com/jinjor/elm-html-parser.git
创建时间: 2016-08-30T17:00:32Z
项目社区:https://github.com/jinjor/elm-html-parser

开源协议:BSD 3-Clause "New" or "Revised" License

下载


Note

For anyone who is using Elm 0.19 and blocked by this library, consider using hecrj/html-parser for now. It seems still WIP but passed the same test cases. I think that means the most difficult part of the HTML spec should be already covered (e.g. <li> does not need a closing tag). Also, it uses the official parser elm/parser which I was planning to use in the newer version. So I think contributing to hecrj/html-parser is the fastest path for everyone getting happy :)

elm-html-parser

Build Status

Parse HTML in Elm! (DEMO)

Parse

  1. import HtmlParser as HtmlParser exposing (..)
  2. parse "text" == [ Text "text" ]
  3. parse "<h1>Hello<br>World</h1>"
  4. == [ Element "h1" [] [ Text "Hello", Element "br" [] [], Text "World" ] ]
  5. parse """<a href="http://example.com">Example</a>"""
  6. == [ Element "a" [("href", "http://example.com")] [ Text "Example" ] ]

Query

  1. import HtmlParser exposing (..)
  2. import HtmlParser.Util exposing (..)
  3. table = """
  4. <table border=0 cellpadding=0 cellspacing=0 width=216 style='border-collapse:
  5. collapse;width:162pt'>
  6. <!--StartFragment-->
  7. <col width=72 span=3 style='width:54pt'>
  8. <tr height=18 style='height:13.5pt'>
  9. <td height=18 align=right width=72 style='height:13.5pt;width:54pt'>1</td>
  10. <td align=right width=72 style='width:54pt'>2</td>
  11. <td align=right width=72 style='width:54pt'>3</td>
  12. </tr>
  13. <tr height=18 style='height:13.5pt'>
  14. <td height=18 class=xl69 align=right style='height:13.5pt'>2</td>
  15. <td class=xl66 align=right>3</td>
  16. <td align=right>4</td>
  17. </tr>
  18. <!--EndFragment-->
  19. </table>
  20. """
  21. ( parse table
  22. |> getElementsByTagName "tr"
  23. |> mapElements
  24. (\_ _ innerTr ->
  25. innerTr
  26. |> mapElements (\_ _ innerTd -> textContent innerTd)
  27. |> String.join "\t"
  28. |> String.trim
  29. )
  30. |> String.join "\n"
  31. ) == "1\t2\t3\n2\t3\t4"

LICENSE

BSD3