Skip to content

Commit aa8fb7c

Browse files
committed
format java code in website project
1 parent c4f2429 commit aa8fb7c

File tree

7 files changed

+332
-260
lines changed

7 files changed

+332
-260
lines changed

website/src/main/java/app/views/MainView.java

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,32 @@
11
package app.views;
22

33
import j2html.tags.DomContent;
4-
5-
import static j2html.TagCreator.*;
4+
import static j2html.TagCreator.a;
5+
import static j2html.TagCreator.attrs;
6+
import static j2html.TagCreator.body;
7+
import static j2html.TagCreator.br;
8+
import static j2html.TagCreator.div;
9+
import static j2html.TagCreator.document;
10+
import static j2html.TagCreator.fileAsString;
11+
import static j2html.TagCreator.footer;
12+
import static j2html.TagCreator.h1;
13+
import static j2html.TagCreator.head;
14+
import static j2html.TagCreator.header;
15+
import static j2html.TagCreator.html;
16+
import static j2html.TagCreator.img;
17+
import static j2html.TagCreator.join;
18+
import static j2html.TagCreator.li;
19+
import static j2html.TagCreator.link;
20+
import static j2html.TagCreator.main;
21+
import static j2html.TagCreator.meta;
22+
import static j2html.TagCreator.nav;
23+
import static j2html.TagCreator.p;
24+
import static j2html.TagCreator.scriptWithInlineFile;
25+
import static j2html.TagCreator.scriptWithInlineFile_min;
26+
import static j2html.TagCreator.span;
27+
import static j2html.TagCreator.styleWithInlineFile_min;
28+
import static j2html.TagCreator.title;
29+
import static j2html.TagCreator.ul;
630

731
public class MainView {
832

@@ -23,24 +47,24 @@ public static String render(String title, String heading, DomContent... tags) {
2347
fileAsString("/html/googleTagManager.html"),
2448
fileAsString("/html/githubBanner.html"),
2549
header(attrs(".top-header"),
26-
nav(attrs(".width-limit"),
27-
a().withId("logo").withHref("/").with(
28-
span(img().withSrc("/img/logo.svg").withAlt("j2html logo"))
29-
),
30-
ul(
31-
li(a("Home").withHref("/")),
32-
li(a("Download").withHref("/download.html")),
33-
li(a("Examples").withHref("/examples.html")),
34-
li(a("News").withHref("/news.html"))
35-
)
36-
)
50+
nav(attrs(".width-limit"),
51+
a().withId("logo").withHref("/").with(
52+
span(img().withSrc("/img/logo.svg").withAlt("j2html logo"))
53+
),
54+
ul(
55+
li(a("Home").withHref("/")),
56+
li(a("Download").withHref("/download.html")),
57+
li(a("Examples").withHref("/examples.html")),
58+
li(a("News").withHref("/news.html"))
59+
)
60+
)
3761

3862
),
3963
header(attrs(".banner"),
40-
h1(attrs(".width-limit"), heading)
64+
h1(attrs(".width-limit"), heading)
4165
),
4266
main(attrs(".width-limit"),
43-
tags //content from other template
67+
tags //content from other template
4468
),
4569
div(attrs("#javalin-suggestion"), join(
4670
span("✖").withClass("close"),

website/src/main/java/app/views/Partials.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
import j2html.tags.ContainerTag;
44
import j2html.tags.Text;
5-
6-
import static j2html.TagCreator.*;
5+
import static j2html.TagCreator.a;
6+
import static j2html.TagCreator.attrs;
7+
import static j2html.TagCreator.code;
8+
import static j2html.TagCreator.div;
9+
import static j2html.TagCreator.fileAsEscapedString;
10+
import static j2html.TagCreator.join;
11+
import static j2html.TagCreator.li;
12+
import static j2html.TagCreator.p;
13+
import static j2html.TagCreator.pre;
14+
import static j2html.TagCreator.ul;
715

816
public class Partials {
917

@@ -23,12 +31,12 @@ public static ContainerTag javaComparison(String filename) {
2331
),
2432
pre(attrs(".nowith-pre"),
2533
code(attrs(".language-java"),
26-
fileAsEscapedString("/codeExamples/" + filename + "_new.java")
34+
fileAsEscapedString("/codeExamples/" + filename + "_new.java")
2735
)
2836
),
2937
pre(attrs(".with-pre"),
3038
code(attrs(".language-java"),
31-
fileAsEscapedString("/codeExamples/" + filename + ".java")
39+
fileAsEscapedString("/codeExamples/" + filename + ".java")
3240
)
3341
)
3442
);

website/src/main/java/app/views/pages/DownloadView.java

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,30 @@
11
package app.views.pages;
22

33
import app.views.MainView;
4-
5-
import static app.views.Partials.*;
6-
import static j2html.TagCreator.*;
4+
import static app.views.Partials.codeSnippet;
5+
import static j2html.TagCreator.a;
6+
import static j2html.TagCreator.attrs;
7+
import static j2html.TagCreator.fileAsEscapedString;
8+
import static j2html.TagCreator.h2;
9+
import static j2html.TagCreator.join;
10+
import static j2html.TagCreator.p;
11+
import static j2html.TagCreator.section;
712

813
public class DownloadView {
914
public static String render() {
1015
return MainView.render(
1116
"Download j2html",
1217
"Maven and GitHub",
1318
section(attrs("#download"),
14-
h2("Maven dependency"),
15-
p("To experience the joy of generating HTML with a Java HTML builder, add the j2html dependency to your POM:"),
16-
codeSnippet("markup", fileAsEscapedString("/codeExamples/mavenDep.xml")),
17-
h2("Clone the repo on GitHub"),
18-
p(join(
19-
"Please clone and/or fork the repo on",
20-
a("GitHub").withHref("https://github.com/tipsy/j2html").withTarget("_blank"),
21-
", make changes, and create pull requests! We will go through pull requests every sunday, so don't be shy."
22-
))
19+
h2("Maven dependency"),
20+
p("To experience the joy of generating HTML with a Java HTML builder, add the j2html dependency to your POM:"),
21+
codeSnippet("markup", fileAsEscapedString("/codeExamples/mavenDep.xml")),
22+
h2("Clone the repo on GitHub"),
23+
p(join(
24+
"Please clone and/or fork the repo on",
25+
a("GitHub").withHref("https://github.com/tipsy/j2html").withTarget("_blank"),
26+
", make changes, and create pull requests! We will go through pull requests every sunday, so don't be shy."
27+
))
2328

2429
)
2530
);

website/src/main/java/app/views/pages/ExamplesView.java

Lines changed: 93 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,23 @@
11
package app.views.pages;
22

3+
import app.views.MainView;
34
import java.util.Arrays;
45
import java.util.List;
5-
6-
import app.views.MainView;
7-
8-
import static app.views.Partials.*;
9-
import static j2html.TagCreator.*;
6+
import static app.views.Partials.codeSnippet;
7+
import static app.views.Partials.javaComparison;
8+
import static j2html.TagCreator.a;
9+
import static j2html.TagCreator.attrs;
10+
import static j2html.TagCreator.each;
11+
import static j2html.TagCreator.em;
12+
import static j2html.TagCreator.fileAsEscapedString;
13+
import static j2html.TagCreator.h2;
14+
import static j2html.TagCreator.p;
15+
import static j2html.TagCreator.section;
16+
import static j2html.TagCreator.table;
17+
import static j2html.TagCreator.tbody;
18+
import static j2html.TagCreator.td;
19+
import static j2html.TagCreator.text;
20+
import static j2html.TagCreator.tr;
1021

1122
public class ExamplesView {
1223
private static List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 9, 10);
@@ -16,93 +27,93 @@ public static String render() {
1627
"Examples of how to use j2html",
1728
"Reclaim control of your HTML",
1829
section(attrs("#examples"),
19-
h2(attrs("#basic-example"), "Basic example"),
20-
p("Creating a basic HTML structure in j2html is pretty similar to plain HTML. This Java code:"),
21-
javaComparison("basic"),
22-
p("Becomes this HTML:"),
23-
codeSnippet("markup", fileAsEscapedString("/codeExamples/basic.html")),
24-
p(
25-
text(
26-
"It's literally impossible to forget to close a div, mistype an attribute name, or forget an attribute quote! " +
27-
"Remember to include the Java wrapping code though, j2html is not a template language, all files are .java. " +
28-
"To see how the wrapping code could look, check out the "
29-
),
30-
a("getting started example").withHref("/"),
31-
text(".")
30+
h2(attrs("#basic-example"), "Basic example"),
31+
p("Creating a basic HTML structure in j2html is pretty similar to plain HTML. This Java code:"),
32+
javaComparison("basic"),
33+
p("Becomes this HTML:"),
34+
codeSnippet("markup", fileAsEscapedString("/codeExamples/basic.html")),
35+
p(
36+
text(
37+
"It's literally impossible to forget to close a div, mistype an attribute name, or forget an attribute quote! " +
38+
"Remember to include the Java wrapping code though, j2html is not a template language, all files are .java. " +
39+
"To see how the wrapping code could look, check out the "
3240
),
41+
a("getting started example").withHref("/"),
42+
text(".")
43+
),
3344

34-
h2(attrs("#core-concepts"), "Core concepts"),
35-
codeSnippet("java", fileAsEscapedString("/codeExamples/coreConcepts.java")),
45+
h2(attrs("#core-concepts"), "Core concepts"),
46+
codeSnippet("java", fileAsEscapedString("/codeExamples/coreConcepts.java")),
3647

37-
h2(attrs("#loops"), "Loops, each() and filter()"),
38-
p("Using Java 8's lambda syntax, you can write loops (via streams) inside your HTML-builder:"),
39-
javaComparison("forLoopLambda"),
48+
h2(attrs("#loops"), "Loops, each() and filter()"),
49+
p("Using Java 8's lambda syntax, you can write loops (via streams) inside your HTML-builder:"),
50+
javaComparison("forLoopLambda"),
4051

41-
p("j2html also offers a custom each method, which is slightly more powerful:"),
42-
javaComparison("each"),
52+
p("j2html also offers a custom each method, which is slightly more powerful:"),
53+
javaComparison("each"),
4354

44-
p("If you need to filter your collection, j2html has a built in filter function too:"),
45-
javaComparison("filter"),
55+
p("If you need to filter your collection, j2html has a built in filter function too:"),
56+
javaComparison("filter"),
4657

47-
p(
48-
"Since this is pure Java, all the Employee methods (getName, getImgPath, getTitle) are available to you, " +
49-
"and you get autocomplete suggestions and compile time errors."
50-
),
51-
p("Given three random employees, all the above approaches would give the same HTML:"),
52-
codeSnippet("markup", fileAsEscapedString("/codeExamples/forLoop.html")),
58+
p(
59+
"Since this is pure Java, all the Employee methods (getName, getImgPath, getTitle) are available to you, " +
60+
"and you get autocomplete suggestions and compile time errors."
61+
),
62+
p("Given three random employees, all the above approaches would give the same HTML:"),
63+
codeSnippet("markup", fileAsEscapedString("/codeExamples/forLoop.html")),
5364

54-
h2(attrs("#2d-table-example"), "Two dimensional table example"),
55-
javaComparison("2dTable"),
56-
p("The code above is generating this table:"),
57-
table(attrs("#table-example"),
58-
tbody(
59-
each(numbers, i -> tr(
60-
each(numbers, j -> td(
61-
String.valueOf(i * j)
62-
))
63-
))
64-
)
65-
),
65+
h2(attrs("#2d-table-example"), "Two dimensional table example"),
66+
javaComparison("2dTable"),
67+
p("The code above is generating this table:"),
68+
table(attrs("#table-example"),
69+
tbody(
70+
each(numbers, i -> tr(
71+
each(numbers, j -> td(
72+
String.valueOf(i * j)
73+
))
74+
))
75+
)
76+
),
6677

67-
h2(attrs("#partials"), "Partials"),
68-
p("You can create partials for elements you use a lot:"),
69-
javaComparison("partial"),
70-
p("The equivalent HTML would be:"),
71-
codeSnippet("markup", fileAsEscapedString("/codeExamples/partial.html")),
72-
p("You can then use these partials, for example in a registration form:"),
73-
javaComparison("view"),
74-
p("Pretty clean, right? The rendered HTML is more verbose:"),
75-
codeSnippet("markup", fileAsEscapedString("/codeExamples/view.html")),
76-
p(
77-
text("Imagine if you wanted labels in addition. The Java snippet would look almost identical: You could create a partial called"),
78-
em(" passwordAndLabel() "),
79-
text(
80-
"and nothing but the method name would change. The resulting HTML however, would be twice or thrice as big, " +
81-
"depending on whether or not you wrapped the input and label in another tag."
82-
)
83-
),
78+
h2(attrs("#partials"), "Partials"),
79+
p("You can create partials for elements you use a lot:"),
80+
javaComparison("partial"),
81+
p("The equivalent HTML would be:"),
82+
codeSnippet("markup", fileAsEscapedString("/codeExamples/partial.html")),
83+
p("You can then use these partials, for example in a registration form:"),
84+
javaComparison("view"),
85+
p("Pretty clean, right? The rendered HTML is more verbose:"),
86+
codeSnippet("markup", fileAsEscapedString("/codeExamples/view.html")),
87+
p(
88+
text("Imagine if you wanted labels in addition. The Java snippet would look almost identical: You could create a partial called"),
89+
em(" passwordAndLabel() "),
90+
text(
91+
"and nothing but the method name would change. The resulting HTML however, would be twice or thrice as big, " +
92+
"depending on whether or not you wrapped the input and label in another tag."
93+
)
94+
),
8495

85-
h2(attrs("#dynamic-views"), "Dynamic views"),
86-
p(
87-
"Once you've set up partials, you can call them from wherever, which greatly reduces potential errors. " +
88-
"Let's say we want to include the form from the partials-example in our webpage. " +
89-
"We want a header above and a footer below. A lot of templating languages make you do this: "
90-
),
91-
codeSnippet("java", fileAsEscapedString("/codeExamples/otherTemplates.vm")),
92-
p(
93-
"This is a pain to work with. You have no idea what the header and footer expects, and you have no way to affect how they treat your content. " +
94-
"You can easily break the site by forgetting to close divs, or by forgetting to include either the header or the footer in one of your views. " +
95-
"In j2html you can specify the context in which a view is rendered, and supply the rendering method with type safe parameters! " +
96-
"If we want to insert our form in a header/footer frame, we simply create a MainView and make it take our view as an argument:"
97-
),
96+
h2(attrs("#dynamic-views"), "Dynamic views"),
97+
p(
98+
"Once you've set up partials, you can call them from wherever, which greatly reduces potential errors. " +
99+
"Let's say we want to include the form from the partials-example in our webpage. " +
100+
"We want a header above and a footer below. A lot of templating languages make you do this: "
101+
),
102+
codeSnippet("java", fileAsEscapedString("/codeExamples/otherTemplates.vm")),
103+
p(
104+
"This is a pain to work with. You have no idea what the header and footer expects, and you have no way to affect how they treat your content. " +
105+
"You can easily break the site by forgetting to close divs, or by forgetting to include either the header or the footer in one of your views. " +
106+
"In j2html you can specify the context in which a view is rendered, and supply the rendering method with type safe parameters! " +
107+
"If we want to insert our form in a header/footer frame, we simply create a MainView and make it take our view as an argument:"
108+
),
98109

99-
javaComparison("main"),
100-
p("Which will result in the rendered HTML:"),
101-
codeSnippet("markup", fileAsEscapedString("/codeExamples/main.html")),
102-
p(
103-
"We would now get a compilation error if we forgot to include a title, and there is 0 chance of forgetting either header or footer, mistyping paths" +
104-
", forgetting to close divs, or anything else."
105-
)
110+
javaComparison("main"),
111+
p("Which will result in the rendered HTML:"),
112+
codeSnippet("markup", fileAsEscapedString("/codeExamples/main.html")),
113+
p(
114+
"We would now get a compilation error if we forgot to include a title, and there is 0 chance of forgetting either header or footer, mistyping paths" +
115+
", forgetting to close divs, or anything else."
116+
)
106117

107118
)
108119
);

0 commit comments

Comments
 (0)