diff --git a/CHANGELOG.md b/CHANGELOG.md index d4d3d31..7dedd38 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# next version + +- Improvement: Support more Android styling tags (#278) + # 1.0.5 (2019-02-24) - Bugfix: Incorrect language detection when reading localization files (#251) diff --git a/README.md b/README.md index c93588e..814b7e3 100644 --- a/README.md +++ b/README.md @@ -80,7 +80,7 @@ Twine currently supports the following output formats: * [Android String Resources][androidstrings] (format: android) * HTML tags will be escaped by replacing `<` with `<` * Tags inside ``, ``, `` and `` links. + * Supports [basic styling][androidstyling] according to [Android documentation](https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithHTML). All of the documented tags are supported, in addition to `` links. * These tags will *not* be escaped if the string doesn't contain placeholders. You can reference them directly in your layouts or by using [`getText()`](https://developer.android.com/reference/android/content/res/Resources.html#getText(int)) to read them programatically. * These tags *will* be escaped if the string contains placeholders. You can use [`getString()`](https://developer.android.com/reference/android/content/res/Resources.html#getString(int,%20java.lang.Object...)) combined with [`fromHtml`](https://developer.android.com/reference/android/text/Html.html#fromHtml(java.lang.String)) as shown in the [documentation][androidstyling] to display them. * See [\#212](https://github.com/scelis/twine/issues/212) for details. diff --git a/lib/twine/formatters/android.rb b/lib/twine/formatters/android.rb index f87d469..1e394e3 100644 --- a/lib/twine/formatters/android.rb +++ b/lib/twine/formatters/android.rb @@ -113,22 +113,26 @@ module Twine # http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling def escape_value(value) - inside_cdata = /<\!\[CDATA\[((?!\]\]>).)*$/ # opening CDATA tag ('') - inside_opening_anchor_tag = /).)*$/ # anchor tag start ('' + inside_cdata = /<\!\[CDATA\[((?!\]\]>).)*$/ # opening CDATA tag ('') + inside_opening_tag = /<(a|font|span|p)\s?((?!>).)*$/ # tag start ('' # escape double and single quotes and & signs - value = gsub_unless(value, '"', '\\"') { |substring| substring =~ inside_cdata || substring =~ inside_opening_anchor_tag } + value = gsub_unless(value, '"', '\\"') { |substring| substring =~ inside_cdata || substring =~ inside_opening_tag } value = gsub_unless(value, "'", "\\'") { |substring| substring =~ inside_cdata } - value = gsub_unless(value, /&/, '&') { |substring| substring =~ inside_cdata || substring =~ inside_opening_anchor_tag } + value = gsub_unless(value, /&/, '&') { |substring| substring =~ inside_cdata || substring =~ inside_opening_tag } # if `value` contains a placeholder, escape all angle brackets # if not, escape opening angle brackes unless it's a supported styling tag # https://github.com/scelis/twine/issues/212 # https://stackoverflow.com/questions/3235131/#18199543 if number_of_twine_placeholders(value) > 0 - angle_bracket = /<(?!(\/?(\!\[CDATA)))/ # matches all `<` but , , , and , , , , , , , , , , + # , , , , ,