diff --git a/README.md b/README.md index 6c45158..9b8a837 100644 --- a/README.md +++ b/README.md @@ -44,9 +44,9 @@ soup |> to_string;; - : string = "
World!
" ``` -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. diff --git a/example/README.md b/example/README.md new file mode 100644 index 0000000..04ccd68 --- /dev/null +++ b/example/README.md @@ -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` diff --git a/example/bin/dune b/example/bin/dune new file mode 100644 index 0000000..6596ca5 --- /dev/null +++ b/example/bin/dune @@ -0,0 +1,3 @@ +(executable + (name main) + (libraries lambdasoup)) diff --git a/example/bin/main.ml b/example/bin/main.ml new file mode 100644 index 0000000..6472754 --- /dev/null +++ b/example/bin/main.ml @@ -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" diff --git a/example/dune-project b/example/dune-project new file mode 100644 index 0000000..555629d --- /dev/null +++ b/example/dune-project @@ -0,0 +1,2 @@ +(lang dune 3.14) +(name lambdasoup_example) diff --git a/example/dune-workspace b/example/dune-workspace new file mode 100644 index 0000000..0d806ba --- /dev/null +++ b/example/dune-workspace @@ -0,0 +1,2 @@ +(lang dune 3.14) +(env (dev (flags :standard -warn-error -A))) diff --git a/example/example.html b/example/example.html new file mode 100644 index 0000000..45524d9 --- /dev/null +++ b/example/example.html @@ -0,0 +1,16 @@ + + + + +Para 1
More
Para 2
+