Skip to content

Commit

Permalink
Support RefImageLink
Browse files Browse the repository at this point in the history
  • Loading branch information
wattazoum committed Aug 5, 2016
1 parent 2d40f1d commit 6c9f471
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,35 +479,86 @@ public void visit(ListItemNode lin) {

@Override
public void visit(final ExpImageNode ein) {
//visitChildren(ein);
// We always have a URL, relative or not

try {
final ArrayList<String> alt = new ArrayList<String>();
boolean found = findByClass(ein, TextNode.class, new FindPredicate<TextNode>() {

final java.net.URI uri = new java.net.URI(ein.url);
@Override
public boolean f(TextNode node, Node parent, int index) {
alt.add(node.getText());
return true;
}
});

final String scheme = uri.getScheme();
if (!found) {
throw new IllegalStateException("The alt name should be mandatory in Markdown for images: " + ein.url);
}

if( !uri.isAbsolute()
&& (null==uri.getScheme() || scheme.isEmpty())
&& findByClass(ein, TextNode.class, new FindPredicate<TextNode>() {
String titlePart = isNotBlank(ein.title) ? format("|title=\"%s\"", ein.title) : "";
_buffer.append( format( "!%s|alt=\"%s\"%s!", ein.url, alt.get(0), titlePart));

@Override
public boolean f(TextNode node, Node parent, int index) {
_buffer.append( format( "!%s!", node.getText() ));
return true;
}
}))
{
return;
}

@Override
public void visit(final RefImageNode rin) {

final ArrayList<String> alt = new ArrayList<String>();
boolean found = findByClass(rin, TextNode.class, new FindPredicate<TextNode>() {

@Override
public boolean f(TextNode node, Node parent, int index) {
alt.add(node.getText());
return true;
}
});

if (!found) {
throw new IllegalStateException("The alt name should be mandatory in Markdown for images. ");
}

final SuperNode referenceKey = rin.referenceKey;
final String ref = getRefString(rin, referenceKey);
String url = ref;
String title = null;
ReferenceNode referenceNode = referenceNodes.get(ref);
if (referenceNode != null) {
if (isNotBlank(referenceNode.getUrl())) {
url = referenceNode.getUrl();
}
title = referenceNode.getTitle();
}

} catch (URISyntaxException ex) {
// @TODO notify error
}
String titlePart = isNotBlank(title) ? format("|title=\"%s\"", title) : "";
_buffer.append( format( "!%s|alt=\"%s\"%s!", url, alt.get(0), titlePart));
}

private String getRefString(final SuperNode refnode, final SuperNode referenceKey) {
final String ref;
if( referenceKey != null ) {

_buffer.append( format( "!%s!", ein.url));
ref = bufferVisit(new F<Void, Void>() {
@Override
public Void f(Void p) {
visitChildren(referenceKey);
return null;
}
}).toString();
} else {
// in case the refkey is not with the link, we use the references found in the root node
ref = bufferVisit(new F<Void, Void>() {
@Override
public Void f(Void p) {
visitChildren(refnode);
return null;
}
}).toString();
}
return ref;
}

private static boolean isNotBlank(String str) {
return str != null && str.length() > 0;
}

@Override
Expand Down Expand Up @@ -577,26 +628,8 @@ public void visit(final RefLinkNode rln) {
visitChildren(rln);
_buffer.append('|');

final String ref;
if( rln.referenceKey != null ) {
final String ref = getRefString(rln, rln.referenceKey);

ref = bufferVisit(new F<Void, Void>() {
@Override
public Void f(Void p) {
visitChildren(rln.referenceKey);
return null;
}
}).toString();
} else {
// in case the refkey is not with the link, we use the references found in the root node
ref = bufferVisit(new F<Void, Void>() {
@Override
public Void f(Void p) {
visitChildren(rln);
return null;
}
}).toString();
}
final String parentPageTitle = getHomePageTitle();
String url = ref;

Expand Down Expand Up @@ -721,11 +754,6 @@ public void visit(ReferenceNode rn) {
// nothing to do. already done in RootNode
}

@Override
public void visit(RefImageNode rin) {
notImplementedYet(rin);
}

@Override
public void visit(SimpleNode sn) {
notImplementedYet(sn);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,21 @@ public void shouldSupportReferenceNode() throws IOException {
assertThat(converted, containsString("[more complex google|http://google.com|Other google]"));
assertThat(converted, containsString("[google|http://google.com]"));
}

@Test
public void shouldSupportImgRefLink() throws IOException {
InputStream stream = getClass().getResourceAsStream("withImgRefLink.md");
InputStream inputStream = Site.processMarkdown(stream, "Test IMG");
String converted = IOUtils.toString(inputStream);

assertThat(converted, containsString("!http://www.lewe.com/wp-content/uploads/2016/03/conf-icon-64.png|alt=\"conf-icon\"|title=\"My conf-icon\"!"));
assertThat(converted, containsString("!conf-icon-64.png|alt=\"conf-icon\"|title=\"My conf-icon\"!"));
assertThat(converted, containsString("!conf-icon-64.png|alt=\"conf-icon\"!"));
assertThat(converted, containsString("!http://www.lewe.com/wp-content/uploads/2016/03/conf-icon-64.png|alt=\"conf-icon-y\"|title=\"My conf-icon\"!"));
assertThat(converted, containsString("!http://www.lewe.com/wp-content/uploads/2016/03/conf-icon-64.png|alt=\"conf-icon-y1\"!"));
assertThat(converted, containsString("!conf-icon-64.png|alt=\"conf-icon-y2\"!"));
assertThat(converted, containsString("!conf-icon-64.png|alt=\"conf-icon-none\"!"));
}


}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Site ref link test

* add an absolute ![conf-icon](http://www.lewe.com/wp-content/uploads/2016/03/conf-icon-64.png "My conf-icon") with title.
* add a relative ![conf-icon](conf-icon-64.png "My conf-icon") with title.
* add a relative ![conf-icon](conf-icon-64.png) without title.
* add a ref img ![conf-icon-y][y] with title.
* add a ref img ![conf-icon-y1][y1] without title.
* add a ref img ![conf-icon-y2][y2] relative.
* add a ref img ![conf-icon-none] relative with default refname.

[y]: http://www.lewe.com/wp-content/uploads/2016/03/conf-icon-64.png "My conf-icon"
[y1]: http://www.lewe.com/wp-content/uploads/2016/03/conf-icon-64.png
[y2]: conf-icon-64.png
[conf-icon-none]: conf-icon-64.png

0 comments on commit 6c9f471

Please sign in to comment.