Package org.opencypher.tools.io
Class HtmlTag
- java.lang.Object
-
- org.opencypher.tools.io.HtmlTag
-
- All Implemented Interfaces:
AutoCloseable
public final class HtmlTag extends Object implements AutoCloseable
Tiny DSL for generating HTML. Usage:try (HtmlTag.Htmlhtml =HtmlTag.html(outputPath) ) { html.head(HtmlTag.head("title", "My Page") ); try (HtmlTagbody = html.body() ) { body.tag("h1") // opens a new <h1> tag .text("Welcome to My Page") // adds text content to the tag .close(); // closes the </h1> tag body.text("This is a very neat page."); body.p("It contains some text!"); body.text("You should come back when there is more content."); body.br(); body.text("Until then, here is a picture of a cat for you to look at:"); // img tags should not be closed, so we simply don't invoke theclose()method. body.tag("img",HtmlTag.attr("src", "http://thecatapi.com/api/images/get?format=src&type=gif") ); body.textTag("b", "To do:"); try (HtmlTaglist = body.tag("ul") ) { list.textTag("li", "Find cuter cat") .textTag("li", "???") .textTag("li", "Profit!"); } } }
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static interfaceHtmlTag.Attributestatic classHtmlTag.HeadTagstatic classHtmlTag.Html
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description HtmlTaga(String href, String text)static HtmlTag.Attributeattr(String attribute, String value)Generate html tag attributes for use in .voidbr()voidclose()static HtmlTag.HeadTaghead(String tag, String text, HtmlTag.Attribute... attributes)Generate html tags for use in<head>.static HtmlTag.Htmlhtml(Path file)static HtmlTag.Htmlhtml(Output output)static HtmlTag.HeadTagmeta(String name, String value)Generate meta tags for use in<head>.voidp(String text)HtmlTagtag(String tag, HtmlTag.Attribute... attributes)HtmlTagtext(String text)OutputtextOutput()Unsafe access to the underlyingOutput.HtmlTagtextTag(String tag, String text, HtmlTag.Attribute... attributes)
-
-
-
Method Detail
-
html
public static HtmlTag.Html html(Path file)
-
html
public static HtmlTag.Html html(Output output)
-
tag
public final HtmlTag tag(String tag, HtmlTag.Attribute... attributes)
-
textTag
public HtmlTag textTag(String tag, String text, HtmlTag.Attribute... attributes)
-
p
public void p(String text)
-
br
public void br()
-
attr
public static HtmlTag.Attribute attr(String attribute, String value)
Generate html tag attributes for use in . This is an alternative tobody.tag( "img", src -> imgUri ), allowing the use of the API on earlier builds of the JDK asbody.tag( "img", attr( "src", imgUri ) ).- Parameters:
attribute- the name of the attribute.value- the value of the attribute.- Returns:
- an object that generates the attribute.
-
meta
public static HtmlTag.HeadTag meta(String name, String value)
Generate meta tags for use in<head>.- Parameters:
name- the name of the meta attribute.value- the value of the meta attribute.- Returns:
- an object that generates the tag in the head.
-
head
public static HtmlTag.HeadTag head(String tag, String text, HtmlTag.Attribute... attributes)
Generate html tags for use in<head>. Allows adding attributes to a head tag.- Parameters:
tag- the name of the head tag.text- the contents of the head tag.attributes- the attributes of the head tag.- Returns:
- an object that generates the tag in the head.
-
close
public void close()
- Specified by:
closein interfaceAutoCloseable
-
-