Skip to content

Commit 6b4a9d7

Browse files
authored
Merge pull request #416 from wordpress-mobile/issue/331-plugin-architecture
Plugin refactoring
2 parents 3823e3c + 3dcfbbc commit 6b4a9d7

File tree

5 files changed

+53
-46
lines changed

5 files changed

+53
-46
lines changed

aztec/src/main/java/org/wordpress/aztec/Html.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.ccil.cowan.tagsoup.HTMLSchema;
3535
import org.ccil.cowan.tagsoup.Parser;
3636
import org.wordpress.aztec.plugins.IAztecPlugin;
37-
import org.wordpress.aztec.plugins.ICommentHandler;
37+
import org.wordpress.aztec.plugins.html2visual.IHtmlCommentHandler;
3838
import org.wordpress.aztec.spans.IAztecBlockSpan;
3939
import org.wordpress.aztec.spans.AztecCodeSpan;
4040
import org.wordpress.aztec.spans.AztecCursorSpan;
@@ -708,8 +708,8 @@ public void comment(char[] chars, int start, int length) throws SAXException {
708708
boolean wasCommentHandled = false;
709709
if (plugins != null) {
710710
for (IAztecPlugin plugin : plugins) {
711-
if (plugin instanceof ICommentHandler) {
712-
wasCommentHandled = ((ICommentHandler) plugin).handleCommentHtml(comment, spannableStringBuilder, context, nestingLevel);
711+
if (plugin instanceof IHtmlCommentHandler) {
712+
wasCommentHandled = ((IHtmlCommentHandler) plugin).handleComment(comment, spannableStringBuilder, nestingLevel);
713713
if (wasCommentHandled) {
714714
break;
715715
}

aztec/src/main/kotlin/org/wordpress/aztec/AztecParser.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import android.text.style.CharacterStyle
2424
import org.wordpress.aztec.AztecText.OnImageTappedListener
2525
import org.wordpress.aztec.AztecText.OnVideoTappedListener
2626
import org.wordpress.aztec.plugins.IAztecPlugin
27-
import org.wordpress.aztec.plugins.ICommentHandler
27+
import org.wordpress.aztec.plugins.visual2html.IInlineSpanHandler
2828
import org.wordpress.aztec.spans.*
2929
import org.wordpress.aztec.util.SpanWrapper
3030
import java.util.*
@@ -414,9 +414,9 @@ class AztecParser(val plugins: List<IAztecPlugin> = ArrayList()) {
414414
out.append("<!--")
415415
}
416416

417-
plugins.filter { it is ICommentHandler && it.canHandle(span) }
417+
plugins.filter { it is IInlineSpanHandler && it.canHandleSpan(span) }
418418
.forEach {
419-
(it as ICommentHandler).handleCommentSpanStart(out, span)
419+
(it as IInlineSpanHandler).handleSpanStart(out, span)
420420
if (!it.shouldParseContent()) {
421421
i = next
422422
}
@@ -450,9 +450,9 @@ class AztecParser(val plugins: List<IAztecPlugin> = ArrayList()) {
450450
out.append("-->")
451451
}
452452

453-
plugins.filter { it is ICommentHandler && it.canHandle(span) }
453+
plugins.filter { it is IInlineSpanHandler && it.canHandleSpan(span) }
454454
.forEach {
455-
(it as ICommentHandler).handleCommentSpanEnd(out, span)
455+
(it as IInlineSpanHandler).handleSpanEnd(out, span)
456456
}
457457

458458
if (span is HiddenHtmlSpan) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.wordpress.aztec.plugins.html2visual
2+
3+
import android.text.Editable
4+
5+
/**
6+
* An interface for HTML comment processing plugins.
7+
*/
8+
interface IHtmlCommentHandler {
9+
/**
10+
* A plugin handler used by [org.wordpress.aztec.Html] parser during HTML-to-span parsing.
11+
*
12+
* This method is called when a comment is encountered in HTML.
13+
*
14+
* @param text the content/text of the comment.
15+
* @param output the parsed output [Editable], used for span manipulation.
16+
* @param nestingLevel the nesting level within the HTML DOM tree.
17+
*
18+
* @return true if this plugin handled the comment and no other handler should be called, false otherwise.
19+
*/
20+
fun handleComment(text: String, output: Editable, nestingLevel: Int) : Boolean {
21+
return true
22+
}
23+
}

aztec/src/main/kotlin/org/wordpress/aztec/plugins/ICommentHandler.kt renamed to aztec/src/main/kotlin/org/wordpress/aztec/plugins/visual2html/IInlineSpanHandler.kt

Lines changed: 8 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.wordpress.aztec.plugins
1+
package org.wordpress.aztec.plugins.visual2html
22

33
import android.annotation.SuppressLint
44
import android.content.Context
@@ -8,43 +8,27 @@ import android.text.style.CharacterStyle
88
import org.wordpress.aztec.plugins.IAztecPlugin
99

1010
/**
11-
* An interface for HTML comment processing plugins.
11+
* An interface for processing spans during visual-to-HTML.
1212
*/
1313
@SuppressLint("NewApi")
14-
interface ICommentHandler : IAztecPlugin {
14+
interface IInlineSpanHandler : IAztecPlugin {
1515
/**
16-
* Determines, whether the content of a comment (the text) should be parsed/rendered by [org.wordpress.aztec.AztecParser]
16+
* Determines, whether the content of a span (text, if any) should be parsed/rendered by [org.wordpress.aztec.AztecParser]
1717
*
18-
* @return true if text should be parsed, false otherwise.
18+
* @return true if content should be parsed, false otherwise.
1919
*/
2020
fun shouldParseContent(): Boolean {
2121
return true
2222
}
2323

24-
/**
25-
* A plugin handler used by [org.wordpress.aztec.Html] parser during HTML-to-span parsing.
26-
*
27-
* This method is called when a comment is encountered in HTML.
28-
*
29-
* @param text the content/text of the comment.
30-
* @param output the parsed output [Editable], used for span manipulation.
31-
* @param context the Android context.
32-
* @param nestingLevel the nesting level within the HTML DOM tree.
33-
*
34-
* @return true if this plugin handled the comment and no other handler should be called, false otherwise.
35-
*/
36-
fun handleCommentHtml(text: String, output: Editable, context: Context, nestingLevel: Int) : Boolean {
37-
return true
38-
}
39-
4024
/**
4125
* Determines, whether the plugin can handle a particular [span] type.
4226
*
4327
* This method is called by [org.wordpress.aztec.AztecParser] during span-to-HTML parsing.
4428
*
4529
* @return true for compatible spans, false otherwise.
4630
*/
47-
fun canHandle(span: CharacterStyle): Boolean {
31+
fun canHandleSpan(span: CharacterStyle): Boolean {
4832
return true
4933
}
5034

@@ -56,7 +40,7 @@ interface ICommentHandler : IAztecPlugin {
5640
* @param html the resulting HTML string output.
5741
* @param span the encountered span.
5842
*/
59-
fun handleCommentSpanStart(html: StringBuilder, span: CharacterStyle)
43+
fun handleSpanStart(html: StringBuilder, span: CharacterStyle)
6044

6145
/**
6246
* A plugin handler used by [org.wordpress.aztec.AztecParser] during span-to-HTML parsing.
@@ -66,5 +50,5 @@ interface ICommentHandler : IAztecPlugin {
6650
* @param html the resulting HTML string output.
6751
* @param span the encountered span.
6852
*/
69-
fun handleCommentSpanEnd(html: StringBuilder, span: CharacterStyle)
53+
fun handleSpanEnd(html: StringBuilder, span: CharacterStyle)
7054
}

wordpress-comments/src/main/java/org/wordpress/aztec/plugins/wpcomments/WordPressCommentsPlugin.kt

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,35 @@
11
package org.wordpress.aztec.plugins.wpcomments
22

3-
import android.content.Context
43
import android.support.v4.content.ContextCompat
54
import android.text.Editable
65
import android.text.Spannable
76
import android.text.style.CharacterStyle
87
import org.wordpress.aztec.AztecText
98
import org.wordpress.aztec.Constants
10-
import org.wordpress.aztec.plugins.ICommentHandler
9+
import org.wordpress.aztec.plugins.html2visual.IHtmlCommentHandler
10+
import org.wordpress.aztec.plugins.visual2html.IInlineSpanHandler
1111
import org.wordpress.aztec.plugins.wpcomments.spans.WordPressCommentSpan
1212

13-
class WordPressCommentsPlugin(val visualEditor: AztecText) : ICommentHandler {
13+
class WordPressCommentsPlugin(val visualEditor: AztecText) : IInlineSpanHandler, IHtmlCommentHandler {
1414

15-
override fun canHandle(span: CharacterStyle): Boolean {
15+
override fun canHandleSpan(span: CharacterStyle): Boolean {
1616
return span is WordPressCommentSpan
1717
}
1818

1919
override fun shouldParseContent(): Boolean {
2020
return false
2121
}
2222

23-
override fun handleCommentSpanStart(out: StringBuilder, span: CharacterStyle) {
24-
out.append("<!--")
25-
out.append((span as WordPressCommentSpan).commentText)
23+
override fun handleSpanStart(html: StringBuilder, span: CharacterStyle) {
24+
html.append("<!--")
25+
html.append((span as WordPressCommentSpan).commentText)
2626
}
2727

28-
override fun handleCommentSpanEnd(out: StringBuilder, span: CharacterStyle){
29-
out.append("-->")
28+
override fun handleSpanEnd(html: StringBuilder, span: CharacterStyle){
29+
html.append("-->")
3030
}
3131

32-
override fun handleCommentHtml(text: String, output: Editable, context: Context, nestingLevel: Int) : Boolean {
32+
override fun handleComment(text: String, output: Editable, nestingLevel: Int) : Boolean {
3333

3434
val spanStart = output.length
3535

@@ -40,8 +40,8 @@ class WordPressCommentsPlugin(val visualEditor: AztecText) : ICommentHandler {
4040
output.setSpan(
4141
WordPressCommentSpan(
4242
text,
43-
context,
44-
ContextCompat.getDrawable(context, R.drawable.img_more),
43+
visualEditor.context,
44+
ContextCompat.getDrawable(visualEditor.context, R.drawable.img_more),
4545
nestingLevel
4646
),
4747
spanStart,
@@ -56,8 +56,8 @@ class WordPressCommentsPlugin(val visualEditor: AztecText) : ICommentHandler {
5656
output.setSpan(
5757
WordPressCommentSpan(
5858
text,
59-
context,
60-
ContextCompat.getDrawable(context, R.drawable.img_page),
59+
visualEditor.context,
60+
ContextCompat.getDrawable(visualEditor.context, R.drawable.img_page),
6161
nestingLevel
6262
),
6363
spanStart,

0 commit comments

Comments
 (0)