From 46b33710df5418fd3b668ea0753a8bd72f63c406 Mon Sep 17 00:00:00 2001 From: Mike Sinkovsky Date: Sat, 26 Jun 2021 01:29:17 +0500 Subject: Cleanup paragraphs and tables in GFM renderer (#1946) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * GFM renderer: cleanup paragraphs * GFM renderer: cleanup tables * GFM renderer: add tests for wrong header in table Table with extra cell in row is really generated by `all-modules-page` plugin * Remove commented-out lines * Add BriefCommentPreprocessor which inserts a line break between a signature and a brief comment Fixed a bug with `mapTransform` function which replaces table headers with their contents Co-authored-by: Kamil Doległo --- core/src/main/kotlin/pages/utils.kt | 46 +++++++++++++++++++++++++++++-------- 1 file changed, 37 insertions(+), 9 deletions(-) (limited to 'core/src') diff --git a/core/src/main/kotlin/pages/utils.kt b/core/src/main/kotlin/pages/utils.kt index c9039416..4afc0a26 100644 --- a/core/src/main/kotlin/pages/utils.kt +++ b/core/src/main/kotlin/pages/utils.kt @@ -5,6 +5,9 @@ import kotlin.reflect.KClass inline fun R.mapTransform(noinline operation: (T) -> T): R = mapTransform(T::class, operation) +inline fun R.recursiveMapTransform(noinline operation: (T) -> T): R = + recursiveMapTransform(T::class, operation) + @PublishedApi @Suppress("UNCHECKED_CAST") internal fun R.mapTransform(type: KClass, operation: (T) -> T): R { @@ -12,20 +15,45 @@ internal fun R.mapTransform(type: KClass, return operation(this as T) as R } val new = when (this) { - is ContentGroup -> this.copy(children.map { it.mapTransform(type, operation) }) - is ContentHeader -> this.copy(children.map { it.mapTransform(type, operation) }) - is ContentCodeBlock -> this.copy(children.map { it.mapTransform(type, operation) }) - is ContentCodeInline -> this.copy(children.map { it.mapTransform(type, operation) }) - is ContentTable -> this.copy(children.map { it.mapTransform(type, operation) }) - is ContentList -> this.copy(children.map { it.mapTransform(type, operation) }) - is ContentDivergentGroup -> this.copy(children.map { it.mapTransform(type, operation) }) - is ContentDivergentInstance -> this.copy( + is ContentGroup -> copy(children = children.map { it.mapTransform(type, operation) }) + is ContentHeader -> copy(children = children.map { it.mapTransform(type, operation) }) + is ContentCodeBlock -> copy(children = children.map { it.mapTransform(type, operation) }) + is ContentCodeInline -> copy(children = children.map { it.mapTransform(type, operation) }) + is ContentTable -> copy(header = header.map { it.recursiveMapTransform(type, operation) }, children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentList -> copy(children = children.map { it.mapTransform(type, operation) }) + is ContentDivergentGroup -> copy(children = children.map { it.mapTransform(type, operation) }) + is ContentDivergentInstance -> copy( before = before?.mapTransform(type, operation), divergent = divergent.mapTransform(type, operation), after = after?.mapTransform(type, operation) ) - is PlatformHintedContent -> this.copy(inner.mapTransform(type, operation)) + is PlatformHintedContent -> copy(inner = inner.mapTransform(type, operation)) else -> this } return new as R } + +@PublishedApi +@Suppress("UNCHECKED_CAST") +internal fun R.recursiveMapTransform(type: KClass, operation: (T) -> T): R { + val new = when (this) { + is ContentGroup -> copy(children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentHeader -> copy(children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentCodeBlock -> copy(children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentCodeInline -> copy(children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentTable -> copy(header = header.map { it.recursiveMapTransform(type, operation) }, children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentList -> copy(children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentDivergentGroup -> copy(children = children.map { it.recursiveMapTransform(type, operation) }) + is ContentDivergentInstance -> copy( + before = before?.recursiveMapTransform(type, operation), + divergent = divergent.recursiveMapTransform(type, operation), + after = after?.recursiveMapTransform(type, operation) + ) + is PlatformHintedContent -> copy(inner = inner.recursiveMapTransform(type, operation)) + else -> this + } + if (new::class == type) { + return operation(new as T) as R + } + return new as R +} -- cgit