Skip to content

Latest commit

 

History

History
88 lines (69 loc) · 2.21 KB

README.md

File metadata and controls

88 lines (69 loc) · 2.21 KB

SnooParser - Reddit HTML Parser

Add to Gradle

Add the following to your app build.gradle file and sync.

Jitpack Instructions

dependencies {
  ...
  compile 'com.github.vishnumad:SnooParser:LATEST-COMMIT-HASH'
}
repositories {
  maven { url 'https://jitpack.io' }
}

Usage

XML

<xyz.vishnum.snoohtmlparser.SnooView
            android:id="@+id/snoo_body_view"
            android:padding="16dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

See example

Java

Basic usage

SnooView snooView = (SnooView) findViewById(R.id.snoo_body_view);
SnooParser parser = new SnooParser();

String exampleHtml = getCommentHtml();
List<RedditBlock> blocks = parser.getBlocks(exampleHtml);

snooView.setBlocks(blocks);

Handling link clicks — Defaults to open in browser, but you can add your own listener

snooView.setOnUrlClickListener(new SnooView.OnUrlClickListener() {
    @Override
    public void onClick(String url) {
        Log.d(TAG, "Url clicked: " + url);
    }
});

Custom tag handling<pre>, <hr/>, and <table> tags will not be affected

SnooParser parser = new SnooParser();

// Override how the <strong> tag is handled
// All bold text with the strong tag will be displayed as blue colored text
parser.replaceHandler("strong", new ExampleStrongHandler());
public class ExampleStrongHandler extends TagNodeHandler {
    @Override
    public void handleTagNode(TagNode node, SpannableStringBuilder builder, int start, int end) {
        builder.setSpan(new ForegroundColorSpan(Color.BLUE), start, end,
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    }
}

See full example

External dependencies

This project depends on the JSoup and HtmlSpanner libraries

Todo

  • Whitespace (padding/margins) around codeblocks and tables
  • Background color for code block
  • Color for HR block
  • Table outlines
  • Table column alignments