Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
fixed #12
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreau committed Feb 24, 2014
1 parent 05bf117 commit 95f8921
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 1 deletion.
8 changes: 7 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

<groupId>com.mgreau.wwsmad</groupId>
<artifactId>when-websocket-met-asciidoctor</artifactId>
<version>0.1.0-alpha2-SNAPSHOT</version>
<version>0.1.0-alpha3-SNAPSHOT</version>
<packaging>war</packaging>

<name>wWSmAD : When Websocket met Asciidoctor : </name>
Expand Down Expand Up @@ -145,6 +145,12 @@
<groupId>org.asciidoctor</groupId>
<artifactId>asciidoctor-java-integration</artifactId>
</dependency>
<!-- PostProcessor -->
<dependency>
<groupId>org.jsoup</groupId>
<artifactId>jsoup</artifactId>
<version>1.7.1</version>
</dependency>

<!-- Tests -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.asciidoctor.DocumentHeader;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.asciidoctor.extension.ExtensionRegistry;

@ApplicationScoped
public class AsciidoctorProcessor {
Expand All @@ -25,6 +26,10 @@ public class AsciidoctorProcessor {
// tag::render[]
public String renderAsDocument(String source, String baseDir) {
logger.info("Start rendering adoc");

ExtensionRegistry extensionRegistry = this.delegate.extensionRegistry();
extensionRegistry.postprocessor(IFrameAnchorPostProcessor.class);

return delegate.render(source, OptionsBuilder.options()
.safe(SafeMode.UNSAFE).backend("html5").headerFooter(true).eruby("erubis")
//.option("base_dir", baseDir)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mgreau.wwsmad.asciidoctor;

import org.asciidoctor.extension.Postprocessor;
import org.asciidoctor.internal.DocumentRuby;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;

/**
* #12 : iframe : making linking between anchors work.
*
* @author greau.maxime@gmail.com
*
*/
public class IFrameAnchorPostProcessor extends Postprocessor {

public IFrameAnchorPostProcessor(DocumentRuby documentRuby) {
super(documentRuby);
}

@Override
public String process(String output) {

final Document document = Jsoup.parse(output);
final String js = "js/iframe/iframe_anchors.js";

if (document.getElementsByAttributeValue("src", js).size() == 0){
Element head = document.getElementsByTag("head").first();

head.appendElement("script").attr("type", "text/javascript").attr("src", "http://code.jquery.com/jquery-1.7.2.min.js");
head.appendElement("script").attr("type", "text/javascript").attr("src", js);
}

return document.html();
}

}
27 changes: 27 additions & 0 deletions src/main/webapp/js/iframe/iframe_anchors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
$(function () {
//var iframeOffset = $("#html5-rendered", window.parent.document).offset();
$("a").each(function () {
var link = $(this);
var href = link.attr("href");
if (href && href[0] == "#") {
var name = href.substring(1);
$(this).click(function () {
var nameElement = $("[name='" + name + "']");
var idElement = $("#" + name);
var element = null;
if (nameElement.length > 0) {
element = nameElement;
} else if (idElement.length > 0) {
element = idElement;
}

if (element) {
var offset = element.offset();
window.scrollTo(offset.left, offset.top);
}

return false;
});
}
});
});

0 comments on commit 95f8921

Please sign in to comment.