Coder Social home page Coder Social logo

commonsguy / cwac-anddown Goto Github PK

View Code? Open in Web Editor NEW
237.0 14.0 40.0 176 KB

CWAC AndDown: Markdown Utility Library

Home Page: http://commonsware.com/cwac

License: Apache License 2.0

C 96.78% Java 2.31% CMake 0.91%
android android-library markdown markdown-converter

cwac-anddown's Introduction

CWAC AndDown: Markdown. Android. Perfect Together.

Markdown is a popular markup language for wikitext and the like. While there are Java-based Markdown processors available, these are likely to be slow on Android, where they run at all.

This project offers an Android-compliant wrapper around hoedown, a C-based Markdown parser and HTML generator. By using the NDK with this C library, parsing speeds are nice and fast -- 5K of input can be converted into HTML in about 1 millisecond.

If your objective is to put the results of Markdown in a TextView, or something else in Android that understands Spanned objects, you are perhaps better served using Markwon. That project goes straight from Markdown to a Spanned, bypassing HTML.

If you would prefer a Markdown->HTML converter that does not involve native code, consider commonmark-java.

Installation

Option #1: Clone the repository and add the anddown/ subdirectory as an Android library project to your application. You will also need to install the NDK and run ndk-build from the project root directory, in order to build the .so file.

Option #2: Use the AAR artifact for use with Gradle. To use that, add the following blocks to your build.gradle file:

repositories {
    maven {
        url "https://s3.amazonaws.com/repo.commonsware.com"
    }
}

dependencies {
    compile 'com.commonsware.cwac:anddown:0.4.0'
}

Or, if you cannot use SSL, use http://repo.commonsware.com for the repository URL.

The AAR artifact contains compiled binaries for all CPU architectures, so you do not need the NDK to use the library. You will need it if you want to build the library from source code.

Usage

Create an instance of com.commonsware.cwac.anddown.AndDown, then call markdownToHtml() on it, supplying a String containing your Markdown source. This method returns a String containing HTML generated from that source.

AndDown andDown=new AndDown();

String result=andDown.markdownToHtml("Hello, world");

There is also a three-parameter version of markdownToHtml(), taking a pair of int values after the Markdown. The first represents a bitfield of "extensions" that hoedown offers; the second represents a bitfield of "flags" to the HTML generator in hoedown.

AndDown andDown=new AndDown();

String result=andDown.markdownToHtml("This \"contains\" a quote", AndDown.HOEDOWN_EXT_QUOTE, 0);

The AndDown source shows the available values for the extensions and flags. In terms of what they mean... hoedown does not really document them. YMMV.

And, that's pretty much it, at least at this time.

There is no statefulness in the AndDown object. It should be reusable without having to create a fresh instance each time. It might even be thread-safe, though this has not been tested.

Upgrading

Upgrading to v0.3.0 from earlier versions should work without changes, except that the minSdkVersion of the library is now 8 instead of 7. If you are still supporting API Level 7, you are a saint, insane, or possibly both.

Limitations

The Markdown spec says that Markdown converted to HTML should use <em> and <strong> tags. On most browsers, those map to italics and boldface, respectively. However, the Html.fromHtml() method in Android that creates a SpannedString from HTML source flips those, so what you might be used to seeing in boldface turns into italics and vice-versa. This should only be an issue if you are displaing the Markdown-generated HTML in a TextViewWebView in particular should behave more normally.

Also, while hoedown is very fast, using the resulting HTML will inevitably be slower. The same 5K sample file that hoedown processes in about 1 millisecond takes a 100+ milliseconds to convert into a SpannedString via Html.fromHtml().

The author of this project is a complete klutz when it comes to C/JNI/NDK development. You have been warned.

Dependencies

This project has no dependencies. This repository includes a copy of the relevant files from the hoedown project.

This project should work on API Level 8 and higher — please report bugs if you find otherwise.

Version

The latest version of this library is v0.4.0. Um, yay?

Demo

In the demo/ sub-project you will find a sample activity that demonstrates the use of AndDown, loading the hoedown README out of a raw resource and displaying it in a TextView wrapped in a ScrollView.

Additional Documentation

JavaDocs are available, though since the API is pretty limited, there is little information to be found there.

The Busy Coder's Guide to Android Development contains a chapter on the NDK that includes coverage of this library.

License

The code in this project is licensed under the Apache Software License 2.0, per the terms of the included LICENSE file.

The hoedown source code is available under its own license, which appears to be a modified BSD license.

Questions

If you have questions regarding the use of this code, please post a question on StackOverflow tagged with commonsware-cwac and android after searching to see if there already is an answer. Be sure to indicate what CWAC module you are having issues with, and be sure to include source code and stack traces if you are encountering crashes.

You are also welcome to join the CommonsWare Community and post questions and ideas to the CWAC category.

If you have encountered what is clearly a bug, please post an issue. Be certain to include complete steps for reproducing the issue. The contribution guidelines provide some suggestions for how to create a bug report that will get the problem fixed the fastest.

Do not ask for help via social media.

Release Notes

  • v0.4.0: updated to CMake-based build
  • v0.3.0: reorganized code to Android Studio-standard structure, added stub test suite, exposed hoedown's "extensions" and "flags"
  • v0.2.4: added 64-bit x86 and ARM support
  • v0.2.3: better Android.mk, updated to hoedown 3.0.1
  • v0.2.2: updated for Android Studio 1.0 and new AAR publishing system
  • v0.2.1: updated Gradle, fixed manifest for merging
  • v0.2.0: migrated to hoedown and Gradle
  • v0.1.1: added Application.mk to support both x86 and ARM
  • v0.1.0: initial release

Who Made This?

CommonsWare

cwac-anddown's People

Contributors

commonsguy avatar iptux avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cwac-anddown's Issues

Links to AAR Artifact Broken

Mark - thanks for a great library! I'm trying to resuscitate a project of mine that utilizes cwac-anddown.

Trying to get rid of my cwac-anddown submodule and use the AAR artifact. I've tried with both http/https to no avail.

Is the information in the Readme still up to date?

Math Equation

Hi,

Please, does cwac-anddown take in account Mathematic equation ?

No list created

I have the following markdown: "- item1 \n- item2" or "* item1 \n* item2"
It should be converted to:

  • item1
  • item2

but the text remains unchanged

maven repository not found

I checked your two repositories (aws s3 & repo.commonsware.com) in IntelliJ, both failed to fetch the data. would you please check your repo?

Apostrophe converted to &#39;

Hello, not sure if I am doing something wrong but I am being sent a string that I need to convert to a list, it has \n characters dividing the list items, and that works fine. The problem I have is with ' character. In string it is passed as ' and is converted to &#39; in text.

Release build[R8 enabled] in Android Studio 3.5.1 crashes: UnsatisfiableLinkError

Crashstack

java.lang.UnsatisfiedLinkError: No implementation found for java.lang.String c.c.a.a.a.a(java.lang.String, int, int) (tried Java_c_c_a_a_a_a and Java_c_c_a_a_a_a__Ljava_lang_String_2II)
at c.c.a.a.a.a(Native Method)
at .DetailActivity.onCreate(:1)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

Nested quotes, new lines behavior

Hello,

I tried to convert a java string:

>**Text1**\n>Text2\n

and got as a result:

<blockquote>\n<p><strong>Text1</strong>\nText2</p>\n</blockquote>\n

whereas I would have expected something like:

<blockquote><p><strong>Text1</strong><br/>Text2</p></blockquote>

or:

<blockquote>\n<p><strong>Text1</strong><br/>\nText2</p>\n</blockquote>\n

The problem here concerns both "\n" and "<br/>". Is this the expected behavior ?
Thanks in advance.

Strikethrough markdown not parsing.

Hello. trying to parse text like "~~Random text~~" and it's not converting it to Html <s> or <strike> - so I cannot get "Random text".

Using it like:
AndDown andDown = new AndDown()
andDown.markdownToHtml("~~random text~~")

Unfortunately didn't find any documentation about it.

Instant run incompatible with cwac-anddown

When running the development version of the Fidesmo app and trying to access the cwac-anddown library I get a IllegalStateException. This only happens when running the app with Instant Run. Turning off Instant Run and running the app once again makes everything work properly. Without heavily looking into this I think the issue is similar to the following two issues with the library Realm: https://code.google.com/p/android/issues/detail?id=200052 & https://code.google.com/p/android/issues/detail?id=200635
The project Realm also has an issue for this: realm/realm-java#1421 (comment)

I don't expect this issue to be solved by cwac-anddown but rather as the Google developers say "will be fixed in beta7 [of Android Studio 2.0]". I think this issue was annoying enough to track down that it has some value of being open here until it is resolved by Google. In the meantime the suggestion is to turn off Instant Run in the Android Studio settings under "Build, Execution, Deployment" / "Instant Run". If you don't see the value of this issue being here until their release feel free to close it.

My stacktrace:

03-03 12:01:42.711 18364-18364/com.fidesmo.sec.android E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.fidesmo.sec.android, PID: 18364
                                                                         java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker thread.
                                                                             at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:62)
                                                                             at android.os.Handler.handleCallback(Handler.java:739)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                          Caused by: java.lang.UnsatisfiedLinkError: com.android.tools.fd.runtime.IncrementalClassLoader$DelegateClassLoader[DexPathList[[dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_9_f54138b24e596d9c38350def0520b82aa36feb19-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_8_b42c140b2a5995d44b8262bd423ece168f6c3134-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_7_572e625d9a85faa36fed4eccf76e9752adc4d347-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_6_988230e88edba42d7137499bcd365a70c170be3e-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_5_7caf7c41eccdb46706b4c00fa31f68abf77444e9-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_4_a6154ab258ab97d11fc704c7c4d3c64311cdd8f5-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_3_2216dfe8669a4d17c40bb753c715d062c502e099-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_2_49e629e75468a4bc7ce243a5071a1cbafa525436-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_1_ddf39f4d040f93e5ba5a733bf8b83c76ef140949-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-slice_0_7dd766466e57ec26e459338b8492dfc4984d1d97-classes.dex", dex file "/data/data/com.fidesmo.sec.android/files/instant-run/dex/slice-dependencies_0d527e6889c9ce4c5d28e076c32136d293e7759f-classes.dex"],nativeLibraryDirectories=[/data/data/com.fidesmo.sec.android/lib, /vendor/lib64, /system/lib64]]] couldn't find "libanddown.so"
                                                                             at java.lang.Runtime.loadLibrary(Runtime.java:367)
                                                                             at java.lang.System.loadLibrary(System.java:1076)
                                                                             at com.commonsware.cwac.anddown.AndDown.<clinit>(AndDown.java:5)
                                                                             at com.fidesmo.sec.android.app_store.AppActivity.setApplicationDescription(AppActivity.java:311)
                                                                             at com.fidesmo.sec.android.app_store.AppActivity$2.call(AppActivity.java:271)
                                                                             at com.fidesmo.sec.android.app_store.AppActivity$2.call(AppActivity.java:263)
                                                                             at rx.Observable$28.onNext(Observable.java:7976)
                                                                             at rx.observers.SafeSubscriber.onNext(SafeSubscriber.java:139)
                                                                             at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber.pollQueue(OperatorObserveOn.java:202)
                                                                             at rx.internal.operators.OperatorObserveOn$ObserveOnSubscriber$2.call(OperatorObserveOn.java:162)
                                                                             at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
                                                                             at android.os.Handler.handleCallback(Handler.java:739) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

Android device: Huawei Nexus 6P running Android 6.0.1. Running stock, not rooted or unlocked.
Development environment: Surface Pro 3 running Linux Mint 17.3 and Android Studio 2.0 Beta 6

Consider 64bit release

In nexus9 it failed to load the library.
Got this error:
/vendor/lib64, /system/lib64 couldn't find "libanddown.so"

Please consider providing 64bit release of this library.

Horizontal rules not supported?

Noticing that horizontal rules are not supported when markdown text is converted to html?

I have markdown that contains rules denoted by "---", and these should translate to solid line separations for sections etc, but when converted to HTML it looks like they get ignored/removed

I'm using
AndDown andDown = new AndDown();
String result = andDown.markdownToHtml(markdownString);

But not getting any horizontal rules showing. Any help?

supports lists?

cwac-anddown supports lists?

For example:

  1. First ordered list item.
  2. Another item.
  3. And another item.
  • Unordered list can use asterisks
  • Or minuses
  • Or pluses

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.