Fix line wrapping issues with Markdown examples
Project: http://git-wip-us.apache.org/repos/asf/tomee-site-generator/repo Commit: http://git-wip-us.apache.org/repos/asf/tomee-site-generator/commit/1cc43e0e Tree: http://git-wip-us.apache.org/repos/asf/tomee-site-generator/tree/1cc43e0e Diff: http://git-wip-us.apache.org/repos/asf/tomee-site-generator/diff/1cc43e0e Branch: refs/heads/master Commit: 1cc43e0e17c3d156d5d92b54a887e46930fcdfb4 Parents: 3c8d303 Author: dblevins <[email protected]> Authored: Sun Nov 25 22:31:57 2018 -0800 Committer: dblevins <[email protected]> Committed: Sun Nov 25 22:31:57 2018 -0800 ---------------------------------------------------------------------- .../org/apache/tomee/website/Examples2.java | 2 + .../org/apache/tomee/website/FixMarkdown.java | 100 +++++++++++++ .../java/org/apache/tomee/website/Sources.java | 17 +-- .../apache/tomee/website/FixMarkdownTest.java | 142 +++++++++++++++++++ 4 files changed, 250 insertions(+), 11 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/1cc43e0e/src/main/java/org/apache/tomee/website/Examples2.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tomee/website/Examples2.java b/src/main/java/org/apache/tomee/website/Examples2.java index 1891877..79aaa03 100644 --- a/src/main/java/org/apache/tomee/website/Examples2.java +++ b/src/main/java/org/apache/tomee/website/Examples2.java @@ -24,6 +24,7 @@ import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; +import java.util.ListIterator; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -50,6 +51,7 @@ public class Examples2 { .peek(example -> example.updateDestination(destDir)) .peek(this::copyToDest) .peek(this::addJbakeHeader) + .peek(FixMarkdown::process) .collect(Collectors.toList()); http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/1cc43e0e/src/main/java/org/apache/tomee/website/FixMarkdown.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tomee/website/FixMarkdown.java b/src/main/java/org/apache/tomee/website/FixMarkdown.java new file mode 100644 index 0000000..2bb15de --- /dev/null +++ b/src/main/java/org/apache/tomee/website/FixMarkdown.java @@ -0,0 +1,100 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tomee.website; + +import org.apache.openejb.loader.IO; +import org.apache.openejb.util.Join; + +import java.io.File; +import java.io.IOException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.function.Consumer; +import java.util.regex.Pattern; + +public class FixMarkdown { + + private final List<String> completed = new ArrayList<String>(); + private Consumer<String> processor = this::findJbakeHeader; + + public static void process(final Example example) { + if (!example.getExt().startsWith("md")) return; + + final FixMarkdown fixMarkdown = new FixMarkdown(); + + try { + final List<String> lines = new ArrayList<>(); + Collections.addAll(lines, IO.slurp(example.getDestReadme()).split("\n")); + + for (final String line : lines) { + fixMarkdown.process(line); + } + + fixMarkdown.process(""); + + // Update the destination readme file + IO.copy(IO.read(Join.join("\n", fixMarkdown.completed)), example.getDestReadme()); + + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private void process(final String line) { + processor.accept(line); + } + + private void findJbakeHeader(final String line) { + completed.add(line); + + if (line.equals("~~~~~~")) { + processor = this::findWrappableLines; + } + } + + private void findWrappableLines(final String line) { + if (isWrappable(line)) { + processor = new Unwrap()::process; + processor.accept(line); + } else { + completed.add(line); + } + } + + public class Unwrap { + private final List<String> lines = new ArrayList<>(); + + public void process(final String line) { + if (!isWrappable(line)) { + completed.add(Join.join(" ", lines)); + + processor = FixMarkdown.this::findWrappableLines; + processor.accept(line); + return; + } + + lines.add(line); + } + } + + private final Pattern alpha = Pattern.compile("^[a-zA-Z].*"); + + private boolean isWrappable(final String line) { + return alpha.matcher(line).matches(); + } +} http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/1cc43e0e/src/main/java/org/apache/tomee/website/Sources.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/tomee/website/Sources.java b/src/main/java/org/apache/tomee/website/Sources.java index c08783e..c8592a4 100644 --- a/src/main/java/org/apache/tomee/website/Sources.java +++ b/src/main/java/org/apache/tomee/website/Sources.java @@ -52,6 +52,12 @@ public class Sources { repos.mkdirs(); Collections.addAll(this.sources, sources); + for (final Source source : sources) { + if (source.isLatest()) { + this.sources.add(new Source(source.getScmUrl(), source.getBranch(), "latest")); + break; + } + } } public File getDestination() { @@ -98,15 +104,4 @@ public class Sources { private static void done(final Source source) { System.out.println("Done " + source); } - - public static void main(String[] args) throws Exception { - final Sources sources = new Sources(new File("target/jbake"), new File("repos"), new File("src/main/jbake/content"), - new Source("https://git-wip-us.apache.org/repos/asf/tomee.git", "master", "master"), - new Source("https://git-wip-us.apache.org/repos/asf/tomee.git", "tomee-7.1.0", "tomee-7.1"), - new Source("https://git-wip-us.apache.org/repos/asf/tomee.git", "tomee-7.0.5", "tomee-7.0"), - new Source("https://git-wip-us.apache.org/repos/asf/tomee.git", "tomee-8.0.0-M1", "tomee-8.0", true) - ); - - sources.prepare(); - } } http://git-wip-us.apache.org/repos/asf/tomee-site-generator/blob/1cc43e0e/src/test/java/org/apache/tomee/website/FixMarkdownTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/tomee/website/FixMarkdownTest.java b/src/test/java/org/apache/tomee/website/FixMarkdownTest.java new file mode 100644 index 0000000..45341e9 --- /dev/null +++ b/src/test/java/org/apache/tomee/website/FixMarkdownTest.java @@ -0,0 +1,142 @@ +package org.apache.tomee.website; + +import org.apache.openejb.loader.IO; +import org.junit.Assert; +import org.junit.Test; + +import java.io.File; + +public class FixMarkdownTest extends Assert { + + @Test + public void twoSimpleParagraphs() throws Exception { + + final File srcReadMe = File.createTempFile("example-", ".md"); + final File destReadme = File.createTempFile("example-", ".md"); + + final Example example = new Example(srcReadMe, "cdi-basic", "md", "cdi-basic.html", "none"); + example.setDestReadme(destReadme); + + IO.copy(IO.read("~~~~~~\n" + + "To use `@Inject`, the first thing you need is a `META-INF/beans.xml` file in the module\n" + + "or jar. This effectively turns on CDI and allows the `@Inject` references to work.\n" + + "No `META-INF/beans.xml` no injection, period. This may seem overly strict,\n" + + "but it is not without reason. The CDI API is a bit greedy and does consume a fair\n" + + "about of resources by design.\n" + + "\n" + + "When the container constructs a bean with an `@Inject` reference,\n" + + "it will first find or create the object that will be injected. For the sake of\n" + + "simplicity, the is example has a basic `Faculty` pojo with a no-arg constructor. Anyone\n" + + "referencing `@Inject Faculty` will get their own instance of `Faculty`. If the desire\n" + + "is to share the same instance of `Faculty`, see the concept of `scopes` -- this is\n" + + "exactly what scopes are for.\n" + ), srcReadMe); + IO.copy(srcReadMe, destReadme); + + FixMarkdown.process(example); + + final String expected = "~~~~~~\n" + + "To use `@Inject`, the first thing you need is a `META-INF/beans.xml` file in the module " + + "or jar. This effectively turns on CDI and allows the `@Inject` references to work. " + + "No `META-INF/beans.xml` no injection, period. This may seem overly strict, " + + "but it is not without reason. The CDI API is a bit greedy and does consume a fair " + + "about of resources by design.\n" + + "\n" + + "When the container constructs a bean with an `@Inject` reference, " + + "it will first find or create the object that will be injected. For the sake of " + + "simplicity, the is example has a basic `Faculty` pojo with a no-arg constructor. Anyone " + + "referencing `@Inject Faculty` will get their own instance of `Faculty`. If the desire " + + "is to share the same instance of `Faculty`, see the concept of `scopes` -- this is " + + "exactly what scopes are for.\n"; + + final String actual = IO.slurp(destReadme); + + assertEquals(expected, actual); + } + + @Test + public void sectionWithCodeBlock() throws Exception { + + final File srcReadMe = File.createTempFile("example-", ".md"); + final File destReadme = File.createTempFile("example-", ".md"); + + final Example example = new Example(srcReadMe, "cdi-basic", "md", "cdi-basic.html", "none"); + example.setDestReadme(destReadme); + + IO.copy(IO.read("~~~~~~\n" + + "## Faculty <small>a basic injectable pojo</small>\n" + + "\n" + + " public class Faculty {\n" + + "\n" + + " private List<String> facultyMembers;\n" + + "\n" + + " private String facultyName;\n" + + "\n" + + " @PostConstruct\n" + + " public void initialize() {\n" + + " this.facultyMembers = new ArrayList<String>();\n" + + " facultyMembers.add(\"Ian Schultz\");\n" + + " facultyMembers.add(\"Diane Reyes\");\n" + + " facultyName = \"Computer Science\";\n" + + " }\n" + + "\n" + + " public List<String> getFacultyMembers() {\n" + + " return facultyMembers;\n" + + " }\n" + + "\n" + + " public String getFacultyName() {\n" + + " return facultyName;\n" + + " }\n" + + "\n" + + " }\n" + + "\n" + + "## Course <small>a simple session bean</small>\n"), srcReadMe); + IO.copy(srcReadMe, destReadme); + + FixMarkdown.process(example); + + // We expect the original content -- nothing should be changed + final String expected = IO.slurp(srcReadMe); + + final String actual = IO.slurp(destReadme); + + assertEquals(expected, actual); + } + + @Test + public void skipJBakeMarkdownHeaders() throws Exception { + + final File srcReadMe = File.createTempFile("example-", ".md"); + final File destReadme = File.createTempFile("example-", ".md"); + + final Example example = new Example(srcReadMe, "cdi-basic", "md", "cdi-basic.html", "none"); + example.setDestReadme(destReadme); + + IO.copy(IO.read("type=page\n" + + "status=awesome\n" + + "title=Awesome\n" + + "~~~~~~\n" + + "Here we have\n" + + "a sentence that needs unwrapping\n" + + "\n" + + "But the header should not be\n" + + "unwrapped. That would be bad.\n" + ), srcReadMe); + IO.copy(srcReadMe, destReadme); + + FixMarkdown.process(example); + + final String expected = "type=page\n" + + "status=awesome\n" + + "title=Awesome\n" + + "~~~~~~\n" + + "Here we have a sentence that needs unwrapping\n" + + "\n" + + "But the header should not be unwrapped. That would be bad.\n"; + + final String actual = IO.slurp(destReadme); + + assertEquals(expected, actual); + } + +} \ No newline at end of file
