This is an automated email from the ASF dual-hosted git repository. juanpablo pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/jspwiki.git
commit 087f90afab36f7962367a7dbe7dac0339e93c400 Author: Juan Pablo Santos RodrÃguez <[email protected]> AuthorDate: Tue Jul 12 22:59:07 2022 +0200 JSPWiki to Markdown syntax conversion tool --- .../markdown/migration/WikiSyntaxConverter.java | 74 ++++++++++++++++++++++ .../migration/filters/TidyMarkupFilter.java | 18 ++++++ .../parser/JSPWikiToMarkdownMarkupParser.java | 63 ++++++++++++++++++ 3 files changed, 155 insertions(+) diff --git a/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/WikiSyntaxConverter.java b/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/WikiSyntaxConverter.java new file mode 100644 index 000000000..ce3856fdd --- /dev/null +++ b/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/WikiSyntaxConverter.java @@ -0,0 +1,74 @@ +/* + Licensed to the Apache Software Foundation (ASF) under one + or more contributor license agreements. See the NOTICE file + distributed with this work for additional information + regarding copyright ownership. The ASF licenses this file + to you 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.apache.wiki.markdown.migration; + +import org.apache.wiki.TestEngine; +import org.apache.wiki.api.core.Context; +import org.apache.wiki.api.core.ContextEnum; +import org.apache.wiki.api.core.Engine; +import org.apache.wiki.api.core.Page; +import org.apache.wiki.api.spi.Wiki; +import org.apache.wiki.htmltowiki.HtmlStringToWikiTranslator; +import org.apache.wiki.markdown.migration.parser.JSPWikiToMarkdownMarkupParser; +import org.apache.wiki.pages.PageManager; +import org.apache.wiki.plugin.PluginManager; +import org.apache.wiki.render.RenderingManager; +import org.junit.jupiter.api.Test; + +import java.util.Collection; + +import static org.apache.wiki.TestEngine.with; + + +public class WikiSyntaxConverter { + + @Test + void jspwikiToMarkdownConverter() throws Exception { + final Engine jspw = buildEngine( "jspwiki", "../jspwiki-wikipages/en/src/main/resources" ); + final Engine md = buildEngine( "markdown", "./target/pages-markdown" ); + jspw.getManager( PluginManager.class ).enablePlugins( false ); + + final Collection< Page > pages = jspw.getManager( PageManager.class ).getAllPages(); + for( final Page p : pages ) { + final Context context = Wiki.context().create( jspw, p ); + context.setRequestContext( ContextEnum.PAGE_NONE.getRequestContext() ); + context.setVariable( Context.VAR_WYSIWYG_EDITOR_MODE, Boolean.TRUE ); + final String pagedata = jspw.getManager( PageManager.class ).getPureText( p.getName(), p.getVersion() ); + final String html = jspw.getManager( RenderingManager.class ).textToHTML( context, pagedata, null, null, null, false, false ); + final String syntax = new HtmlStringToWikiTranslator( md ).translate( html ); + final Context contextMD = Wiki.context().create( md, p ); + md.getManager( PageManager.class ).saveText( contextMD, clean( syntax ) ); + } + } + + Engine buildEngine( final String syntax, final String pageDir ) { + return TestEngine.build( with( "jspwiki.fileSystemProvider.pageDir", pageDir ), + with( RenderingManager.PROP_PARSER, JSPWikiToMarkdownMarkupParser.class.getName() ), // will be overwritten if jspwiki.syntax=markdown + with( "jspwiki.test.disable-clean-props", "true" ), + with( "jspwiki.workDir", "./target/workDir" + syntax ), + with( "appender.rolling.fileName", "./target/wiki-" + syntax + ".log" ), + with( "jspwiki.cache.enable", "false" ), + with( "jspwiki.syntax", syntax ) ); + } + + String clean( final String wikiSyntax ) { + return wikiSyntax; + } + +} diff --git a/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/filters/TidyMarkupFilter.java b/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/filters/TidyMarkupFilter.java new file mode 100644 index 000000000..eda7311d4 --- /dev/null +++ b/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/filters/TidyMarkupFilter.java @@ -0,0 +1,18 @@ +package org.apache.wiki.markdown.migration.filters; + +import org.apache.wiki.api.core.Context; +import org.apache.wiki.api.exceptions.FilterException; +import org.apache.wiki.api.filters.BasePageFilter; + + +/** + * Tidy up wiki markup in order to ease html -> markdown conversion. + */ +public class TidyMarkupFilter extends BasePageFilter { + + @Override + public String preTranslate( final Context context, final String content ) throws FilterException { + //content.replace( "", "" ) + return super.preTranslate( context, content ); + } +} diff --git a/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/parser/JSPWikiToMarkdownMarkupParser.java b/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/parser/JSPWikiToMarkdownMarkupParser.java new file mode 100644 index 000000000..999f43609 --- /dev/null +++ b/jspwiki-markdown/src/test/java/org/apache/wiki/markdown/migration/parser/JSPWikiToMarkdownMarkupParser.java @@ -0,0 +1,63 @@ +package org.apache.wiki.markdown.migration.parser; + +import org.apache.wiki.api.core.Context; +import org.apache.wiki.parser.JSPWikiMarkupParser; +import org.apache.wiki.parser.PluginContent; +import org.apache.wiki.parser.WikiDocument; +import org.jdom2.Content; +import org.jdom2.Element; +import org.jdom2.Text; + +import java.io.IOException; +import java.io.Reader; +import java.util.List; + + +public class JSPWikiToMarkdownMarkupParser extends JSPWikiMarkupParser { + + /** + * Creates a markup parser. + * + * @param context The WikiContext which controls the parsing + * @param in Where the data is read from. + */ + public JSPWikiToMarkdownMarkupParser( final Context context, final Reader in ) { + super( context, in ); + } + + /** {@inheritDoc} */ + @Override + public WikiDocument parse() throws IOException { + final WikiDocument doc = super.parse(); + translatePluginACLAndVariableTextLinksToMarkdown( doc.getRootElement(), 0 ); + return doc; + } + + void translatePluginACLAndVariableTextLinksToMarkdown( final Content element, final int childNumber ) { + if( element instanceof PluginContent ) { + final PluginContent plugin = ( PluginContent ) element; + final String str = plugin.getText(); + if( str.startsWith( "[{" ) && str.endsWith( "}]" ) ) { + final Element parent = plugin.getParent(); + plugin.detach(); + if( parent != null ) { + parent.addContent( childNumber, new Text( str + "()" ) ); + } + } + } else if( element instanceof Text ) { + final Text text = ( Text )element; + if( text.getText().startsWith( "[{" ) && text.getText().endsWith( "}]" ) ) { + text.append( "()" ); + } + } else if( element instanceof Element ) { + final Element base = ( Element )element; + base.getContent(); + final List< Content > content = base.getContent(); + for( int i = 0; i < content.size(); i++ ) { + final Content c = content.get( i ); + translatePluginACLAndVariableTextLinksToMarkdown( c, i ); + } + } + } + +}
