Skip to content

Commit

Permalink
Transform url to <a href> in formatter
Browse files Browse the repository at this point in the history
  • Loading branch information
fgravin committed Jul 11, 2017
1 parent 40678e0 commit 7d5a96c
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
7 changes: 7 additions & 0 deletions web/pom.xml
Expand Up @@ -520,6 +520,13 @@
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.8</version>
</dependency>

<!--Detect urls in String and transform to html format-->
<dependency>
<groupId>org.nibor.autolink</groupId>
<artifactId>autolink</artifactId>
<version>0.6.0</version>
</dependency>
</dependencies>


Expand Down
Expand Up @@ -2,6 +2,7 @@ package common

import org.fao.geonet.api.records.formatters.FormatType
import org.fao.geonet.api.records.formatters.groovy.Environment
import org.nibor.autolink.*;

public class Functions {
org.fao.geonet.api.records.formatters.groovy.Handlers handlers;
Expand Down Expand Up @@ -47,4 +48,22 @@ public class Functions {
return '<div class="col-md-' + cols + '">' + content + '</div>'
}

def urlToHtml(content) {
LinkExtractor linkExtractor = LinkExtractor.builder()
.linkTypes(EnumSet.of(LinkType.URL)) // limit to URLs
.build();

Iterable<LinkSpan> links = linkExtractor.extractLinks(content);
String result = Autolink.renderLinks(content, links, new LinkRenderer() {
void render(LinkSpan link, CharSequence text, StringBuilder sb) {
sb.append("<a target=\"_blank\" href=\"");
sb.append(text, link.getBeginIndex(), link.getEndIndex());
sb.append("\">");
sb.append(text, link.getBeginIndex(), link.getEndIndex());
sb.append("</a>");
}
});
return result
}

}

2 comments on commit 7d5a96c

@fxprunayre
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build is failling since quite some days with



4 errors

	at org.fao.geonet.api.records.formatters.GroovyFormatter.loadScripts(GroovyFormatter.java:226)
	at org.fao.geonet.api.records.formatters.GroovyFormatter.getParentClassLoader(GroovyFormatter.java:160)
	at org.fao.geonet.api.records.formatters.GroovyFormatter.createTransformer(GroovyFormatter.java:115)
	at org.fao.geonet.api.records.formatters.GroovyFormatter.format(GroovyFormatter.java:97)
	at org.fao.geonet.api.records.formatters.FormatterApi.execXml(FormatterApi.java:373)
	at org.fao.geonet.api.records.formatters.FormatterApiIntegrationTest.testXmlFormatRelativeUrl(FormatterApiIntegrationTest.java:389)

testExecGroovy(org.fao.geonet.api.records.formatters.FormatterApiIntegrationTest)  Time elapsed: 0.947 sec  <<< FAILURE!
java.lang.AssertionError: Errors occurred while compiling files:

/work/nodes/default_data_dir/data/formatter/groovy/common/Handlers.groovy:
startup failed:
jimfs://VirtualCswApiTestded40abb-59da-41fe-90e0-5c4b33cb8dbe/work/nodes/default_data_dir/data/formatter/groovy/common/Handlers.groovy: 22: unable to resolve class common.Functions 
 @ line 22, column 5.
       common.Functions func
       ^

jimfs://VirtualCswApiTestded40abb-59da-41fe-90e0-5c4b33cb8dbe/work/nodes/default_data_dir/data/formatter/groovy/common/Handlers.groovy: 29: unable to resolve class common.Functions 
 @ line 29, column 16.
           func = new common.Functions(handlers: handlers, f:f, env:env)
                  ^

2 errors


/work/nodes/default_data_dir/data/formatter/groovy/common/Functions.groovy:
startup failed:
jimfs://VirtualCswApiTestded40abb-59da-41fe-90e0-5c4b33cb8dbe/work/nodes/default_data_dir/data/formatter/groovy/common/Functions.groovy: 52: unable to resolve class LinkExtractor 
 @ line 52, column 23.
           LinkExtractor linkExtractor = LinkExtractor.builder()
                         ^

jimfs://VirtualCswApiTestded40abb-59da-41fe-90e0-5c4b33cb8dbe/work/nodes/default_data_dir/data/formatter/groovy/common/Functions.groovy: 56: unable to resolve class LinkSpan 
 @ line 56, column 18.
           Iterable<LinkSpan> links = linkExtractor.extractLinks(content);
                    ^

jimfs://VirtualCswApiTestded40abb-59da-41fe-90e0-5c4b33cb8dbe/work/nodes/default_data_dir/data/formatter/groovy/common/Functions.groovy: 57: unable to resolve class LinkRenderer 
 @ line 57, column 82.
   t, links, new LinkRenderer()  {
                                 ^

jimfs://VirtualCswApiTestded40abb-59da-41fe-90e0-5c4b33cb8dbe/work/nodes/default_data_dir/data/formatter/groovy/common/Functions.groovy: 58: unable to resolve class LinkSpan 
 @ line 58, column 25.
               void render(LinkSpan link, CharSequence text, StringBuilder sb) {
                    

Could this be related ?

@fxprunayre
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm that dropping that function content make the test pass again.

Please sign in to comment.