Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fourlastor/dante
A sane rich text parsing and styling library.
https://github.com/fourlastor/dante
android html parser-library rich-text spannable
Last synced: 19 days ago
JSON representation
A sane rich text parsing and styling library.
- Host: GitHub
- URL: https://github.com/fourlastor/dante
- Owner: fourlastor
- License: mit
- Created: 2017-02-16T20:33:35.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2021-10-24T11:41:53.000Z (about 3 years ago)
- Last Synced: 2024-11-10T18:45:07.952Z (about 1 month ago)
- Topics: android, html, parser-library, rich-text, spannable
- Language: Java
- Homepage:
- Size: 2.06 MB
- Stars: 456
- Watchers: 13
- Forks: 33
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-github-android-ui - dante - 文本解析器 (TextView)
README
# Dante
Dante is a text parser to easily generate a `Spannable` from a raw input, right now it supports only HTML but the idea is to be able to support multiple input types (e.g., MarkDown).
Originally I authored Dante in [Pause](http://pause.fm), the project is now dead and I decided to open source it.
This is the very first release of Dante, the api will (most likely) change for the better with successive iterations.
## Installation
Simply add the following to your `build.gradle`:
```
repositories {
jcenter()
}dependencies {
compile 'com.fourlastor:dante:1.0.1'
}
```## Usage with HTML
```java
FlavoredHtml flavoredHtml = new FlavoredHtml.Builder(context)
.newLine("p", "h1", "h2", "h3", "h4", "h5", "h6", "li")
.textAppearance(R.style.headline, "h1")
.textAppearance(R.style.title, "h2")
.textAppearance(R.style.subhead, "h3")
.textAppearance(R.style.body, "p", "li")
.style(Typeface.BOLD, "b", "strong")
.style(Typeface.ITALIC, "i", "em")
.bullet(15, "li")
.build();Spanned styledHtml = flavoredHtml.parse(htmlString);
```Keep in mind that this will be executed on the same thread on which `flavoredHtml.parse()` is executed. Since loading images might entail executing network requests, make sure you invoke `flavoredHtml.parse()` outside of the main thread if you are also loading images.
## Loading images
You'll have to implement either `ImgLoader` to load images, if you need to load bitmaps (e.g., from the network) you can use `ImgLoader.BitmapLoader`:
```java
FlavoredHtml flavoredHtml = new FlavoredHtml.Builder(context)
.img(new ImgListener.BitmapImgGetter(getResources()) {
@Override
protected Bitmap getBitmap(String src) {
try {
return Picasso.with(MainActivity.this)
.load(src)
.get();
} catch (IOException e) {
throw new RuntimeException("Whoops!");
}
}
});
```Keep in mind that this will be executed in the same thread in which `flavoredHtml.parse()` is executed, if you wish to execute network requests, do so invoking `flavoredHtml.parse()` outside of the main thread.
## Different input types
As long as you implement `Parser`, it shouldn't be hard to support a different text style, see `HtmlParser`'s implementation as a reference.