Skip to content

Commit

Permalink
s/pcdata/txt/
Browse files Browse the repository at this point in the history
  • Loading branch information
Drup committed Oct 28, 2018
1 parent 4f92f69 commit 7df50cc
Show file tree
Hide file tree
Showing 19 changed files with 104 additions and 88 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ TyXML is a library for building statically correct HTML5 and SVG documents:

```ocaml
open Tyxml
let to_ocaml = Html.(a ~a:[a_href "ocaml.org"] [pcdata "OCaml!"])
let to_ocaml = Html.(a ~a:[a_href "ocaml.org"] [txt "OCaml!"])
```

Tyxml can also be used with the standard HTML syntax, using the PPX:
Expand Down
16 changes: 8 additions & 8 deletions docs/manual-wiki/intro.wiki
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tyxml is a library for building statically correct HTML and SVG documents.

<<code language="ocaml"|
open Tyxml
let to_ocaml = Html.(a ~a:[a_href "ocaml.org"] [pcdata "OCaml!"])
let to_ocaml = Html.(a ~a:[a_href "ocaml.org"] [txt "OCaml!"])
>>

==@@id="usage"@@ Using TyXML ==
Expand Down Expand Up @@ -45,11 +45,11 @@ The first thing to understand about TyXML is that for most intents and purposes,
In this tutorial, we will build the [[https://github.com/ocsigen/tyxml/tree/master/examples/mini_website|Mini website]].
If you prefer the native HTML syntax, you can also use the <<a_manual chapter="ppx"|PPX syntax extension>> and consult the [[https://github.com/ocsigen/tyxml/tree/master/examples/mini_website_ppx|PPX mini website]].

Let us start by building the content of our website. For text, we use the {{{pcdata}}} combinator. In traditional Web fashion, we put everything in a ##div##.
Let us start by building the content of our website. For text, we use the {{{txt}}} combinator. In traditional Web fashion, we put everything in a ##div##.
<<code language="ocaml"|
let mycontent =
div [
pcdata "This is a fabulous content." ;
txt "This is a fabulous content." ;
]
>>

Expand All @@ -60,7 +60,7 @@ Our content is fabulous, but for the sake of CSS styling (and still in true Web
<<code language="ocaml"|
let mycontent =
div ~a:[a_class ["content"]] [
pcdata "This is a fabulous content." ;
txt "This is a fabulous content." ;
]
>>

Expand All @@ -71,8 +71,8 @@ In order to add a title to our fabulous content, we use the <<a_api text="h1"|va
<<code language="ocaml"|
let mycontent =
div ~a:[a_class ["content"]] [
h1 [pcdata "A fabulous title"] ;
pcdata "This is a fabulous content." ;
h1 [txt "A fabulous title"] ;
txt "This is a fabulous content." ;
]
>>

Expand All @@ -85,14 +85,14 @@ which accept, respectively, one child and zero children.
<<a_api text="title"|val Html_sigs.T.title>> is an example of a {{{unary}}}
combinator.
<<code language="ocaml"|
let mytitle = title (pcdata "A Fabulous Web Page")
let mytitle = title (txt "A Fabulous Web Page")
>>

====@@id="type-errors"@@ Interlude about type errors ====

However, what would happen if we were to try to put **bold** text in our title? This is not specification-compliant! Let's try it.
<<code language="ocaml"|
let mytitle = title (b [pcdata "A Bold Web Page"])
let mytitle = title (b [txt "A Bold Web Page"])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Error: This expression has type ([> Html_types.b ] as 'a) elt
but an expression was expected of type
Expand Down
22 changes: 11 additions & 11 deletions examples/basic_website/site_html.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Tyxml.Html

let this_title = title (pcdata "Your Cool Web Page")
let this_title = title (txt "Your Cool Web Page")

let image_box =
div ~a:[a_id "image_box"]
Expand All @@ -9,25 +9,25 @@ let image_box =
let links_box =
ul ~a:[a_class ["links_bar"]; a_id "links_bar"]
[li ~a:[a_id "home_click"]
[pcdata "My Musings"];
[txt "My Musings"];
li ~a:[a_id "about_click"]
[pcdata "About Me"];
[txt "About Me"];
li ~a:[a_id "blog_posts_click"]
[pcdata "Blog"];
[txt "Blog"];
li ~a:[a_id "hackathons_click"]
[pcdata "Hackathons"]]
[txt "Hackathons"]]

let common_footer =
footer ~a:[a_id "footer_box"]
[p [pcdata "This site was made with ";
a ~a:[a_href "http://ocaml.org"] [pcdata "OCaml"];
pcdata " and ";
a ~a:[a_href "https://www.gnu.org/software/emacs/"] [pcdata "emacs"]]]
[p [txt "This site was made with ";
a ~a:[a_href "http://ocaml.org"] [txt "OCaml"];
txt " and ";
a ~a:[a_href "https://www.gnu.org/software/emacs/"] [txt "emacs"]]]

let home_content =
div
[h2
[pcdata "Hello Coder"]]
[txt "Hello Coder"]]

let main_payload =
div ~a:[a_id "payload"]
Expand All @@ -43,7 +43,7 @@ let content_box =
common_footer]

let main_script =
script ~a:[a_src (Xml.uri_of_string "main.js")] (pcdata "")
script ~a:[a_src (Xml.uri_of_string "main.js")] (txt "")

let home_page_doc =
html (head this_title
Expand Down
2 changes: 1 addition & 1 deletion examples/basic_website_ppx/site_html.ml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
open Tyxml

let this_title = Html.pcdata "Your Cool Web Page"
let this_title = Html.txt "Your Cool Web Page"

let image_box = [%html
"<div id=image_box></div>"
Expand Down
6 changes: 3 additions & 3 deletions examples/mini_website/minihtml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ open Tyxml.Html

let mycontent =
div ~a:[a_class ["content"]] [
h1 [pcdata "A fabulous title"] ;
pcdata "This is a fabulous content." ;
h1 [txt "A fabulous title"] ;
txt "This is a fabulous content." ;
]

let mytitle = title (pcdata "A Fabulous Web Page")
let mytitle = title (txt "A Fabulous Web Page")

let mypage =
html
Expand Down
2 changes: 1 addition & 1 deletion examples/mini_website_ppx/minihtml.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ let%html mycontent = {|
|}


let mytitle = Html.pcdata "A Fabulous Web Page"
let mytitle = Html.txt "A Fabulous Web Page"

let%html mypage =
{|<html>
Expand Down
3 changes: 2 additions & 1 deletion lib/html_f.ml
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,8 @@ struct

let nav = star "nav"

let pcdata = Xml.pcdata
let txt s = Xml.pcdata s
let pcdata = txt

let entity = Xml.entity

Expand Down
24 changes: 15 additions & 9 deletions lib/html_sigs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ module type T = sig
are either {{!nullary}nullary}, {{!unary}unary} or {{!star}star},
depending on the number of children they accept.
Children are usually given as a list of elements.
{{!pcdata}pcdata} is used for text.
{{!txt}txt} is used for text.
[div [a [pcdata "Foo"]]]
[div [a [txt "Foo"]]]
is equivalent to
[<div><a>foo</a></div>]
Expand All @@ -57,7 +57,7 @@ module type T = sig
Attribute constructors are in section {{!attributes}attributes} and their name starts
with [a_]. Attributes are given to elements with the [~a] optional argument.
[a ~a:[a_href "ocsigen.org"] [pcdata "link!"]]
[a ~a:[a_href "ocsigen.org"] [txt "link!"]]
is equivalent to
[<a href="ocsigen.org">link!</a>]
Expand Down Expand Up @@ -679,7 +679,7 @@ module type T = sig

(** {2:elements Elements} *)

val pcdata : string wrap -> [> | pcdata] elt
val txt : string wrap -> [> | txt] elt

val html :
?a: ((html_attrib attrib) list) ->
Expand Down Expand Up @@ -993,13 +993,13 @@ module type T = sig
@see <http://www.w3schools.com/html/html_entities.asp> A tutorial on HTML entities.
@see <https://www.w3.org/TR/html5/syntax.html#named-character-references> The list of HTML entities.
*)
val entity : string -> [> | pcdata] elt
val entity : string -> [> | txt] elt

val space : unit -> [> | pcdata] elt
val space : unit -> [> | txt] elt

val cdata : string -> [> | pcdata] elt
val cdata_script : string -> [> | pcdata] elt
val cdata_style : string -> [> | pcdata] elt
val cdata : string -> [> | txt] elt
val cdata_script : string -> [> | txt] elt
val cdata_style : string -> [> | txt] elt


(** {3 Interactive} *)
Expand Down Expand Up @@ -1053,6 +1053,12 @@ module type T = sig

val ruby : ([< | ruby_attrib], [< | ruby_content_fun], [> | ruby]) star

(** {3 Deprecated} *)

val pcdata : string wrap -> [> | pcdata] elt
[@@ocaml.deprecated "Use txt instead"]
(** @deprecated Use txt instead *)

(** {2 Conversion with untyped representation} *)

val tot : Xml.elt -> 'a elt
Expand Down
1 change: 1 addition & 0 deletions lib/html_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -1122,6 +1122,7 @@ type flow5_without_sectioning_heading_header_footer =
Type for HTML for elements
*)
type pcdata = [ | `PCDATA ]
type txt = [ | `PCDATA ]

type notag

Expand Down
3 changes: 2 additions & 1 deletion lib/svg_f.ml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ struct

let foreignObject ?a children = Xml.node ?a "foreignObject" children

let pcdata s = Xml.pcdata s
let txt s = Xml.pcdata s
let pcdata = txt

(* generated *)
let a_version = string_attrib "version"
Expand Down
10 changes: 8 additions & 2 deletions lib/svg_sigs.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module type T = sig
are either {{!nullary}nullary}, {{!unary}unary} or {{!star}star},
depending on the number of children they accept.
Children are usually given as a list of elements.
{{!pcdata}pcdata} is used for text.
{{!txt}txt} is used for text.
The type variable ['a] is used to track the element's type. This
allows the OCaml typechecker to check SVG validity.
Expand Down Expand Up @@ -690,7 +690,7 @@ module type T = sig

(** {2:elements Elements} *)

val pcdata : string wrap -> [> | `PCDATA] elt
val txt : string wrap -> [> | txt] elt

val svg : ([< | svg_attr], [< | svg_content], [> | svg]) star

Expand Down Expand Up @@ -955,6 +955,12 @@ module type T = sig
?a: ((foreignobject_attr attrib) list) ->
Xml.elt list_wrap -> [> | foreignobject] elt

(** {3 Deprecated} *)

val pcdata : string wrap -> [> txt] elt
[@@ocaml.deprecated "Use txt instead"]
(** @deprecated Use txt instead *)

(** {2 Conversion with untyped representation} *)

val tot : Xml.elt -> 'a elt
Expand Down
1 change: 1 addition & 0 deletions lib/svg_types.mli
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ type rotate = float list


type pcdata = [ `PCDATA ]
type txt = [ | `PCDATA ]

(** {1 Element} *)

Expand Down
6 changes: 3 additions & 3 deletions lib/xml_iter.ml
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,18 @@ module Make(Xml : Xml_sigs.Iterable) = struct
| _ -> head in
List.map aux l

let rec fold of_empty of_comment of_pcdata of_encodedpcdata of_entity
let rec fold of_empty of_comment of_txt of_encodedpcdata of_entity
of_leaf of_node n =
match content n with
| Empty -> of_empty ()
| Comment s -> of_comment s
| PCDATA s -> of_pcdata s
| PCDATA s -> of_txt s
| EncodedPCDATA s -> of_encodedpcdata s
| Entity s -> of_entity s
| Leaf (name, attribs) -> of_leaf name attribs
| Node (name, attribs, elts) ->
of_node name attribs
(List.map (fold of_empty of_comment of_pcdata of_encodedpcdata of_entity of_leaf of_node) elts)
(List.map (fold of_empty of_comment of_txt of_encodedpcdata of_entity of_leaf of_node) elts)

let all_entities elt =
let f _ = [] in
Expand Down
12 changes: 6 additions & 6 deletions ppx/element_content.ml
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,28 @@ type assembler =

(* Helpers. *)

(* Given a parse tree [e], if [e] represents [_.pcdata s], where [s] is a string
(* Given a parse tree [e], if [e] represents [_.txt s], where [s] is a string
constant, evaluates to [Some s]. Otherwise, evaluates to [None]. *)
let to_pcdata = function
let to_txt = function
| [%expr[%e? {pexp_desc = Pexp_ident f; _}]
( [%e? {pexp_desc = Pexp_ident f2; _}] [%e? arg])] -> begin
match Longident.last f.txt, Longident.last f2.txt, Ast_convenience.get_str arg with
| "pcdata", "return", Some s -> Some s
| "txt", "return", Some s -> Some s
| _ -> None
end
| _ -> None

(** Test if the expression is a pcdata containing only whitespaces. *)
(** Test if the expression is a txt containing only whitespaces. *)
let is_whitespace = function
| Common.Val e -> begin
match to_pcdata e with
match to_txt e with
| Some s when String.trim s = "" -> true
| _ -> false
end
| _ -> false

(* Given a list of parse trees representing children of an element, filters out
all children that consist of applications of [pcdata] to strings containing
all children that consist of applications of [txt] to strings containing
only whitespace. *)
let filter_whitespace = List.filter (fun e -> not @@ is_whitespace e)

Expand Down
4 changes: 2 additions & 2 deletions ppx/element_content.mli
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type assembler =
run-time implementation of the element function that will be applied to the
children. It is either [Html] or [Svg], and is based on the element's
namespace. It is used for wrapping child elements, and for scoping child
[pcdata] elements.
[txt] elements.
The [name] argument is used for error reporting. *)

Expand Down Expand Up @@ -83,7 +83,7 @@ val select : assembler

(** {1 Misc utilities} *)

(** Remove pcdata containing only whitespace that are at the beginning or the end
(** Remove txt node containing only whitespace that are at the beginning or the end
of the list. *)
val filter_surrounding_whitespace :
Parsetree.expression Common.value list ->
Expand Down
16 changes: 8 additions & 8 deletions ppx/tyxml_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -199,28 +199,28 @@ end

(** Building block to rebuild the output with expressions intertwined. *)

let make_pcdata ~loc ~lang s =
let pcdata = Common.make ~loc lang "pcdata" in
let make_txt ~loc ~lang s =
let txt = Common.make ~loc lang "txt" in
let arg = Common.wrap lang loc @@ Common.string loc s in
Ast_helper.Exp.apply ~loc pcdata [Common.Label.nolabel, arg]
Ast_helper.Exp.apply ~loc txt [Common.Label.nolabel, arg]

(** Walk the text list to replace placeholders by OCaml expressions when
appropriate. Use {!make_pcdata} on the rest. *)
appropriate. Use {!make_txt} on the rest. *)
let make_text ~loc ~lang ss =
let buf = Buffer.create 17 in
let push_pcdata buf l =
let push_txt buf l =
let s = Buffer.contents buf in
Buffer.clear buf ;
if s = "" then l else Common.value (make_pcdata ~loc ~lang s) :: l
if s = "" then l else Common.value (make_txt ~loc ~lang s) :: l
in
let rec aux ~loc res = function
| [] -> push_pcdata buf res
| [] -> push_txt buf res
| `Text s :: t ->
Buffer.add_string buf s ;
aux ~loc res t
| `Delim g :: t ->
let e = Antiquot.get loc @@ Re.get g 0 in
aux ~loc (Common.antiquot e :: push_pcdata buf res) t
aux ~loc (Common.antiquot e :: push_txt buf res) t
in
aux ~loc [] @@ Re.split_full Antiquot.re_id @@ String.concat "" ss

Expand Down
Loading

0 comments on commit 7df50cc

Please sign in to comment.