Skip to content

Commit 7cf12b7

Browse files
committed
Improved docs.
1 parent 6a9bc90 commit 7cf12b7

File tree

5 files changed

+42
-28
lines changed

5 files changed

+42
-28
lines changed

README.md

+9-9
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ output = [
2222
[![Travis CI](https://travis-ci.org/diffplug/durian.svg?branch=master)](https://travis-ci.org/diffplug/durian)
2323
<!---freshmark /shields -->
2424

25-
Generating URL's for the buttons above is tricky. Once they're generated, it's hard to keep them up-to-date as new versions are released. FreshMark solves the "Markdown with variables" problem by embedding tiny JavaScript programs into the comments of your Markdown, which statically generate the rest of the document. By running these programs as part of your build script, your project's documentation will always stay up-to-date.
25+
Generating URL's for the buttons above is tricky. Once they're generated, it's hard to keep them up-to-date as new versions are released. FreshMark solves the "Markdown with variables" problem by embedding tiny JavaScript SCRIPTs into the comments of your Markdown, which statically generate the rest of the document. By running these SCRIPTs as part of your build script, your project's documentation will always stay up-to-date.
2626

2727
Here is what the code looks like for the shields at the top of this document:
2828

@@ -51,20 +51,20 @@ To run FreshMark on some text, call [FreshMark.compile()](https://diffplug.githu
5151
5252
## How it works
5353
54-
FreshMark has three pieces, `SECTION`, `PROGRAM`, and `INPUT`. They are parsed as shown below:
54+
FreshMark has three pieces, `SECTION`, `SCRIPT`, and `BODY`. They are parsed as shown below:
5555
5656
```javascript
5757
<!---freshmark SECTION (identifier)
58-
PROGRAM (javascript)
58+
SCRIPT (javascript)
5959
-->
60-
INPUT (markdown)
60+
BODY (markdown)
6161
<!---freshmark /SECTION -->
6262
```
6363
64-
When `PROGRAM` is run, there are two magic variables:
64+
When `SCRIPT` is run, there are two magic variables:
6565
66-
* `input` - This is everything inside of INPUT (guaranteed to have only unix newlines at runtime)
67-
* `output` - The script must assign to this variable. FreshMark will generate a new string where the `INPUT` section has been replaced with this value.
66+
* `input` - This is everything inside of BODY (guaranteed to have only unix newlines at runtime)
67+
* `output` - The script must assign to this variable. FreshMark will generate a new string where the `BODY` section has been replaced with this value.
6868
6969
Only four functions are provided:
7070
@@ -79,8 +79,8 @@ It's full ECMAScript 5.1, so you can define any other functions you like, but th
7979
8080
When you run FreshMark, you can supply it with a map of key-value pairs using the command line or via a properties file. If you're running FreshMark from a build system plugin such as Gradle, then all of your project's metadata will automatically be supplied. These key-value pairs are used in the following way:
8181
82-
* Before `PROGRAM` is executed, any `{{key}}` templates will be substituted with their corresponding value.
83-
* When `PROGRAM` is executed, all of these key-value pairs are available as variables.
82+
* Before `SCRIPT` is executed, any `{{key}}` templates will be substituted with their corresponding value.
83+
* When `SCRIPT` is executed, all of these key-value pairs are available as variables.
8484
8585
## How to run it
8686

src/main/java/com/diffplug/freshmark/CommentScript.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
* you must provide:
4848
* <ul>
4949
* <li>A {@link Parser} to split the comment text from the body text.</li>
50-
* <li>{@link #keyToValue} - defines how template keys in the script string are transformed into values</li>
51-
* <li>{@link #setupScriptEngine} - initializes any functions or variables which should be available to the script</li>
50+
* <li>{@link #keyToValue} - defines how template keys in the script string are transformed into values.</li>
51+
* <li>{@link #setupScriptEngine} - initializes any functions or variables which should be available to the script.</li>
5252
* </ul>
5353
* @see FreshMark
5454
*/

src/main/java/com/diffplug/freshmark/FreshMark.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@
3030
import com.diffplug.jscriptbox.JScriptBox;
3131
import com.diffplug.jscriptbox.Language;
3232

33-
/** The default implementation. */
33+
/**
34+
* FreshMark is designed to generate and modify
35+
* markdown using javascript.
36+
*/
3437
public class FreshMark extends CommentScript {
3538
private static final String INTRON = "<!---freshmark";
3639
private static final String EXON = "-->";
@@ -70,17 +73,17 @@ protected String keyToValue(String section, String key) {
7073
// built-in functions //
7174
////////////////////////
7275
/** Generates a markdown link. */
73-
static String link(String text, String url) {
76+
public static String link(String text, String url) {
7477
return "[" + text + "](" + url + ")";
7578
}
7679

7780
/** Generates a markdown image. */
78-
static String image(String altText, String url) {
81+
public static String image(String altText, String url) {
7982
return "!" + link(altText, url);
8083
}
8184

8285
/** Generates shields using <a href="http://shields.io/">shields.io</a>. */
83-
static String shield(String altText, String subject, String status, String color) {
86+
public static String shield(String altText, String subject, String status, String color) {
8487
return image(altText, "https://img.shields.io/badge/" + shieldEscape(subject) + "-" + shieldEscape(status) + "-" + shieldEscape(color) + ".svg");
8588
}
8689

@@ -91,7 +94,7 @@ private static String shieldEscape(String raw) {
9194
}
9295

9396
/** Replaces after prefix and before delimiter with replacement. */
94-
static String prefixDelimiterReplace(String input, String prefix, String delimiter, String replacement) {
97+
public static String prefixDelimiterReplace(String input, String prefix, String delimiter, String replacement) {
9598
StringBuilder builder = new StringBuilder(input.length() * 3 / 2);
9699
int lastElement = 0;
97100
Pattern pattern = Pattern.compile("(.*?" + Pattern.quote(prefix) + ")(.*?)(" + Pattern.quote(delimiter) + ")", Pattern.DOTALL);

src/main/java/com/diffplug/freshmark/Parser.java

+11-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,14 @@
1717

1818
import java.util.function.Consumer;
1919

20-
/** A format defined by "tag start" and "tag end" chunks of text. */
20+
/**
21+
* Parser splits text into tags and body sections, passes
22+
* pairs of matching sections to a {@link SectionCompiler}, then
23+
* combines that output with the original document to generate
24+
* the final output.
25+
*
26+
* @see {@link ParserIntronExtron}
27+
*/
2128
public abstract class Parser {
2229
/** Interface which can compile a single section of a FreshMark document. */
2330
@FunctionalInterface
@@ -36,13 +43,13 @@ protected interface ChunkHandler {
3643
}
3744

3845
/**
39-
* Given an input string, parses out the body sections from the tag sections.
46+
* Given an input string, parses out the body sections from the tags section.
4047
*
41-
* @param rawInput the raw input string
48+
* @param fullInput the raw input string
4249
* @param body called for every chunk of text outside a tag
4350
* @param tag called for every chunk of text inside a tag
4451
*/
45-
protected abstract void bodyAndTags(String rawInput, ChunkHandler body, ChunkHandler tag);
52+
protected abstract void bodyAndTags(String fullInput, ChunkHandler body, ChunkHandler tag);
4653

4754
/**
4855
* Reassembles a section/script/output chunk back into

src/main/java/com/diffplug/freshmark/ParserIntronExon.java

+12-8
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,11 @@
1818
import java.util.regex.Matcher;
1919
import java.util.regex.Pattern;
2020

21-
/** A format defined by "tag start" and "tag end" chunks of text. */
21+
/**
22+
* A parser defined by "intron" and "extron" chunks of text.
23+
*
24+
* @see {@link FreshMark}
25+
*/
2226
public class ParserIntronExon extends Parser {
2327
final String intron, exon;
2428
final Pattern pattern;
@@ -76,23 +80,23 @@ protected void bodyAndTags(String rawInput, ChunkHandler body, ChunkHandler tag)
7680
*
7781
* @param section
7882
* @param script
79-
* @param output
83+
* @param body
8084
* @return
8185
*/
8286
@Override
83-
protected String reassemble(String section, String script, String output) {
87+
protected String reassemble(String section, String script, String body) {
8488
// make sure that the compiled output starts and ends with a newline,
8589
// so that the tags stay separated separated nicely
86-
if (!output.startsWith("\n")) {
87-
output = "\n" + output;
90+
if (!body.startsWith("\n")) {
91+
body = "\n" + body;
8892
}
89-
if (!output.endsWith("\n")) {
90-
output = output + "\n";
93+
if (!body.endsWith("\n")) {
94+
body = body + "\n";
9195
}
9296
return intron + " " + section + "\n" +
9397
script +
9498
exon +
95-
output +
99+
body +
96100
intron + " /" + section + " " + exon;
97101
}
98102
}

0 commit comments

Comments
 (0)