Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Process images with LinkRenderer (fixes #95) #115

Merged
merged 2 commits into from Dec 9, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/org/pegdown/LinkRenderer.java
Expand Up @@ -60,6 +60,11 @@ public Rendering render(ExpLinkNode node, String text) {
return StringUtils.isEmpty(node.title) ? rendering : rendering.withAttribute("title", encode(node.title));
}

public Rendering render(ExpImageNode node, String text) {
Rendering rendering = new Rendering(node.url, text);
return StringUtils.isEmpty(node.title) ? rendering : rendering.withAttribute("title", encode(node.title));
}

public Rendering render(MailLinkNode node) {
String obfuscated = obfuscate(node.getText());
return new Rendering("mailto:" + obfuscated, obfuscated);
Expand All @@ -70,6 +75,11 @@ public Rendering render(RefLinkNode node, String url, String title, String text)
return StringUtils.isEmpty(title) ? rendering : rendering.withAttribute("title", encode(title));
}

public Rendering render(RefImageNode node, String url, String title, String alt) {
Rendering rendering = new Rendering(url, alt);
return StringUtils.isEmpty(title) ? rendering : rendering.withAttribute("title", encode(title));
}

public Rendering render(WikiLinkNode node) {
try {
String url = "./" + URLEncoder.encode(node.getText().replace(' ', '-'), "UTF-8") + ".html";
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/pegdown/ToHtmlSerializer.java
Expand Up @@ -123,7 +123,8 @@ public void visit(DefinitionTermNode node) {
}

public void visit(ExpImageNode node) {
printImageTag(node, node.url);
String text = printChildrenToString(node);
printImageTag(linkRenderer.render(node, text));
}

public void visit(ExpLinkNode node) {
Expand Down Expand Up @@ -197,7 +198,7 @@ public void visit(RefImageNode node) {
if (node.referenceKey != null) printer.print(key);
printer.print(']');
}
} else printImageTag(node, refNode.getUrl());
} else printImageTag(linkRenderer.render(node, refNode.getUrl(), refNode.getTitle(), text));
}

public void visit(RefLinkNode node) {
Expand Down Expand Up @@ -384,9 +385,14 @@ protected void printIndentedTag(SuperNode node, String tag) {
printer.indent(-2).println().print('<').print('/').print(tag).print('>');
}

protected void printImageTag(SuperNode imageNode, String url) {
printer.print("<img src=\"").print(url).print("\" alt=\"")
.printEncoded(printChildrenToString(imageNode)).print("\"/>");
protected void printImageTag(LinkRenderer.Rendering rendering) {
printer.print("<img");
printAttribute("src", rendering.href);
printAttribute("alt", rendering.text);
for (LinkRenderer.Attribute attr : rendering.attributes) {
printAttribute(attr.name, attr.value);
}
printer.print("\"/>");
}

protected void printLink(LinkRenderer.Rendering rendering) {
Expand Down
8 changes: 4 additions & 4 deletions src/test/resources/Maruku/images.html
@@ -1,9 +1,9 @@
<p>This page does not uilizes <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" /></p>

<p>Please mouseover to see the title: <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" /></p>
<p>Please mouseover to see the title: <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" title="Title ok!" /></p>

<p>Please mouseover to see the title: <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" /></p>
<p>Please mouseover to see the title: <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" title="Title ok!" /></p>

<p>I&#8217;ll say it one more time: this page does not use <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" /></p>
<p>I&#8217;ll say it one more time: this page does not use <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" title="Optional title attribute" /></p>

<p>This is double size: <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" /></p>
<p>This is double size: <img src="http://jigsaw.w3.org/css-validator/images/vcss" alt="Cascading Style Sheets" title="Optional title attribute" /></p>