Working on a literate generator similar to Docco.

This commit is contained in:
Jonathan Bernard
2011-09-02 16:47:46 -05:00
parent 7717439274
commit c2c2f9da3d
9 changed files with 388 additions and 91 deletions

View File

@ -0,0 +1,23 @@
package com.jdblabs.jlp.experimental;
import org.parboiled.Action;
import org.parboiled.BaseParser;
import org.parboiled.Context;
import org.parboiled.Rule;
import org.parboiled.annotations.*;
@BuildParseTree
public class DeadSimple extends BaseParser {
public Rule S() {
return OneOrMore(FirstOf(Line(), EmptyLine()).skipNode()); }
public Rule Line() {
return Sequence(OneOrMore(NOT_EOL).skipNode(), EOL); }
public Rule EmptyLine() {
return EOL; }
public Rule EOL = Ch('\n');
public Rule NOT_EOL = Sequence(TestNot(EOL), ANY);
}

View File

@ -0,0 +1,37 @@
package com.jdblabs.jlp.experimental
import com.jdblabs.jlp.experimental.ast.*
import java.util.List
import java.util.Map
public abstract class JLPBaseGenerator {
protected Map docState
protected JLPBaseGenerator() {
docState = [orgs: [:], // stores `@org` references
codeTrees: [:], // stores code ASTs for
currentDocId: false ] } // store the current docid
protected Map<String, String> generate(Map<String, SourceFile> sources) {
Map result = [:]
sources.each { sourceId, sourceAST ->
// set up the current generator state for this source
docState.currentDocId = sourceId
docState.codeTrees[sourceId] = sourceAST.codeAST
// generate the doc for this source
result[sourceId] = emit(sourceAST) }
// return our results
return result }
protected abstract String emit(SourceFile sourceFile)
protected abstract String emit(Block block)
protected abstract String emit(DocBlock docBlock)
protected abstract String emit(CodeBlock codeBlock)
protected abstract String emit(DocText docText)
protected abstract String emit(Directive directive)
}

View File

@ -22,13 +22,67 @@ public class JLPPegParser extends BaseParser<Object> {
*/
public Rule SourceFile() {
return Sequence(
// At the start of processing a new SourceFile, we need to set up
// our internal state.
// Clear the line count.
clearLineCount(),
// Add the top-level SourceFile AST node.
push(new SourceFile()),
// A SourceFile is made up of one or more Blocks
OneOrMore(Sequence(
FirstOf(Block(), DocBlock(), CodeBlock()),
// All of these options result in one new Block pushed onto the
// stack.
FirstOf(
addBlock((ASTNode) pop())))); }
// Match a whole Block. This pushes a Block on the stack.
Block(),
// A standalone DocBlock. We will create an empty CodeBlock
// to pair with it, then push a new Block onto the stack
// made from the DocBlock and the empty CodeBlock
Sequence(
// 1. We need to remember the line number to create the
// Block
push(curLineNum),
// 2. Match the DocBlock.
DocBlock(),
// 3. Create the empty CodeBlock.
push(new CodeBlock(curLineNum)),
// 4. Create the Block and push it onto the stack.
push(new Block((CodeBlock) pop(), (DocBlock) pop(),
popAsInt()))),
// A standalone CodeBlock. Similar to the standalone
// DocBlock, we will create an empty DocBlock to pair with
// the CodeBlock to make a Block, which is pushed onto the
// stack:
//
// *Note: With the way the parser is currently written,
// this will only match a CodeBlock that occurs
// before any DocBlock.*
Sequence(
// 1. Remember the line number for the Block.
push(curLineNum),
// 2. Create the empty DocBlock.
push(new DocBlock(curLineNum)),
// 3. Match the CodeBlock
CodeBlock(),
// 4. Create the Block and push it onto the stack
push(new Block((CodeBlock) pop(), (DocBlock) pop(),
popAsInt())))),
// pop the Block created by one of the above options and add it
// to the SourceFile
addBlockToSourceFile((Block) pop())))); }
/**
* Parses the rule:
@ -71,7 +125,7 @@ public class JLPPegParser extends BaseParser<Object> {
/**
* Parses the rule:
* Directive = DocLineStart AT (LongDirective / ShortFirective)
* Directive = DocLineStart AT (LongDirective / ShortDirective)
*
* Pushes a Directive node on the stack.
*/
@ -82,7 +136,7 @@ public class JLPPegParser extends BaseParser<Object> {
/**
* Parses the rule:
* LongDirective =
* (AUTHOR_DIR / DOC_DIR / EXAMPLE_DIR) RemainingLine DocText?
* (API_DIR / EXAMPLE_DIR) RemainingLine DocText?
*
* Pushes a Directive node onto the stack.
*/
@ -90,7 +144,7 @@ public class JLPPegParser extends BaseParser<Object> {
return Sequence(
push(curLineNum),
FirstOf(AUTHOR_DIR, DOC_DIR, EXAMPLE_DIR), push(match()),
FirstOf(API_DIR, EXAMPLE_DIR), push(match()),
RemainingLine(), push(match()),
Optional(Sequence(
@ -102,14 +156,14 @@ public class JLPPegParser extends BaseParser<Object> {
/**
* Parses the rule:
* ShortDirective = (ORG_DIR / COPYRIGHT_DIR) RemainingLine
* ShortDirective = (AUTHOR_DIR / ORG_DIR / COPYRIGHT_DIR) RemainingLine
*
* Pushes a Directive node onto the stack.
*/
Rule ShortDirective() {
return Sequence(
push(curLineNum),
FirstOf(ORG_DIR, COPYRIGHT_DIR), push(match()),
FirstOf(AUTHOR_DIR, ORG_DIR, COPYRIGHT_DIR), push(match()),
RemainingLine(),
push(new Directive(match().trim(), popAsString(), popAsInt()))); }
@ -127,27 +181,28 @@ public class JLPPegParser extends BaseParser<Object> {
DocLineStart(), TestNot(AT), RemainingLine(),
addToDocText(match())))); }
@SuppressSubnodes
Rule DocLineStart() {
return Sequence(
ZeroOrMore(SPACE), DOC_LINE_START, Optional(SPACE)); }
@SuppressSubnodes
Rule NonEmptyLine() {
return Sequence(OneOrMore(NOT_EOL), FirstOf(EOL, EOI)); }
Rule RemainingLine() {
return FirstOf(
Sequence(ZeroOrMore(NOT_EOL), EOL, incLineCount()),
Sequence(OneOrMore(NOT_EOL), EOI, incLineCount())); }
Rule AT = Ch('@');
Rule EOL = OneOrMore(AnyOf("\r\n"));
Rule NOT_EOL = Sequence(TestNot(EOL), ANY);
Rule SPACE = AnyOf(" \t");
Rule DOC_LINE_START = String("%%");
Rule AT = Ch('@').label("AT");
Rule EOL = FirstOf(String("\r\n"), Ch('\n'), Ch('\r')).label("EOL");
Rule NOT_EOL = Sequence(TestNot(EOL), ANY).label("NOT_EOL");
Rule SPACE = AnyOf(" \t").label("SPACE");
Rule DOC_LINE_START = String("%%").label("DOC_LINE_START");
// directive terminals
Rule AUTHOR_DIR = IgnoreCase("author");
Rule COPYRIGHT_DIR = IgnoreCase("copyright");
Rule DOC_DIR = IgnoreCase("doc");
Rule API_DIR = IgnoreCase("api");
Rule EXAMPLE_DIR = IgnoreCase("example");
Rule ORG_DIR = IgnoreCase("org");
@ -159,7 +214,7 @@ public class JLPPegParser extends BaseParser<Object> {
boolean incLineCount() { curLineNum++; return true; }
boolean addBlock(ASTNode block) {
boolean addBlockToSourceFile(Block block) {
SourceFile sourceFile = (SourceFile) pop();
sourceFile.blocks.add(block);
return push(sourceFile); }

View File

@ -0,0 +1,171 @@
package com.jdblabs.jlp.experimental
import com.jdblabs.jlp.experimental.ast.*
import com.jdblabs.jlp.experimental.ast.Directive.DirectiveType
import org.pegdown.Extensions
import org.pegdown.PegDownProcessor
import java.util.List
public class LiterateMarkdownGenerator extends JLPBaseGenerator {
protected PegDownProcessor pegdown
protected LiterateMarkdownGenerator() {
super()
pegdown = new PegDownProcessor(
Extensions.TABLES & Extensions.DEFINITIONS) }
protected static Map<String, String> generateDocuments(
Map<String, SourceFile> sources) {
LiterateMarkdownGenerator inst = new LiterateMarkdownGenerator()
return inst.generate(sources) }
protected String emit(SourceFile sourceFile) {
StringBuilder sb = new StringBuilder()
// Create the HTML file head
sb.append(
"""<!DOCTYPE html>
<html>
<head>
<title>${docState.currentDocId}</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<!-- <link rel="syltesheet" media="all" href=""/> -->
</head>
<body>
<div id="container">
<table cellpadding="0" cellspacing="0">
<thead><tr>
<th class="doc"><h1>${docState.currentDocId}</h1></th>
<th class="code"/>
</tr></thead>
<tbody>""")
// Emit the document to Markdown
sourceFile.blocks.each { block -> sb.append(emit(block)) }
// Create the HTML file foot
sb.append(
""" </tbody>
</table>
</div>
</body>
</html>""")
return sb.toString() }
protected String emit(Block block) {
StringBuilder sb = new StringBuilder()
// Look for an `@org` directive in the `Block`
Directive orgDir = block.docBlock.directives.find {
it.type == DirectiveType.Org }
// Create the `tr` that will hold the `Block`
if (orgDir) { sb.append("\n<tr id='${orgDir.value}'>") }
else { sb.append("<tr>") }
// Create the `td` for the documentation.
sb.append('\n<td class="doc">')
sb.append(emit(block.docBlock))
sb.append('</td>')
// Create the `td` for the `CodeBlock`
sb.append('\n<td class="code">')
sb.append(emit(block.codeBlock))
sb.append('</td>')
// Close the table row.
sb.append('</tr>') }
protected String emit(DocBlock docBlock) {
// Create a queue for the doc block elements, we are going to
// sort them by type and line number
List emitQueue
// Later we will need a string builder to hold our result.
StringBuilder sb
// Add all the directives
emitQueue = docBlock.directives.collect { directive ->
def queueItem = [lineNumber: directive.lineNumber, value: directive]
switch(directive.type) {
case DirectiveType.Api: queueItem.priority = 12; break
case DirectiveType.Author: queueItem.priority = 10; break
case DirectiveType.Copyright: queueItem.priority = 11; break
case DirectiveType.Example: queueItem.priority = 50; break
case DirectiveType.Org:
docState.orgs[directive.value] = directive
queueItem.priority = 0
break }
return queueItem }
// Add all the doc text blocks
emitQueue.addAll(docBlock.docTexts.collect { docText ->
[lineNumber: docText.lineNumber, priority: 50, value: docText] })
println emitQueue
println "----------"
// Sort the emit queue by priority, then line number.
emitQueue.sort(
{i1, i2 -> i1.priority != i2.priority ?
i1.priority - i2.priority :
i1.line - i2.line} as Comparator)
// Finally, we want to treat the whole block as one markdown chunk, so
// we will concatenate the values in the emit queue and then process
// the whole block once
sb = new StringBuilder()
emitQueue.each { queueItem -> sb.append(emit(queueItem.value)) }
return pegdown.markdownToHtml(sb.toString())
}
protected String emit(CodeBlock codeBlock) {
def codeLines
// Collect the lines into an array.
codeLines = codeBlock.lines.collect { lineNumber, line ->
[lineNumber, line] }
// Sort by line number.
codeLines.sort({ i1, i2 -> i1[0] - i2[0] } as Comparator)
codeLines = codeLines.collect { arr -> arr[1] }
// write out the lines in a <pre><code> block
return "<pre><code>${codeLines.join('')}</code></pre>" }
protected String emit(DocText docText) { return docText.value }
protected String emit(Directive directive) {
switch(directive.type) {
// An `@api` directive is immediately processed and wrapped in a
// div (we need to process this now because Markdown does not
// process input inside HTML elements).
case DirectiveType.Api:
return "<div class='api'>" +
pegdown.markdownToHtml(directive.value) + "</div>\n"
// An `@author` directive is turned into a definition list.
case DirectiveType.Author:
return "Author\n: ${directive.value}\n"
case DirectiveType.Copyright:
return "&copy; ${directive.value}\n"
// An `@example` directive is returned as is
case DirectiveType.Example: return directive.value
// An `@org` directive is ignored.
case DirectiveType.Org: return "" }
}
}

View File

@ -3,9 +3,9 @@ package com.jdblabs.jlp.experimental.ast
public class Directive extends ASTNode {
public static enum DirectiveType {
Api,
Author,
Copyright,
Doc,
Example,
Org;

View File

@ -4,7 +4,9 @@ public class DocText extends ASTNode {
public String value
public DocText(int lineNumber) { super(lineNumber) }
public DocText(int lineNumber) {
super(lineNumber)
value = "" }
public String toString() { value }
}

View File

@ -3,4 +3,6 @@ package com.jdblabs.jlp.experimental.ast
public class SourceFile {
public List<ASTNode> blocks = []
public def codeAST
public String id
}