Skip to content

Commit 1ce0765

Browse files
authored
Add unescapeHtmlTags flag and bump dependencies (#53)
1 parent 380cbca commit 1ce0765

File tree

15 files changed

+229
-61
lines changed

15 files changed

+229
-61
lines changed

.idea/compiler.xml

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CHANGELOG.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,34 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2222

2323
## [Unreleased]
2424
### Added
25-
- No new features!
25+
- Add new `unescapeHtmlTags` flag to enable or disable HTML unescaping from strings.
26+
27+
<details open><summary>Groovy</summary>
28+
29+
```groovy
30+
poEditor {
31+
apiToken = "your_api_token"
32+
projectId = 12345
33+
defaultLang = "en"
34+
unescapeHtmlTags = false
35+
}
36+
```
37+
38+
</details>
39+
40+
<details><summary>Kotlin</summary>
41+
42+
```kotlin
43+
poEditor {
44+
apiToken = "your_api_token"
45+
projectId = 12345
46+
defaultLang = "en"
47+
unescapeHtmlTags = false
48+
}
49+
```
50+
51+
</details>
52+
2653
### Changed
2754
- No changed features!
2855
### Deprecated

README.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ Attribute | Description
9393
```resFileName``` | (Since 3.1.0) (Optional) Sets the file name for the imported string resource XML files. Defaults to `strings`.
9494
```order``` | (Since 3.1.0) (Optional) Defines how to order the export. Accepted values are defined by the POEditor API.
9595
```unquoted``` | (Since 3.2.0) (Optional) Defines if the strings should be unquoted, overriding default PoEditor configuration. Defaults to `false`.
96+
```unescapeHtmlTags``` | (Since 3.4.0) (Optional) Whether or not to unescape HTML entitites from strings. Defaults to true.
9697

9798
After the configuration is done, just run the new ```importPoEditorStrings``` task via Android Studio or command line:
9899

@@ -518,6 +519,37 @@ poEditor {
518519

519520
</details>
520521

522+
## Toggling HTML unescaping
523+
> Requires version 3.4.0 of the plug-in
524+
525+
You can enable or disable HTML tags unescaping with the `unescapeHtmlTags` flag.
526+
527+
<details open><summary>Groovy</summary>
528+
529+
```groovy
530+
poEditor {
531+
apiToken = "your_api_token"
532+
projectId = 12345
533+
defaultLang = "en"
534+
unescapeHtmlTags = false
535+
}
536+
```
537+
538+
</details>
539+
540+
<details><summary>Kotlin</summary>
541+
542+
```kotlin
543+
poEditor {
544+
apiToken = "your_api_token"
545+
projectId = 12345
546+
defaultLang = "en"
547+
unescapeHtmlTags = false
548+
}
549+
```
550+
551+
</details>
552+
521553

522554
## iOS alternative
523555
If you want a similar solution for your iOS projects, check this out: [poeditor-parser-swift](https://github.com/hyperdevs-team/poeditor-parser-swift)

build.gradle.kts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,21 @@ publishing {
135135
}
136136
}
137137

138+
organization {
139+
name.set("HyperDevs")
140+
url.set("https://github.com/hyperdevs-team")
141+
}
142+
143+
issueManagement {
144+
system.set("GitHub Issues")
145+
url.set("https://github.com/hyperdevs-team/poeditor-android-gradle-plugin/issues")
146+
}
147+
148+
scm {
149+
connection.set("git@github.com:hyperdevs-team/poeditor-android-gradle-plugin.git")
150+
url.set("https://github.com/hyperdevs-team/poeditor-android-gradle-plugin.git")
151+
}
152+
138153
developers {
139154
developer {
140155
name.set("Iván Martínez")
@@ -148,12 +163,6 @@ publishing {
148163
id.set("adriangl")
149164
url.set("https://github.com/adriangl")
150165
roles.set(listOf("Maintainer"))
151-
152-
organization {
153-
name.set("HyperDevs")
154-
id.set("hyperdevs-team")
155-
url.set("https://github.com/hyperdevs-team")
156-
}
157166
}
158167
}
159168
}

gradle/libs.versions.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ detekt = { id = "io.gitlab.arturbosch.detekt", version.ref = "detekt" }
3232
gitVersionGradle = { id = "com.gladed.androidgitversion", version = "0.4.14"}
3333
versionsUpdate = { id = "com.github.ben-manes.versions", version = "0.46.0" }
3434

35-
3635
[bundles]
3736
moshi = ["moshi", "moshi-kotlin", "moshi-adapters"]
3837
retrofit = ["retrofit", "retrofit-converterMoshi"]

src/main/kotlin/com/hyperdevs/poeditor/gradle/Main.kt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ fun main() {
5858
?: emptyMap()
5959
val minimumTranslationPercentage = dotenv.get("MINIMUM_TRANSLATION_PERCENTAGE", "85").toInt()
6060
val unquoted = dotenv.get("UNQUOTED", "false").toBoolean()
61+
val unescapeHtmlTags = dotenv.get("UNESCAPE_HTML_TAGS", "true").toBoolean()
6162

6263
PoEditorStringsImporter.importPoEditorStrings(
6364
apiToken,
@@ -70,6 +71,7 @@ fun main() {
7071
languageValuesOverridePathMap,
7172
minimumTranslationPercentage,
7273
resFileName,
73-
unquoted
74+
unquoted,
75+
unescapeHtmlTags
7476
)
7577
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/PoEditorPlugin.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ class PoEditorPlugin : Plugin<Project> {
6060
minimumTranslationPercentage.convention(-1)
6161
resFileName.convention("strings")
6262
unquoted.convention(false)
63+
unescapeHtmlTags.convention(true)
6364
}
6465

6566
// Add flavor and build-type configurations if the project has the "com.android.application" plugin

src/main/kotlin/com/hyperdevs/poeditor/gradle/PoEditorPluginExtension.kt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,15 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
142142
@get:Input
143143
val unquoted: Property<Boolean> = objects.property(Boolean::class.java)
144144

145+
/**
146+
* Whether or not HTML tags in strings should be unescaped or not.
147+
*
148+
* Defaults to true.
149+
*/
150+
@get:Optional
151+
@get:Input
152+
val unescapeHtmlTags: Property<Boolean> = objects.property(Boolean::class.java)
153+
145154
/**
146155
* Sets the configuration as enabled or not.
147156
*
@@ -261,4 +270,14 @@ open class PoEditorPluginExtension @Inject constructor(objects: ObjectFactory, p
261270
* Gradle Kotlin DSL users must use `unquoted.set(value)`.
262271
*/
263272
fun setUnquoted(value: Boolean) = unquoted.set(value)
273+
274+
/**
275+
* Sets if strings should have HTML tags unescaped.
276+
*
277+
* NOTE: added for Gradle Groovy DSL compatibility. Check the note on
278+
* https://docs.gradle.org/current/userguide/lazy_configuration.html#lazy_properties for more details.
279+
*
280+
* Gradle Kotlin DSL users must use `unescapeHtmlTags.set(value)`.
281+
*/
282+
fun setUnescapeHtmlTags(value: Boolean) = unescapeHtmlTags.set(value)
264283
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/PoEditorStringsImporter.kt

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,15 @@ object PoEditorStringsImporter {
9393
languageValuesOverridePathMap: Map<String, String>,
9494
minimumTranslationPercentage: Int,
9595
resFileName: String,
96-
unquoted: Boolean) {
96+
unquoted: Boolean,
97+
unescapeHtmlTags: Boolean) {
9798
try {
9899
val poEditorApiController = PoEditorApiControllerImpl(apiToken, moshi, poEditorApi)
99100

100101
// Retrieve available languages from PoEditor
101102
logger.lifecycle("Retrieving project languages...")
102103
val projectLanguages = poEditorApiController.getProjectLanguages(projectId)
103-
val skippedLanguages = projectLanguages.filter { it.percentage < minimumTranslationPercentage }
104+
val skippedLanguages = projectLanguages.filter { it.percentage < minimumTranslationPercentage }.toSet()
104105

105106
// Iterate over every available language
106107
logger.lifecycle("Available languages: ${projectLanguages.joinAndFormat { it.code }}")
@@ -141,17 +142,20 @@ object PoEditorStringsImporter {
141142
val translationFile = okHttpClient.downloadUrlToString(translationFileUrl)
142143

143144
// Extract final files from downloaded translation XML
144-
val postProcessedXmlDocumentMap =
145-
xmlPostProcessor.postProcessTranslationXml(
146-
translationFile, listOf(TABLET_REGEX_STRING))
145+
val postProcessedXmlDocumentMap = xmlPostProcessor.postProcessTranslationXml(
146+
translationFile,
147+
listOf(TABLET_REGEX_STRING),
148+
unescapeHtmlTags
149+
)
147150

148151
xmlWriter.saveXml(
149152
resDirPath,
150153
resFileName,
151154
postProcessedXmlDocumentMap,
152155
defaultLang,
153156
languageCode,
154-
languageValuesOverridePathMap
157+
languageValuesOverridePathMap,
158+
unescapeHtmlTags
155159
)
156160
}
157161
} catch (e: Exception) {
@@ -161,6 +165,6 @@ object PoEditorStringsImporter {
161165
}
162166
}
163167

164-
private fun List<ProjectLanguage>.joinAndFormat(transform: ((ProjectLanguage) -> CharSequence)) =
168+
private fun Collection<ProjectLanguage>.joinAndFormat(transform: ((ProjectLanguage) -> CharSequence)) =
165169
joinToString(separator = ", ", prefix = "[", postfix = "]", transform = transform)
166170
}

src/main/kotlin/com/hyperdevs/poeditor/gradle/ktx/DocumentExtensions.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fun String.toStringsXmlDocument(): Document {
4444
/**
4545
* Converts a [Document] into a formatted [String].
4646
*/
47-
fun Document.toAndroidXmlString(): String {
47+
fun Document.toAndroidXmlString(unescapeHtmlTags: kotlin.Boolean): String {
4848
val registry = DOMImplementationRegistry.newInstance()
4949
val impl = registry.getDOMImplementation("LS") as DOMImplementationLS
5050
val output = impl.createLSOutput().apply { encoding = "utf-8" }
@@ -59,5 +59,5 @@ fun Document.toAndroidXmlString(): String {
5959

6060
serializer.write(this, output)
6161

62-
return writer.toString().unescapeHtmlTags()
62+
return writer.toString().let { if (unescapeHtmlTags) it.unescapeHtmlTags() else it }
6363
}

0 commit comments

Comments
 (0)