Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure changelog render is consistent with all line separator #183

Merged
merged 5 commits into from
Jul 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Gradle Changelog Plugin

## [Unreleased]
### Fixed
- Changelog render is inconsistent between different line separators [#182](../../issues/182)

## [2.1.1] - 2023-07-07

### Fixed
- Wrong markdown render of changelog that use CRLF or CR line separator [#176](../../issues/176)
- Wrong markdown render of changelog that use CRLF or CR line separator [#176](../../issues/176)
- `Changelog.Item.plus` copies original item without restoring all original data [#179](../../issues/179)

## [2.1.0] - 2023-06-02
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,11 +427,11 @@ It provides a couple of properties and methods that allow altering the output fo

## Helper Methods

| Name | Description | Returned type |
|----------------------------------------|----------------------------------------------------------------|---------------|
| `date(pattern: String = "yyyy-MM-dd")` | Shorthand for retrieving the current date in the given format. | `String` |
| `markdownToHTML(input: String)` | Converts given Markdown content to HTML output. | `String` |
| `markdownToPlainText(input: String)` | Converts given Markdown content to Plain Text output. | `String` |
| Name | Description | Returned type |
|---------------------------------------------------------------|----------------------------------------------------------------|---------------|
| `date(pattern: String = "yyyy-MM-dd")` | Shorthand for retrieving the current date in the given format. | `String` |
| `markdownToHTML(input: String, lineSeparator: String = "\n")` | Converts given Markdown content to HTML output. | `String` |
| `markdownToPlainText(input: String, lineSeparator: String)` | Converts given Markdown content to Plain Text output. | `String` |

> **Note**
>
Expand Down
7 changes: 3 additions & 4 deletions src/main/kotlin/org/jetbrains/changelog/Changelog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ data class Changelog(
.withEmptySections(isUnreleased)
}
}

val links
get() = sequence {
repositoryUrl?.let {
Expand Down Expand Up @@ -410,10 +411,8 @@ data class Changelog(
.trim()

private fun String.processOutput(outputType: OutputType) = when (outputType) {
OutputType.MARKDOWN -> this

OutputType.HTML -> markdownToHTML(this)

OutputType.MARKDOWN -> this.normalizeLineSeparator(lineSeparator) // Ensure that output content has always the correct line separator
OutputType.HTML -> markdownToHTML(this, lineSeparator)
OutputType.PLAIN_TEXT -> markdownToPlainText(this, lineSeparator)
}

Expand Down
3 changes: 2 additions & 1 deletion src/main/kotlin/org/jetbrains/changelog/ChangelogPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,8 @@ class ChangelogPlugin : Plugin<Project> {
if (!exists()) {
createNewFile()
}
readText().normalizeToLineFeed()
// Normalize text to LF, because markdown library currently full support only this line separator
readText().normalizeLineSeparator("\n")
}
}.get(),
defaultPreTitle = preTitle.orNull,
Expand Down
26 changes: 12 additions & 14 deletions src/main/kotlin/org/jetbrains/changelog/extensions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,27 @@ import java.util.*

fun date(pattern: String = DATE_PATTERN) = SimpleDateFormat(pattern).format(Date())!!

fun markdownToHTML(input: String) = ChangelogFlavourDescriptor().run {
HtmlGenerator(input, MarkdownParser(this).buildMarkdownTreeFromString(input), this, false)
fun markdownToHTML(input: String, lineSeparator: String = "\n") = ChangelogFlavourDescriptor().run {
// Normalize text to LF, because markdown library currently full support only this line separator
val lfString = input.normalizeLineSeparator("\n")
HtmlGenerator(lfString, MarkdownParser(this).buildMarkdownTreeFromString(lfString), this, false)
.generateHtml()
.normalizeLineSeparator(lineSeparator)
}

fun markdownToPlainText(input: String, lineSeparator: String) = PlainTextFlavourDescriptor(lineSeparator).run {
HtmlGenerator(input, MarkdownParser(this).buildMarkdownTreeFromString(input), this, false)
// Normalize text to LF, because markdown library currently full support only this line separator
val lfString = input.normalizeLineSeparator("\n")
HtmlGenerator(lfString, MarkdownParser(this).buildMarkdownTreeFromString(lfString), this, false)
.generateHtml(PlainTextTagRenderer())
}

internal fun String.reformat(lineSeparator: String): String {
val result = listOf(
"""(?:^|$lineSeparator)+(#+ [^$lineSeparator]*)(?:$lineSeparator)*""".toRegex() to "$lineSeparator$lineSeparator$1$lineSeparator",
"""((?:^|$lineSeparator)#+ .*?)$lineSeparator(#+ )""".toRegex() to "$1$lineSeparator$lineSeparator$2",
"""$lineSeparator+(\[.*?]:)""".toRegex() to "$lineSeparator$lineSeparator$1",
"""(?<=$lineSeparator)(\[.*?]:.*?)$lineSeparator+""".toRegex() to "$1$lineSeparator",
"""(?:$lineSeparator)+(\[.*?]:)""".toRegex() to "$lineSeparator$lineSeparator$1",
"""(?<=$lineSeparator)(\[.*?]:.*?)(?:$lineSeparator)+""".toRegex() to "$1$lineSeparator",
"""($lineSeparator){3,}""".toRegex() to "$lineSeparator$lineSeparator",
).fold(this) { acc, (pattern, replacement) ->
acc.replace(pattern, replacement)
Expand All @@ -39,15 +44,8 @@ internal fun String.reformat(lineSeparator: String): String {
}
}

internal fun String.normalizeToLineFeed(): String {
val result = listOf(
"\r\n" to "\n",
"\r" to "\n",
).fold(this) { acc, (pattern, replacement) ->
acc.replace(pattern, replacement)
}

return result
internal fun String.normalizeLineSeparator(lineSeparator: String): String {
return this.replace("\\R".toRegex(), lineSeparator)
}

fun interface ChangelogSectionUrlBuilder {
Expand Down
4 changes: 4 additions & 0 deletions src/test/kotlin/org/jetbrains/changelog/BaseTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,8 @@ open class BaseTest {
protected fun assertHTML(@Language("HTML") expected: String, @Language("HTML") actual: String) {
assertEquals(expected.trim(), actual.trim())
}

protected fun assertText(@Language("TEXT") expected: String, @Language("TEXT") actual: String) {
assertEquals(expected.trim(), actual.trim())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
extension.renderItem(this)
)

assertMarkdown(
assertText(
"""
1.0.0
First release.
Expand Down Expand Up @@ -238,7 +238,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent(),
extension.renderItem(this, Changelog.OutputType.HTML)
)
assertMarkdown(
assertText(
"""
1.0.0
First release.
Expand Down Expand Up @@ -295,7 +295,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
!it.endsWith('x')
}.apply {

with (sections) {
with(sections) {
assertEquals(2, size)
assertTrue(containsKey("Added"))
assertEquals(3, get("Added")?.size)
Expand Down Expand Up @@ -488,7 +488,7 @@ class ChangelogPluginExtensionTest : BaseTest() {
""".trimIndent()

assertNotNull(extension.getUnreleased())
assertEquals(
assertHTML(
"""
<h2>Unreleased</h2>
<ul><li>Foo</li></ul>
Expand Down Expand Up @@ -641,4 +641,232 @@ class ChangelogPluginExtensionTest : BaseTest() {
items.last(),
)
}

@Test
fun `returns change notes for the v1_0_0 version of changelog that use CRLF`() {
changelog =
"""
# Changelog
Project description.
Multiline description:
- item 1
- item 2

## [Unreleased]
Not yet released version.

### Added
- Foo

## [1.0.0]
First release.

### Added
- Test [link](https://www.example.org) test

### Removed
- Bar

[Unreleased]: https://blog.jetbrains.com
[1.0.0]: https://jetbrains.com
""".trimIndent().normalizeLineSeparator("\r\n")

extension.get(version).apply {
assertEquals(project.version, version)

assertMarkdown(
"""
## [1.0.0]
First release.

### Added
- Test [link](https://www.example.org) test

### Removed
- Bar

[1.0.0]: https://jetbrains.com
""".trimIndent().normalizeLineSeparator("\r\n"),
extension.renderItem(this)
)

assertHTML(
"""
<h2><a href="https://jetbrains.com">1.0.0</a></h2>
<p>First release.</p>

<h3>Added</h3>
<ul><li>Test <a href="https://www.example.org">link</a> test</li></ul>

<h3>Removed</h3>
<ul><li>Bar</li></ul>
""".trimIndent().normalizeLineSeparator("\r\n"),
extension.renderItem(this, Changelog.OutputType.HTML)
)

assertText(
"""
1.0.0
First release.

Added
- Test link test

Removed
- Bar
""".trimIndent().normalizeLineSeparator("\r\n"),
extension.renderItem(this, Changelog.OutputType.PLAIN_TEXT)
)
}
}

@Test
@Suppress("LongMethod", "MaxLineLength")
fun `render changelog that use CRLF`() {
changelog =
"""
# Changelog
My project description.

## [1.1.0]
First release.

But a great one.

### Added
- Foo *FOO* foo
- Bar **BAR** bar
- Test [link](https://www.example.org) test
- Code `block` code
- Bravo
- Alpha

### Fixed
- Hello
- World

### Removed
- Hola

## [1.0.0]

### Added
- Foo 1.0.0
- Bar 1.0.0

### Removed
- Removed 1.0.0

[1.1.0]: https://jetbrains.com/1.1.0
[1.0.0]: https://jetbrains.com/1.0.0
""".trimIndent().normalizeLineSeparator("\r\n")

extension.get(version).apply {

assertMarkdown(
"""
# Changelog
My project description.

## [1.1.0]
First release.

But a great one.

### Added
- Foo *FOO* foo
- Bar **BAR** bar
- Test [link](https://www.example.org) test
- Code `block` code
- Bravo
- Alpha

### Fixed
- Hello
- World

### Removed
- Hola

## [1.0.0]

### Added
- Foo 1.0.0
- Bar 1.0.0

### Removed
- Removed 1.0.0

[1.1.0]: https://jetbrains.com/1.1.0
[1.0.0]: https://jetbrains.com/1.0.0
""".trimIndent().normalizeLineSeparator("\r\n"),
extension.render(Changelog.OutputType.MARKDOWN)
)
assertHTML(
"""
<h1>Changelog</h1>
<p>My project description.</p>

<h2><a href="https://jetbrains.com/1.1.0">1.1.0</a></h2>
<p>First release.</p>

<p>But a great one.</p>

<h3>Added</h3>
<ul><li>Foo <em>FOO</em> foo</li><li>Bar <strong>BAR</strong> bar</li><li>Test <a href="https://www.example.org">link</a> test</li><li>Code <code>block</code> code</li><li>Bravo</li><li>Alpha</li></ul>

<h3>Fixed</h3>
<ul><li>Hello</li><li>World</li></ul>

<h3>Removed</h3>
<ul><li>Hola</li></ul>

<h2><a href="https://jetbrains.com/1.0.0">1.0.0</a></h2>

<h3>Added</h3>
<ul><li>Foo 1.0.0</li><li>Bar 1.0.0</li></ul>

<h3>Removed</h3>
<ul><li>Removed 1.0.0</li></ul>
""".trimIndent().normalizeLineSeparator("\r\n"),
extension.render(Changelog.OutputType.HTML)
)
assertText(
"""
Changelog
My project description.

1.1.0
First release.

But a great one.

Added
- Foo FOO foo
- Bar BAR bar
- Test link test
- Code block code
- Bravo
- Alpha

Fixed
- Hello
- World

Removed
- Hola

1.0.0

Added
- Foo 1.0.0
- Bar 1.0.0

Removed
- Removed 1.0.0
""".trimIndent().normalizeLineSeparator("\r\n"),
extension.render(Changelog.OutputType.PLAIN_TEXT)
)
}
}
}
Loading