Skip to content

Commit

Permalink
V2.0.0 (#66)
Browse files Browse the repository at this point in the history
* Add `html-parser-api` and `html-parser-impl` modules
* Add `HtmlEmptyTagReplacement`
* Implement Appendable and CharSequence in SpannableBuilder
* Renamed library modules to reflect maven artifact names
* Rename `markwon-syntax` to `markwon-syntax-highlight`
* Add HtmlRenderer asbtraction
* Add CssInlineStyleParser
* Fix Theme#listItemColor and OL
* Fix task list block parser to revert parsing state when line is not matching
* Defined test format files
* image-loader add datauri parser
* image-loader add support for inline data uri image references
* Add travis configuration
* Fix image with width greater than canvas scaled
* Fix blockquote span
* Dealing with white spaces at the end of a document
* image-loader add SchemeHandler abstraction
* Add sample-latex-math module
  • Loading branch information
noties authored Sep 17, 2018
1 parent 8ef4a72 commit e0563dc
Show file tree
Hide file tree
Showing 272 changed files with 23,786 additions and 1,896 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@
.DS_Store
/captures
.externalNativeBuild
**/build
**/build
**/dist
**/node_modules
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# https://docs.travis-ci.com/user/languages/android/
language: android
jdk: openjdk8
sudo: false

android:
components:
- tools
- platform-tools
- tools

- build-tools-27.0.3
- android-27

branches:
except:
- gh-pages

cache:
directories:
- $HOME/.m2
206 changes: 87 additions & 119 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,71 +4,72 @@

[![markwon](https://img.shields.io/maven-central/v/ru.noties/markwon.svg?label=markwon)](http://search.maven.org/#search|ga|1|g%3A%22ru.noties%22%20AND%20a%3A%22markwon%22)
[![markwon-image-loader](https://img.shields.io/maven-central/v/ru.noties/markwon-image-loader.svg?label=markwon-image-loader)](http://search.maven.org/#search|ga|1|g%3A%22ru.noties%22%20AND%20a%3A%22markwon-image-loader%22)
[![markwon-syntax](https://img.shields.io/maven-central/v/ru.noties/markwon-syntax.svg?label=markwon-syntax)](http://search.maven.org/#search|ga|1|g%3A%22ru.noties%22%20AND%20a%3A%22markwon-syntax%22)
[![markwon-syntax-highlight](https://img.shields.io/maven-central/v/ru.noties/markwon-syntax-highlight.svg?label=markwon-syntax-highlight)](http://search.maven.org/#search|ga|1|g%3A%22ru.noties%22%20AND%20a%3A%22markwon-syntax-highlight%22)
[![markwon-view](https://img.shields.io/maven-central/v/ru.noties/markwon-view.svg?label=markwon-view)](http://search.maven.org/#search|ga|1|g%3A%22ru.noties%22%20AND%20a%3A%22markwon-view%22)

**Markwon** is a library for Android that renders markdown as system-native Spannables. It gives ability to display markdown in all TextView widgets (**TextView**, **Button**, **Switch**, **CheckBox**, etc), **Notifications**, **Toasts**, etc. <u>**No WebView is required**</u>. Library provides reasonable defaults for display style of markdown but also gives all the means to tweak the appearance if desired. All markdown features are supported (including limited support for inlined HTML code, markdown tables and images).

<sup>*</sup>*This file is displayed by default in the [sample-apk] application. Which is a generic markdown viewer with support to display markdown via `http`, `https` & `file` schemes and 2 themes included: Light & Dark*

## Installation
```groovy
implementation 'ru.noties:markwon:1.1.0'
implementation 'ru.noties:markwon-image-loader:1.1.0' // optional
implementation 'ru.noties:markwon-syntax:1.1.0' // optional
implementation 'ru.noties:markwon-view:1.1.0' // optional
```

### Snapshot
![markwon-snapshot](https://img.shields.io/nexus/s/https/oss.sonatype.org/ru.noties/markwon.svg?label=markwon)

In order to use latest `SNAPSHOT` version add snapshot repository to your root project's `build.gradle` file:
**Markwon** is a markdown library for Android. It parses markdown
following [commonmark-spec] with the help of amazing [commonmark-java]
library and renders result as _Android-native_ Spannables. **No HTML**
is involved as an intermediate step. <u>**No WebView** is required</u>.
It's extremely fast, feature-rich and extensible.

It gives ability to display markdown in all TextView widgets
(**TextView**, **Button**, **Switch**, **CheckBox**, etc), **Toasts**
and all other places that accept **Spanned content**. Library provides
reasonable defaults to display style of a markdown content but also
gives all the means to tweak the appearance if desired. All markdown
features listed in [commonmark-spec] are supported
(including support for **inlined/block HTML code**, **markdown tables**,
**images** and **syntax highlight**).

[commonmark-spec]: https://spec.commonmark.org/0.28/
[commonmark-java]: https://github.com/atlassian/commonmark-java/blob/master/README.md

```groovy
allprojects {
repositories {
jcenter()
google()
maven { url 'https://oss.sonatype.org/content/repositories/snapshots/' }
}
}
```
<sup>*</sup>*This file is displayed by default in the [sample-apk] (`markwon-sample-{latest-version}-debug.apk`) application. Which is a generic markdown viewer with support to display markdown via `http`, `https` & `file` schemes and 2 themes included: Light & Dark*

and then in your module `build.gradle`:
[sample-apk]: https://github.com/noties/Markwon/releases

## Installation
```groovy
implementation 'ru.noties:markwon:1.1.1-SNAPSHOT'
implementation "ru.noties:markwon:${markwonVersion}"
implementation "ru.noties:markwon-image-loader:${markwonVersion}" // optional
implementation "ru.noties:markwon-syntax-highlight:${markwonVersion}" // optional
implementation "ru.noties:markwon-view:${markwonVersion}" // optional
```

Please note that `markwon-image-loader`, `markwon-syntax` and `markwon-view` are also present in `SNAPSHOT` repository and share the same version as main `markwon` artifact.
Please visit [documentation] web-site for further reference

## Supported markdown features:
* Emphasis (`*`, `_`)
* Strong emphasis (`**`, `__`)
* Strike-through (`~~`)
* Headers (`#{1,6}`)
* Links (`[]()` && `[][]`)
* Images (_requires special handling_)
* Images
* Thematic break (`---`, `***`, `___`)
* Quotes & nested quotes (`>{1,}`)
* Ordered & non-ordered lists & nested ones
* Inline code
* Code blocks
* Tables (*with limitations*)
* Small subset of inline-html (which is rendered by this library):
* * Emphasis (`<i>`, `<em>`, `<cite>`, `<dfn>`)
* * Strong emphasis (`<b>`, `<strong>`)
* * SuperScript (`<sup>`)
* * SubScript (`<sub>`)
* * Underline (`<u>`)
* * Strike-through (`<s>`, `<strike>`, `<del>`)
* other inline html is rendered via (`Html.fromHtml(...)`)
* Syntax highlight
* HTML
* Emphasis (`<i>`, `<em>`, `<cite>`, `<dfn>`)
* Strong emphasis (`<b>`, `<strong>`)
* SuperScript (`<sup>`)
* SubScript (`<sub>`)
* Underline (`<u>`, `ins`)
* Strike-through (`<s>`, `<strike>`, `<del>`)
* Link (`a`)
* Lists (`ul`, `ol`)
* Images (`img` will require configured image loader)
* Blockquote (`blockquote`)
* Heading (`h1`, `h2`, `h3`, `h4`, `h5`, `h6`)
* there is support to render any HTML tag
* Task lists:

- [ ] Not _done_
- [X] **Done** with `X`
- [x] ~~and~~ **or** small `x`

---

## Screenshots
Expand All @@ -84,72 +85,11 @@ By default configuration uses TextView textColor for styling, so changing textCo

---

## Quick start
This is the most simple way to set markdown to a TextView or any of its siblings:
```java
Markwon.setMarkdown(textView, markdown);
```

It's just a helper method, that does underneath:
* constructs a `Parser` (see: [commonmark-java][commonmark-java]) and parses markdown
* constructs a `SpannableConfiguration`
* *renders* parsed markdown to Spannable (via `SpannableRenderer`)
* prepares TextView to display images, tables and links
* sets text

This flow answers the most simple usage of displaying markdown: one shot parsing & configuration of relatively small markdown chunks. If your markdown contains a lot of text or you plan to display multiple UI widgets with markdown you might consider *stepping in* and taking control of this flow.

The candidate requirements to *step in*:
* parsing and processing of parsed markdown in background thread
* reusing `Parser` and/or `SpannableConfiguration` between multiple calls
* ignore images and tables specific logic (you know that markdown won't contain them)

So, if we expand `Markwon.setMarkdown(textView, markdown)` method we will see the following:
```java
// create a Parser instance (can be done manually)
// internally creates default Parser instance & registers `strike-through` & `tables` extension
final Parser parser = Markwon.createParser();

// core class to display markdown, can be obtained via this method,
// which creates default instance (no images handling though),
// or via `builder` method, which lets you to configure this instance
//
// `this` refers to a Context instance
final SpannableConfiguration configuration = SpannableConfiguration.create(this);
## Documentation

// it's better **not** to re-use this class between multiple calls
final SpannableRenderer renderer = new SpannableRenderer();
Please visit [documentation] web-site for reference

final Node node = parser.parse(markdown);
final CharSequence text = renderer.render(configuration, node);

// for links in markdown to be clickable
textView.setMovementMethod(LinkMovementMethod.getInstance());

// we need these due to the limited nature of Spannables to invalidate TextView
Markwon.unscheduleDrawables(textView);
Markwon.unscheduleTableRows(textView);

textView.setText(text);

Markwon.scheduleDrawables(textView);
Markwon.scheduleTableRows(textView);
```

Please note that if you are having trouble with `LinkMovementMethod` you can use
`Markwon.setText(textView, markdown, movementMethod)` method (`@since 1.0.6`) to specify _no_ movement
method (aka `null`) or own implementation. As an alternative to the system `LinkMovementMethod`
you can use [Better-Link-Movement-Method][better-link-movement-method].

Please refer to [SpannableConfiguration] document for more info

## Syntax highlight

Starting with version `1.1.0` there is an artifact (`markwon-syntax`) that allows you to have syntax highlight functionality.
It is based on [Prism4j](https://github.com/noties/Prism4j) project. It contains 2 builtin themes:
`Default` (light, `Prism4jThemeDefault`) and `Darkula` (dark, `Prism4jThemeDarkula`).

[library-syntax](./library-syntax/)
[documentation]: https://noties.github.io/Markwon

---

Expand Down Expand Up @@ -218,8 +158,6 @@ Or leave it empty and use the [link text itself].

Inline `code` has `back-ticks around` it.

<sup>*</sup>*Please note, that syntax highlighting is supported but library provides no means to do it automatically*

```javascript
var s = "JavaScript syntax highlighting";
alert(s);
Expand All @@ -230,6 +168,46 @@ s = "Python syntax highlighting"
print s
```

```java
/**
* Helper method to obtain a Parser with registered strike-through &amp; table extensions
* &amp; task lists (added in 1.0.1)
*
* @return a Parser instance that is supported by this library
* @since 1.0.0
*/
@NonNull
public static Parser createParser() {
return new Parser.Builder()
.extensions(Arrays.asList(
StrikethroughExtension.create(),
TablesExtension.create(),
TaskListExtension.create()
))
.build();
}
```

```xml
<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?android:attr/actionBarSize">

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dip"
android:lineSpacingExtra="2dip"
android:textSize="16sp"
tools:context="ru.noties.markwon.MainActivity"
tools:text="yo\nman" />

</ScrollView>
```

```
No language indicated, so no syntax highlighting.
But let's throw in a <b>tag</b>.
Expand Down Expand Up @@ -275,16 +253,10 @@ Nested quotes

## Inline HTML

<sup>*</sup>*As Android doesn't support HTML out of box, **Markwon** library supports only a small subset of it. Everything else is rendered via `Html.fromHtml()`*

* Emphasis (`<i>`, `<em>`, `<cite>`, `<dfn>`)
* Strong emphasis (`<b>`, `<strong>`)
* SuperScript (`<sup>`)
* SubScript (`<sub>`)
* Underline (`<u>`)
* Strike-through (`<s>`, `<strike>`, `<del>`)
```html
<u><i>H<sup>T<sub>M</sub></sup><b><s>L</s></b></i></u>
```

Let's use it:
<u><i>H<sup>T<sub>M</sub></sup><b><s>L</s></b></i></u>

---
Expand Down Expand Up @@ -330,11 +302,7 @@ Underscores (`_`)
limitations under the License.
```

[sample-apk]: https://github.com/noties/Markwon/releases/download/v1.0.0/markwon-sample-1.0.0.apk
[commonmark-java]: https://github.com/atlassian/commonmark-java/blob/master/README.md
[cheatsheet]: https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet
[SpannableConfiguration]: ./docs/SpannableConfiguration.md
[better-link-movement-method]: https://github.com/saket/Better-Link-Movement-Method

[arbitrary case-insensitive reference text]: https://www.mozilla.org
[1]: http://slashdot.org
Expand Down
34 changes: 18 additions & 16 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ apply plugin: 'com.android.application'

android {

compileSdkVersion TARGET_SDK
buildToolsVersion BUILD_TOOLS
compileSdkVersion config['compile-sdk']
buildToolsVersion config['build-tools']

defaultConfig {
applicationId "ru.noties.markwon"
minSdkVersion MIN_SDK
targetSdkVersion TARGET_SDK
minSdkVersion config['min-sdk']
targetSdkVersion config['target-sdk']
versionCode 1
versionName version
setProperty("archivesBaseName", "markwon-sample-$versionName")
Expand All @@ -28,18 +28,20 @@ android {

dependencies {

implementation project(':library')
implementation project(':library-image-loader')
implementation project(':library-syntax')
implementation project(':markwon')
implementation project(':markwon-image-loader')
implementation project(':markwon-syntax-highlight')

implementation 'ru.noties:debug:3.0.0@jar'
implementation 'me.saket:better-link-movement-method:2.2.0'

implementation OK_HTTP

implementation 'com.google.dagger:dagger:2.10'
annotationProcessor 'com.google.dagger:dagger-compiler:2.10'
deps.with {
implementation it['okhttp']
implementation it['prism4j']
implementation it['debug']
implementation it['better-link-movement']
implementation it['dagger']
}

implementation PRISM_4J
annotationProcessor PRISM_4J_BUNDLER
deps['annotationProcessor'].with {
annotationProcessor it['prism4j-bundler']
annotationProcessor it['dagger-compiler']
}
}
5 changes: 4 additions & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dip"
android:clipToPadding="false"
android:clipChildren="false"
android:scrollbarStyle="outsideOverlay"
android:layout_marginTop="?android:attr/actionBarSize">

<TextView
android:id="@+id/text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dip"
android:lineSpacingExtra="2dip"
android:textSize="16sp"
tools:context="ru.noties.markwon.MainActivity"
Expand Down
Loading

0 comments on commit e0563dc

Please sign in to comment.