From 3335c2c97db9e490f64ff96c9839650920f92c58 Mon Sep 17 00:00:00 2001 From: Otto Jongerius Date: Mon, 17 Sep 2018 12:15:23 +1200 Subject: [PATCH] optimise imports, reformat and remove dead code --- .../markdown/MarkdownFromURLMacro.java | 207 ++++++++---------- .../confluence/markdown/MarkdownMacro.java | 82 +++---- 2 files changed, 124 insertions(+), 165 deletions(-) diff --git a/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java b/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java index 386c139..0002a76 100644 --- a/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java +++ b/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownFromURLMacro.java @@ -2,52 +2,43 @@ package com.atlassian.plugins.confluence.markdown; import com.atlassian.confluence.content.render.xhtml.ConversionContext; import com.atlassian.confluence.content.render.xhtml.DefaultConversionContext; -import com.atlassian.confluence.content.render.xhtml.XhtmlException; import com.atlassian.confluence.macro.Macro; import com.atlassian.confluence.macro.MacroExecutionException; -import com.atlassian.confluence.xhtml.api.MacroDefinition; -import com.atlassian.confluence.xhtml.api.MacroDefinitionHandler; import com.atlassian.confluence.xhtml.api.XhtmlContent; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - +import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import com.atlassian.renderer.RenderContext; import com.atlassian.renderer.v2.RenderMode; import com.atlassian.renderer.v2.macro.BaseMacro; import com.atlassian.renderer.v2.macro.MacroException; - -//import com.atlassian.plugin.spring.scanner.annotation.component.Scanned; -import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import com.atlassian.webresource.api.assembler.PageBuilderService; -import org.springframework.beans.factory.annotation.Autowired; - - import com.vladsch.flexmark.ast.Node; -import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension; -import com.vladsch.flexmark.ext.tables.TablesExtension; -import com.vladsch.flexmark.ext.ins.InsExtension; -import com.vladsch.flexmark.ext.definition.DefinitionExtension; -import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension; -import com.vladsch.flexmark.ext.footnotes.FootnoteExtension; -import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension; -import com.vladsch.flexmark.ext.autolink.AutolinkExtension; import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension; -import com.vladsch.flexmark.superscript.SuperscriptExtension; +import com.vladsch.flexmark.ext.autolink.AutolinkExtension; +import com.vladsch.flexmark.ext.definition.DefinitionExtension; +import com.vladsch.flexmark.ext.footnotes.FootnoteExtension; +import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension; +import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension; +import com.vladsch.flexmark.ext.ins.InsExtension; +import com.vladsch.flexmark.ext.tables.TablesExtension; +import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension; import com.vladsch.flexmark.ext.youtube.embedded.YouTubeLinkExtension; import com.vladsch.flexmark.html.HtmlRenderer; import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.superscript.SuperscriptExtension; import com.vladsch.flexmark.util.options.MutableDataSet; +import org.springframework.beans.factory.annotation.Autowired; + +import java.io.BufferedReader; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.MalformedURLException; +import java.net.URL; +import java.util.Arrays; +import java.util.Map; -import java.net.*; -import java.io.*; - -//@Scanned -public class MarkdownFromURLMacro extends BaseMacro implements Macro -{ +public class MarkdownFromURLMacro extends BaseMacro implements Macro { private final XhtmlContent xhtmlUtils; @@ -59,105 +50,93 @@ public class MarkdownFromURLMacro extends BaseMacro implements Macro this.xhtmlUtils = xhtmlUtils; } -// public MarkdownFromURLMacro(XhtmlContent xhtmlUtils) -// { -// this.xhtmlUtils = xhtmlUtils; -// } @Override - public BodyType getBodyType() - { + public BodyType getBodyType() { return BodyType.PLAIN_TEXT; } @Override - public OutputType getOutputType() - { + public OutputType getOutputType() { return OutputType.BLOCK; } @Override - public String execute(Map parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException - { + public String execute(Map parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException { - if (bodyContent != null) { - pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs"); + if (bodyContent != null) { + pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs"); - MutableDataSet options = new MutableDataSet(); + MutableDataSet options = new MutableDataSet(); - options.set(Parser.EXTENSIONS, Arrays.asList( - TablesExtension.create(), - StrikethroughSubscriptExtension.create(), - InsExtension.create(), - TaskListExtension.create(), - FootnoteExtension.create(), - WikiLinkExtension.create(), - DefinitionExtension.create(), - AnchorLinkExtension.create(), - AutolinkExtension.create(), - SuperscriptExtension.create(), - YouTubeLinkExtension.create() + options.set(Parser.EXTENSIONS, Arrays.asList( + TablesExtension.create(), + StrikethroughSubscriptExtension.create(), + InsExtension.create(), + TaskListExtension.create(), + FootnoteExtension.create(), + WikiLinkExtension.create(), + DefinitionExtension.create(), + AnchorLinkExtension.create(), + AutolinkExtension.create(), + SuperscriptExtension.create(), + YouTubeLinkExtension.create() - )); + )); - String highlightjs = ""; + String highlightjs = ""; - class privateRepositoryException extends Exception { - public privateRepositoryException(String message) { - super(message); - } - } - - Parser parser = Parser.builder(options).build(); - HtmlRenderer renderer = HtmlRenderer.builder(options).build(); - - String exceptionsToReturn = ""; - String html = ""; - String toParse = ""; - try { - URL importFrom = new URL(bodyContent); - BufferedReader in = new BufferedReader( - new InputStreamReader(importFrom.openStream()) - ); - String inputLine; - while ((inputLine = in.readLine()) != null) { - toParse = toParse + "\n" + inputLine; - } - in.close(); - toParse = toParse.trim(); - if (toParse.startsWith("\n\n OpenID transaction in progress")) { - throw new privateRepositoryException("Cannot import from private repository."); - }else { - Node document = parser.parse(toParse); - html = renderer.render(document) + highlightjs; - } - } - catch (MalformedURLException u) { - exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Invalid URL.
Please enter a valid URL. If you are not trying to import markdown from a URL, use the Markdown macro instead of the Markdown from URL macro.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; - } - catch (privateRepositoryException p) { - exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Importing from private Bitbucket repositories is not supported.
Please make your repository public before importing. Alternatively, you can copy and paste your markdown into the Markdown macro.
If you are allowed access, you can find the markdown file here.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; - } - catch (FileNotFoundException f) { - exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: URL does not exist.
" + bodyContent + "
Please double check your URL. Perhaps you made a typo or perhaps the page has been moved.
This can also be caused by changing the Github repository containing the file from public to private. If this is the case go back to the raw file and re-copy the link.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; - } - catch (IOException e) { - exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Unexpected error.
" + e.toString() + "
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; - } - finally { - if (exceptionsToReturn != "") { - html = "

" + exceptionsToReturn + "

"; - } - return html; - } - }else { - return ""; - } + class privateRepositoryException extends Exception { + public privateRepositoryException(String message) { + super(message); + } + } + + Parser parser = Parser.builder(options).build(); + HtmlRenderer renderer = HtmlRenderer.builder(options).build(); + + String exceptionsToReturn = ""; + String html = ""; + String toParse = ""; + try { + URL importFrom = new URL(bodyContent); + BufferedReader in = new BufferedReader( + new InputStreamReader(importFrom.openStream()) + ); + String inputLine; + while ((inputLine = in.readLine()) != null) { + toParse = toParse + "\n" + inputLine; + } + in.close(); + toParse = toParse.trim(); + if (toParse.startsWith("\n\n OpenID transaction in progress")) { + throw new privateRepositoryException("Cannot import from private repository."); + } else { + Node document = parser.parse(toParse); + html = renderer.render(document) + highlightjs; + } + } catch (MalformedURLException u) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Invalid URL.
Please enter a valid URL. If you are not trying to import markdown from a URL, use the Markdown macro instead of the Markdown from URL macro.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } catch (privateRepositoryException p) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Importing from private Bitbucket repositories is not supported.
Please make your repository public before importing. Alternatively, you can copy and paste your markdown into the Markdown macro.
If you are allowed access, you can find the markdown file here.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } catch (FileNotFoundException f) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: URL does not exist.
" + bodyContent + "
Please double check your URL. Perhaps you made a typo or perhaps the page has been moved.
This can also be caused by changing the Github repository containing the file from public to private. If this is the case go back to the raw file and re-copy the link.
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } catch (IOException e) { + exceptionsToReturn = exceptionsToReturn + "Error with Markdown From URL macro: Unexpected error.
" + e.toString() + "
For support visit our Q&A in the Atlassian Community. You can ask a new question by clicking the \"Create\" button on the top right of the Q&A.
"; + } finally { + if (exceptionsToReturn != "") { + html = "

" + exceptionsToReturn + "

"; + } + return html; + } + } else { + return ""; + } } @Override @@ -175,7 +154,7 @@ public class MarkdownFromURLMacro extends BaseMacro implements Macro try { return execute(map, s, new DefaultConversionContext(renderContext)); } catch (MacroExecutionException e) { - throw new MacroException(e.getMessage(),e); + throw new MacroException(e.getMessage(), e); } } -} \ No newline at end of file +} diff --git a/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownMacro.java b/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownMacro.java index e7077bd..76db976 100644 --- a/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownMacro.java +++ b/src/main/java/com/atlassian/plugins/confluence/markdown/MarkdownMacro.java @@ -2,48 +2,36 @@ package com.atlassian.plugins.confluence.markdown; import com.atlassian.confluence.content.render.xhtml.ConversionContext; import com.atlassian.confluence.content.render.xhtml.DefaultConversionContext; -import com.atlassian.confluence.content.render.xhtml.XhtmlException; import com.atlassian.confluence.macro.Macro; import com.atlassian.confluence.macro.MacroExecutionException; -import com.atlassian.confluence.xhtml.api.MacroDefinition; -import com.atlassian.confluence.xhtml.api.MacroDefinitionHandler; import com.atlassian.confluence.xhtml.api.XhtmlContent; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Map; - +import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import com.atlassian.renderer.RenderContext; import com.atlassian.renderer.v2.RenderMode; import com.atlassian.renderer.v2.macro.BaseMacro; import com.atlassian.renderer.v2.macro.MacroException; - -//import com.atlassian.plugin.spring.scanner.annotation.component.Scanned; -import com.atlassian.plugin.spring.scanner.annotation.imports.ComponentImport; import com.atlassian.webresource.api.assembler.PageBuilderService; -import org.springframework.beans.factory.annotation.Autowired; - import com.vladsch.flexmark.ast.Node; -import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension; -import com.vladsch.flexmark.ext.tables.TablesExtension; -import com.vladsch.flexmark.ext.ins.InsExtension; -import com.vladsch.flexmark.ext.definition.DefinitionExtension; -import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension; -import com.vladsch.flexmark.ext.footnotes.FootnoteExtension; -import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension; -import com.vladsch.flexmark.ext.autolink.AutolinkExtension; import com.vladsch.flexmark.ext.anchorlink.AnchorLinkExtension; -import com.vladsch.flexmark.superscript.SuperscriptExtension; +import com.vladsch.flexmark.ext.autolink.AutolinkExtension; +import com.vladsch.flexmark.ext.definition.DefinitionExtension; +import com.vladsch.flexmark.ext.footnotes.FootnoteExtension; +import com.vladsch.flexmark.ext.gfm.strikethrough.StrikethroughSubscriptExtension; +import com.vladsch.flexmark.ext.gfm.tasklist.TaskListExtension; +import com.vladsch.flexmark.ext.ins.InsExtension; +import com.vladsch.flexmark.ext.tables.TablesExtension; +import com.vladsch.flexmark.ext.wikilink.WikiLinkExtension; import com.vladsch.flexmark.ext.youtube.embedded.YouTubeLinkExtension; import com.vladsch.flexmark.html.HtmlRenderer; import com.vladsch.flexmark.parser.Parser; +import com.vladsch.flexmark.superscript.SuperscriptExtension; import com.vladsch.flexmark.util.options.MutableDataSet; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.Arrays; +import java.util.Map; -//@Scanned -public class MarkdownMacro extends BaseMacro implements Macro -{ +public class MarkdownMacro extends BaseMacro implements Macro { private final XhtmlContent xhtmlUtils; @@ -55,26 +43,18 @@ public class MarkdownMacro extends BaseMacro implements Macro this.xhtmlUtils = xhtmlUtils; } -// public MarkdownMacro(XhtmlContent xhtmlUtils) -// { -// this.xhtmlUtils = xhtmlUtils; -// } - @Override - public BodyType getBodyType() - { + public BodyType getBodyType() { return BodyType.PLAIN_TEXT; } @Override - public OutputType getOutputType() - { + public OutputType getOutputType() { return OutputType.BLOCK; } @Override - public String execute(Map parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException - { + public String execute(Map parameters, String bodyContent, ConversionContext conversionContext) throws MacroExecutionException { pageBuilderService.assembler().resources().requireWebResource("com.atlassian.plugins.confluence.markdown.confluence-markdown-macro:highlightjs"); @@ -82,17 +62,17 @@ public class MarkdownMacro extends BaseMacro implements Macro MutableDataSet options = new MutableDataSet(); options.set(Parser.EXTENSIONS, Arrays.asList( - TablesExtension.create(), - StrikethroughSubscriptExtension.create(), - InsExtension.create(), - TaskListExtension.create(), - FootnoteExtension.create(), - WikiLinkExtension.create(), - DefinitionExtension.create(), - AnchorLinkExtension.create(), - AutolinkExtension.create(), - SuperscriptExtension.create(), - YouTubeLinkExtension.create() + TablesExtension.create(), + StrikethroughSubscriptExtension.create(), + InsExtension.create(), + TaskListExtension.create(), + FootnoteExtension.create(), + WikiLinkExtension.create(), + DefinitionExtension.create(), + AnchorLinkExtension.create(), + AutolinkExtension.create(), + SuperscriptExtension.create(), + YouTubeLinkExtension.create() )); @@ -107,7 +87,7 @@ public class MarkdownMacro extends BaseMacro implements Macro HtmlRenderer renderer = HtmlRenderer.builder(options).build(); Node document = parser.parse(bodyContent); - String html = renderer.render(document ) + highlightjs; // "

This is Sparta

\n" + String html = renderer.render(document) + highlightjs; // "

This is Sparta

\n" return html; } @@ -127,7 +107,7 @@ public class MarkdownMacro extends BaseMacro implements Macro try { return execute(map, s, new DefaultConversionContext(renderContext)); } catch (MacroExecutionException e) { - throw new MacroException(e.getMessage(),e); + throw new MacroException(e.getMessage(), e); } } -} \ No newline at end of file +}