Skip to content

Commit

Permalink
Internal improvements:
Browse files Browse the repository at this point in the history
1. Removed warnings about clash of names emitted in jruby
2. Extracted directory scanning procedure to separate class to be able introduce different strategies if pages hieararchy
  • Loading branch information
zeldigas committed Jan 2, 2024
1 parent a02cf03 commit 470b74a
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ fun universalConverter(
space, parameters, mapOf(
"md" to MarkdownFileConverter(parameters.markdownConfiguration),
"adoc" to AsciidocFileConverter(parameters.asciidoctorConfiguration)
)
), FileNameBasedDetector
)
}

internal class UniversalConverter(
val space: String,
val conversionParameters: ConversionParameters,
val converters: Map<String, FileConverter>
val converters: Map<String, FileConverter>,
val pagesDetector: PagesDetector,
) : Converter {

override fun convertFile(file: Path): Page {
Expand Down Expand Up @@ -103,34 +104,29 @@ internal class UniversalConverter(
}
}

private fun scanDocuments(dir: Path) =
dir.toFile().walk().filter { it.supported() }
.map {
it.toPath() to converters.getValue(it.extension.lowercase())
.readHeader(it.toPath(), HeaderReadingContext(conversionParameters.titleConverter))
}
.toMap()

private fun convertFilesInDirectory(dir: Path, context: ConvertingContext): List<Page> {
return dir.listDirectoryEntries().filter { it.supported() }.sorted()
.map { file ->
val content = convertSupported(file, context)
val subdirectory = file.parent.resolve(file.nameWithoutExtension)
val children = if (Files.exists(subdirectory) && Files.isDirectory(subdirectory)) {
convertFilesInDirectory(subdirectory, context)
} else {
emptyList()
}
Page(content, file, children)
}
private fun scanDocuments(dir: Path): Map<Path, PageHeader> {
val headers = mutableMapOf<Path, PageHeader>()
val context = HeaderReadingContext(conversionParameters.titleConverter)
pagesDetector.scanDirectoryRecursively(dir,
filter = { it.supported() },
converter = { file ->
headers[file] = converterFor(file).readHeader(file, context)
},
assembler = { _, _, _ -> }
)
return headers
}

private fun convertSupported(file: Path, context: ConvertingContext): PageContent {
return converterFor(file).convert(file, context)
}
private fun convertFilesInDirectory(dir: Path, context: ConvertingContext): List<Page> =
pagesDetector.scanDirectoryRecursively(dir,
filter = { it.supported() },
converter = { file -> converterFor(file).convert(file, context) },
assembler = { file, content, children -> Page(content, file, children) }
)

private fun converterFor(file: Path) =
converters[file.extension] ?: throw IllegalArgumentException("Unsupported extension: ${file.extension}")
converters[file.extension.lowercase()]
?: throw IllegalArgumentException("Unsupported extension: ${file.extension}")

private fun File.supported() = isFile && !name.startsWith("_") && extension.lowercase() in converters
private fun Path.supported() = toFile().supported()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.github.zeldigas.text2confl.convert

import java.nio.file.Files
import java.nio.file.Path
import kotlin.io.path.listDirectoryEntries
import kotlin.io.path.nameWithoutExtension

interface PagesDetector {

fun <T, R> scanDirectoryRecursively(
dir: Path,
filter: (Path) -> Boolean,
converter: (file: Path) -> T,
assembler: (file: Path, parent: T, children: List<R>) -> R
): List<R>

}

object FileNameBasedDetector : PagesDetector {

override fun <T, R> scanDirectoryRecursively(
dir: Path,
filter: (Path) -> Boolean,
converter: (Path) -> T,
assembler: (file: Path, parent: T, children: List<R>) -> R
): List<R> {
return dir.listDirectoryEntries().filter { filter(it) }.sorted()
.map { file ->
val content = converter(file)
val subdirectory = file.parent.resolve(file.nameWithoutExtension)
val children = if (Files.exists(subdirectory) && Files.isDirectory(subdirectory)) {
scanDirectoryRecursively(subdirectory, filter, converter, assembler)
} else {
emptyList()
}
assembler(file, content, children)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
package com.github.zeldigas.text2confl.convert.confluence

interface LanguageMapper {
object LanguageMappers {
val NOP: LanguageMapper = object : LanguageMapper {
override fun mapToConfluenceLanguage(language: String): String? = null
override val supportedLanguages: Set<String>
get() = emptySet()
}

fun mapToConfluenceLanguage(language: String): String?
fun forServer(defaultLanguage: String? = null, extraMapping: Map<String, String> = emptyMap()): LanguageMapper =
LanguageMapperImpl(
CONFLUENCE_SERVER_LANGUAGES,
mapping = SERVER_REMAPPING + extraMapping,
defaultLanguage = defaultLanguage
)

val supportedLanguages: Set<String>
fun forCloud(defaultLanguage: String? = null, extraMapping: Map<String, String> = emptyMap()): LanguageMapper =
LanguageMapperImpl(
CONFLUENCE_CLOUD_LANGUAGES,
mapping = CLOUD_REMAPPING + extraMapping,
defaultLanguage = defaultLanguage
)
}

companion object {
fun nop(): LanguageMapper = object : LanguageMapper {
override fun mapToConfluenceLanguage(language: String): String? = null
override val supportedLanguages: Set<String>
get() = emptySet()
}
interface LanguageMapper {

fun forServer(defaultLanguage: String? = null, extraMapping: Map<String, String> = emptyMap()): LanguageMapper =
LanguageMapperImpl(
CONFLUENCE_SERVER_LANGUAGES,
mapping = SERVER_REMAPPING + extraMapping,
defaultLanguage = defaultLanguage
)
fun mapToConfluenceLanguage(language: String): String?

fun forCloud(defaultLanguage: String? = null, extraMapping: Map<String, String> = emptyMap()): LanguageMapper =
LanguageMapperImpl(
CONFLUENCE_CLOUD_LANGUAGES,
mapping = CLOUD_REMAPPING + extraMapping,
defaultLanguage = defaultLanguage
)
}
val supportedLanguages: Set<String>

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ internal class UniversalConverterTest(
private val converter = UniversalConverter(
"TEST", conversionParameters, mapOf(
"t" to fileConverter
)
), FileNameBasedDetector
)

@Test
Expand Down Expand Up @@ -85,7 +85,7 @@ internal class UniversalConverterTest(
}

@Test
internal fun `Directory conversion`(@TempDir dir: Path) {
internal fun `Detection of documents with duplicate titles`(@TempDir dir: Path) {
createFileStructure(
dir,
"one.t",
Expand All @@ -96,6 +96,7 @@ internal class UniversalConverterTest(
"three/bar.md",
"four.md",
"five.adoc",
"another.t",
"another/hello.t",
"another/foo.t",
)
Expand All @@ -113,7 +114,7 @@ internal class UniversalConverterTest(
}

@Test
internal fun `Detection of documents with duplicate titles`(@TempDir dir: Path) {
internal fun `Directory conversion`(@TempDir dir: Path) {
createFileStructure(
dir,
"one.t",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assertk.assertFailure
import assertk.assertThat
import assertk.assertions.*
import com.github.zeldigas.text2confl.convert.*
import com.github.zeldigas.text2confl.convert.confluence.LanguageMapper
import com.github.zeldigas.text2confl.convert.confluence.LanguageMappers
import com.github.zeldigas.text2confl.convert.confluence.ReferenceProvider
import io.mockk.every
import io.mockk.mockk
Expand Down Expand Up @@ -141,7 +141,7 @@ class AsciidocFileConverterTest {
mapOf(dir / "src.adoc" to PageHeader("Test document", emptyMap()))
),
ConversionParameters(
LanguageMapper.forServer(),
LanguageMappers.forServer(),
titleTransformer(),
docRootLocation = "http://example.com/",
editorVersion = EditorVersion.V1
Expand Down Expand Up @@ -183,7 +183,7 @@ class AsciidocFileConverterTest {
file, ConvertingContext(
ReferenceProvider.singleFile(),
ConversionParameters(
LanguageMapper.forServer(), titleTransformer(), addAutogeneratedNote = false,
LanguageMappers.forServer(), titleTransformer(), addAutogeneratedNote = false,
editorVersion = EditorVersion.V1
),
"TEST"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.zeldigas.text2confl.convert.asciidoc

import assertk.Assert
import assertk.assertThat
import com.github.zeldigas.text2confl.convert.confluence.LanguageMapper
import com.github.zeldigas.text2confl.convert.confluence.LanguageMappers
import org.junit.jupiter.api.Test

internal class RenderingOfCodeBlocksTest : RenderingTestBase() {
Expand Down Expand Up @@ -50,7 +50,7 @@ internal class RenderingOfCodeBlocksTest : RenderingTestBase() {
----
puts "Hello"
----
""".trimIndent(), languageMapper = LanguageMapper.nop()
""".trimIndent(), languageMapper = LanguageMappers.NOP
)

assertThat(resultWithNullLang).isEqualToConfluenceFormat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ internal class LanguageMapperImplTest {

@Test
internal fun `Server mapper`() {
val mapper = LanguageMapper.forServer("test", CUSTOMIZATION)
val mapper = LanguageMappers.forServer("test", CUSTOMIZATION)
assertThat(mapper).isInstanceOf(LanguageMapperImpl::class).all {
transform { it.defaultLanguage }.isEqualTo("test")
prop(LanguageMapperImpl::mapping).isEqualTo(SERVER_REMAPPING + CUSTOMIZATION)
Expand All @@ -25,7 +25,7 @@ internal class LanguageMapperImplTest {

@Test
internal fun `Cloud mapper`() {
val mapper = LanguageMapper.forCloud("test1", CUSTOMIZATION)
val mapper = LanguageMappers.forCloud("test1", CUSTOMIZATION)
assertThat(mapper).isInstanceOf(LanguageMapperImpl::class).all {
transform { it.defaultLanguage }.isEqualTo("test1")
prop(LanguageMapperImpl::mapping).isEqualTo(CLOUD_REMAPPING + CUSTOMIZATION)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import assertk.assertFailure
import assertk.assertThat
import assertk.assertions.*
import com.github.zeldigas.text2confl.convert.*
import com.github.zeldigas.text2confl.convert.confluence.LanguageMapper
import com.github.zeldigas.text2confl.convert.confluence.LanguageMappers
import com.github.zeldigas.text2confl.convert.confluence.ReferenceProvider
import io.mockk.every
import io.mockk.mockk
Expand Down Expand Up @@ -166,7 +166,7 @@ internal class MarkdownFileConverterTest {
file, ConvertingContext(
ReferenceProvider.singleFile(),
ConversionParameters(
LanguageMapper.forServer(), titleTransformer(), docRootLocation = "http://example.com/",
LanguageMappers.forServer(), titleTransformer(), docRootLocation = "http://example.com/",
markdownConfiguration = MarkdownConfiguration(true, emptyList()),
editorVersion = EditorVersion.V1
),
Expand Down Expand Up @@ -207,7 +207,7 @@ internal class MarkdownFileConverterTest {
val result = converter.convert(
file, ConvertingContext(
ReferenceProvider.singleFile(), ConversionParameters(
LanguageMapper.forServer(), titleTransformer(), addAutogeneratedNote = false,
LanguageMappers.forServer(), titleTransformer(), addAutogeneratedNote = false,
markdownConfiguration = MarkdownConfiguration(true, emptyList()),
editorVersion = EditorVersion.V1
), "TEST"
Expand Down Expand Up @@ -265,7 +265,7 @@ internal class MarkdownFileConverterTest {
val result = converter.convert(
file, ConvertingContext(
ReferenceProvider.singleFile(), ConversionParameters(
LanguageMapper.forServer(), titleTransformer(), addAutogeneratedNote = false,
LanguageMappers.forServer(), titleTransformer(), addAutogeneratedNote = false,
markdownConfiguration = MarkdownConfiguration(true, emptyList()),
editorVersion = EditorVersion.V1
), "TEST"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package com.github.zeldigas.text2confl.convert.markdown

import assertk.Assert
import assertk.assertThat
import com.github.zeldigas.text2confl.convert.confluence.LanguageMapper
import com.github.zeldigas.text2confl.convert.confluence.LanguageMappers
import org.junit.jupiter.api.Test

internal class RenderingOfCodeBlocksTest : RenderingTestBase() {
Expand Down Expand Up @@ -45,7 +45,7 @@ internal class RenderingOfCodeBlocksTest : RenderingTestBase() {
```ruby
puts "Hello"
```
""".trimIndent(), languageMapper = LanguageMapper.nop()
""".trimIndent(), languageMapper = LanguageMappers.NOP
)

assertThat(resultWithNullLang).isEqualToConfluenceFormat(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.github.zeldigas.text2confl.core.config
import com.github.zeldigas.text2confl.convert.EditorVersion
import com.github.zeldigas.text2confl.convert.asciidoc.AsciidoctorConfiguration
import com.github.zeldigas.text2confl.convert.confluence.LanguageMapper
import com.github.zeldigas.text2confl.convert.confluence.LanguageMappers
import com.github.zeldigas.text2confl.convert.markdown.MarkdownConfiguration
import io.ktor.http.*
import java.nio.file.Path
Expand All @@ -20,8 +21,8 @@ data class ConverterConfig(
) {
val languageMapper: LanguageMapper
get() = when (editorVersion) {
EditorVersion.V1 -> LanguageMapper.forServer(codeBlockParams.defaultLanguage, codeBlockParams.extraMapping)
EditorVersion.V2 -> LanguageMapper.forCloud(codeBlockParams.defaultLanguage, codeBlockParams.extraMapping)
EditorVersion.V1 -> LanguageMappers.forServer(codeBlockParams.defaultLanguage, codeBlockParams.extraMapping)
EditorVersion.V2 -> LanguageMappers.forCloud(codeBlockParams.defaultLanguage, codeBlockParams.extraMapping)
}

val titleConverter: (Path, String) -> String
Expand Down

0 comments on commit 470b74a

Please sign in to comment.