Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ soup |> to_string;;
- : string = "<p class=\"Hello\"><strong>World!</strong></p>"
```

For some more examples, see the Lambda Soup [postprocessor][postprocess] that
runs on Lambda Soup's own [documentation][docs] after it is generated by
`ocamldoc`.
For some more examples, see the [examples][examples] directory, and the Lambda
Soup [postprocessor][postprocess] that runs on Lambda Soup's own
[documentation][docs] after it is generated by `ocamldoc`.

The library is [tested][tests] thoroughly.

Expand Down
17 changes: 17 additions & 0 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# lambdasoup

This example is for the lambdasoup library

https://opam.ocaml.org/packages/lambdasoup

# Source file

`bin/main.ml`

# Building and running

`dune exec bin/main.exe`

# Cleaning up

`dune clean`
3 changes: 3 additions & 0 deletions example/bin/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(executable
(name main)
(libraries lambdasoup))
34 changes: 34 additions & 0 deletions example/bin/main.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
(* Extract text from HTML file, write to stdout *)

(* Bring $, $?, $$ opeators only into scope. *)
open Soup.Infix

(* Extract a title from an HTML file and print it. We use the optional forms
throughout. *)
let extract () =
let soup = Soup.parse (Soup.read_file "example.html") in
let title_node = soup $? "title" in
match title_node with
| Some tn ->
begin match Soup.leaf_text tn with
| Some lt -> Printf.printf "Title: %S\n" lt
| None -> Printf.printf "No leaf text in title."
end
| None -> Printf.printf "No leaf text"

(* Now the same with exception-raising forms. Much simpler, but coarser error
reporting. *)
let extract_exn () =
try
Printf.printf
"Title: %S\n"
(Soup.read_file "example.html" |> Soup.parse $ "title" |> Soup.R.leaf_text)
with
e ->
Printf.printf "Lambdasoup exception: %s\n" (Printexc.to_string e)

let () =
match Sys.argv with
| [|_; "extract"|] -> extract ()
| [|_; "extract_exn"|] -> extract_exn ()
| _ -> Printf.eprintf "lambdasoup example: unknown command line\n"
2 changes: 2 additions & 0 deletions example/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 3.14)
(name lambdasoup_example)
2 changes: 2 additions & 0 deletions example/dune-workspace
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
(lang dune 3.14)
(env (dev (flags :standard -warn-error -A)))
16 changes: 16 additions & 0 deletions example/example.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!doctype html>
<html lang=en>
<head>
<meta charset=utf-8>
<title>Lambdasoup example</title>
</head>
<body>
<p>Para 1 <br> More</p>
<p>Para 2</p>
<ul>
<li>a</li>
<li>b</li>
<li>c</li>
</ul>
</body>
</html>