Skip to content

Commit

Permalink
Merge branch 'master' of github.com:sirthias/pegdown into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
sirthias committed Oct 4, 2011
2 parents 73d6f61 + 69b1e31 commit ec23ce5
Show file tree
Hide file tree
Showing 9 changed files with 112 additions and 6 deletions.
7 changes: 4 additions & 3 deletions README.markdown
Expand Up @@ -8,8 +8,8 @@ _pegdown_ is nearly 100% compatible with the original Markdown specification and
On top of the standard Markdown feature set _pegdown_ implements a number of extensions similar to what other popular Markdown processors offer.
Currently _pegdown_ supports the following extensions over standard Markdown:

* SMARTS: Beautifys apostrophes, ellipsises ("..." and ". . .") and dashes ("--" and "---")
* QUOTES: Beautifys single quotes, double quotes and double angle quotes (« and »)
* SMARTS: Beautifies apostrophes, ellipses ("..." and ". . .") and dashes ("--" and "---")
* QUOTES: Beautifies single quotes, double quotes and double angle quotes (« and »)
* SMARTYPANTS: Convenience extension enabling both, SMARTS and QUOTES, at once.
* ABBREVIATIONS: Abbreviations in the way of [PHP Markdown Extra][].
* HARDWRAPS: Alternative handling of newlines, see [Github-flavoured-Markdown][]
Expand All @@ -20,6 +20,7 @@ Currently _pegdown_ supports the following extensions over standard Markdown:
* HTML BLOCK SUPPRESSION: Suppresses the output of HTML blocks.
* INLINE HTML SUPPRESSION: Suppresses the output of inline HTML elements.
* NO_FOLLOW_LINKS: Attaches a `rel="nofollow"` attribute to all generated HTML links.
* WIKILINKS: Support Github-style wiki links to relative HTML files.

Note: _pegdown_ differs from the original Markdown in that it ignores in-word emphasis as in

Expand Down Expand Up @@ -95,7 +96,7 @@ tool [peg-markdown](http://github.com/jgm/peg-markdown).
License
-------

_pegdown_ is licensed under [APL 2.0](http://www.apache.org/licenses/LICENSE-2.0).
_pegdown_ is licensed under [Apache License 2.0](http://www.apache.org/licenses/LICENSE-2.0).


Patch Policy
Expand Down
13 changes: 10 additions & 3 deletions src/main/java/org/pegdown/Extensions.java
Expand Up @@ -26,7 +26,7 @@ public interface Extensions {
static final int NONE = 0x00;

/**
* Pretty ellipsises, dashes and apostrophes.
* Pretty ellipses, dashes and apostrophes.
*/
static final int SMARTS = 0x01;

Expand Down Expand Up @@ -57,8 +57,8 @@ public interface Extensions {
static final int HARDWRAPS = 0x08;

/**
* Enables plain autolinks the way github flavoures markdown implements them.
* With this extension enabled pegdown will intelligently recognize URLs and email adresses
* Enables plain autolinks the way github flavoured markdown implements them.
* With this extension enabled pegdown will intelligently recognize URLs and email addresses
* without any further delimiters and mark them as the respective link type.
*
* @see <a href="http://github.github.com/github-flavored-markdown">Github-flavored-Markdown</a>
Expand Down Expand Up @@ -88,6 +88,13 @@ public interface Extensions {
*/
static final int FENCED_CODE_BLOCKS = 0x80;

/**
* Converts GitHub wiki-links to regular links.
*
* @see <a href="http://github.github.com/github-flavored-markdown">Github-flavored-Markdown</a>
*/
static final int WIKILINKS = 0x100;

/**
* All available extensions excluding the HTML SUPPRESSION and NO_FOLLOW_LINKS options.
*/
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/pegdown/Parser.java
Expand Up @@ -684,6 +684,7 @@ public Rule Image() {
public Rule Link() {
return NodeSequence(
FirstOf(
ext(WIKILINKS) ? WikiLink() : NOTHING,
Sequence(Label(), FirstOf(ExplicitLink(), ReferenceLink())),
AutoLink()
)
Expand Down Expand Up @@ -762,6 +763,15 @@ public Rule AutoLink() {
);
}

public Rule WikiLink() {
return Sequence(
"[[",
OneOrMore(TestNot(']'), ANY), // Not sure ANY is appropriate here, but certainly some symbols and whitespace are
push(new WikiLinkNode(match(), ext(NO_FOLLOW_LINKS))),
"]]"
);
}

public Rule AutoLinkUrl() {
return Sequence(
Sequence(OneOrMore(Letter()), "://", AutoLinkEnd()),
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/pegdown/ToHtmlSerializer.java
Expand Up @@ -313,6 +313,14 @@ public void visit(VerbatimNode node) {
printer.print("</code></pre>");
}

public void visit(WikiLinkNode node) {
printer.print("<a href=\"")
.printEncoded(node.getUrl(), this)
.print(node.isNofollow() ? "\" rel=\"nofollow\">" : "\">")
.printEncoded(node.getText(), this)
.print("</a>");
}

public String transformVerbatimText(String text) {
// transform all initial newlines to HTML breaks
while(text.charAt(0) == '\n') {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/pegdown/ast/Visitor.java
Expand Up @@ -50,6 +50,7 @@ public interface Visitor {
void visit(TableNode node);
void visit(TableRowNode node);
void visit(VerbatimNode node);
void visit(WikiLinkNode node);

void visit(TextNode node);
void visit(SuperNode node);
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/org/pegdown/ast/WikiLinkNode.java
@@ -0,0 +1,55 @@
/*
* Copyright (C) 2010-2011 Mathias Doenitz
*
* Based on peg-markdown (C) 2008-2010 John MacFarlane
*
* Licensed 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.pegdown.ast;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

public class WikiLinkNode
extends TextNode {
private boolean nofollow;
private String url;

public WikiLinkNode(String page, boolean nofollow) {
super(page);
String s;
try {
s = URLEncoder.encode(page.replace(' ', '-'), "UTF-8");
}
catch (UnsupportedEncodingException e) {
s = page;
}
this.url = "./" + s + ".html";
this.nofollow = nofollow;
}

public boolean isNofollow() {
return nofollow;
}

public String getUrl()
{
return url;
}

@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
}
1 change: 1 addition & 0 deletions src/test/java/org/pegdown/CustomPegDownTest.java
Expand Up @@ -60,6 +60,7 @@ public void customPegDownTests() {
test("pegdown/Quoted Blockquote");
test("pegdown/Smartypants");
test("pegdown/Tables");
test("pegdown/Wikilinks");
}

@Test(dependsOnMethods = "customPegDownTests")
Expand Down
11 changes: 11 additions & 0 deletions src/test/resources/pegdown/Wikilinks.html
@@ -0,0 +1,11 @@
<h1>Wikilinks</h1>
<p>Wikilinks are simple URIs like <a href=
"./Autolinks.html">Autolinks</a>,<br/>
which will be converted by pegdown.</p>
<p>Another example with spaces: <a href="./Special-Chars.html">Special Chars</a>.</p>
<p>The following links should work just normally:</p>
<ul>
<li><a href="http://example">example</a></li>
<li><a href="http://example">ex@mple</a></li>
<li><a href="http://example">example://</a></li>
</ul>
12 changes: 12 additions & 0 deletions src/test/resources/pegdown/Wikilinks.md
@@ -0,0 +1,12 @@
# Wikilinks

Wikilinks are simple URIs like [[Autolinks]],
which will be converted by pegdown.

Another example with spaces: [[Special Chars]].

The following links should work just normally:

* [example](http://example)
* [ex@mple](http://example)
* [example://](http://example)

0 comments on commit ec23ce5

Please sign in to comment.