Skip to content

Commit

Permalink
prepare for generating epub from the manual
Browse files Browse the repository at this point in the history
  • Loading branch information
jillesvangurp committed Jan 2, 2020
1 parent ac1351c commit 75ae94a
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion manual/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ a sneak preview of that.

```kotlin
val demoPage = Page("Demo Page", "demo.md")
KotlinForExample.markdownPage(demoPage) {
KotlinForExample.markdownPageWithNavigation(demoPage) {
+"""
A quick example. You can put any markdown here.
Once this code runs, it actually generates the page: [open it here](demo.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class BulkManualTest : AbstractElasticSearchTest(indexPrefix = "manual") {
""", XContentType.JSON)
}

KotlinForExample.markdownPage(bulkPage) {
KotlinForExample.markdownPageWithNavigation(bulkPage) {
+"""
An important part of working with Elasticsearch is adding content. While the CRUD support is useful
for manipulating individual objects in an index, it is not suitable for sending large amounts of data.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ClientCreationManualTest : AbstractElasticSearchTest(indexPrefix = "manual

@Test
fun `explain client creation`() {
KotlinForExample.markdownPage(createClientPage) {
KotlinForExample.markdownPageWithNavigation(createClientPage) {
+"""
To use the ES Kotlin Wrapper Client, you simply have to create an instance
of the Java High Level Restclient (and have this library on the classpath).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class CoRoutinesManualTest: AbstractElasticSearchTest(indexPrefix = "manual") {
}
""", XContentType.JSON)
}
KotlinForExample.markdownPage(coroutinesPage) {
KotlinForExample.markdownPageWithNavigation(coroutinesPage) {
+"""
The RestHighLevelClient exposes asynchronous versions of most APIs that take a call back to process
the response when it comes back. Using this is kind of boiler plate heavy.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class CrudManualTest : AbstractElasticSearchTest(indexPrefix = "manual") {
// make sure we get rid of the things index before running the rest of this
thingDao.deleteIndex()

KotlinForExample.markdownPage(crudPage) {
KotlinForExample.markdownPageWithNavigation(crudPage) {
+"""
To do anything with Elasticsearch we have to store documents in some index. The Java client
provides everything you need to do this but using it the right way requires quite a bit of boiler plate
Expand Down
22 changes: 12 additions & 10 deletions src/test/kotlin/io/inbot/eskotlinwrapper/manual/KotlinForExample.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ data class Page(
val outputDir: String = "manual",
// val previous: String? = null
// val next: String? = null,
val parent: String? = null
val parent: String? = null,
val emitBookPage: Boolean = false
) {
val link = mdLink(title, fileName)
}
Expand Down Expand Up @@ -52,7 +53,7 @@ class KotlinForExample private constructor(
}

fun mdLinkToSelf(title: String="Link to this source file"): String {
val fn = this.sourceFile() ?: throw IllegalStateException("source file not found")
val fn = this.sourceFileOfExampleCaller() ?: throw IllegalStateException("source file not found")
return mdLink(title,"$repoUrl/tree/master/${fn.path}")
}

Expand Down Expand Up @@ -128,7 +129,7 @@ class KotlinForExample private constructor(
return null
}

internal fun sourceFile(): File? {
internal fun sourceFileOfExampleCaller(): File? {
val fileName = getCallerStackTraceElement().className.replace("\\$.*?$".toRegex(), "").replace(
'.',
File.separatorChar
Expand All @@ -145,8 +146,6 @@ class KotlinForExample private constructor(
}

companion object {


fun markdown(
block: KotlinForExample.() -> Unit
): String {
Expand All @@ -155,8 +154,7 @@ class KotlinForExample private constructor(
return example.buf.toString()
}

fun markdownPage(page: Page, block: KotlinForExample.() -> Unit) {

fun markdownPageWithNavigation(page: Page, block: KotlinForExample.() -> Unit) {
val index = pages.indexOf(page)
val previous = if(index<0) null else if(index==0) null else pages[index-1].fileName
val next = if(index<0) null else if(index==pages.size-1) null else pages[index+1].fileName
Expand All @@ -167,10 +165,12 @@ class KotlinForExample private constructor(
)

val example = KotlinForExample()

example.use(block)
val md = """
# ${page.title}
""".trimIndent().trimMargin() + "\n\n" + example.buf.toString()
val md = """
# ${page.title}
""".trimIndent().trimMargin() + "\n\n" + example.buf.toString()

val pageWithNavigationMd =
(if (nav.isNotEmpty()) nav.joinToString(" | ") + "\n---\n\n" else "") +
md + "\n" +
Expand All @@ -181,6 +181,8 @@ class KotlinForExample private constructor(

File(page.outputDir).mkdirs()
File(page.outputDir, page.fileName).writeText(pageWithNavigationMd)
File("epub").mkdirs()
File("epub",page.fileName).writeText(md)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ val coroutinesMd = "coroutines.md"

val readmePage = Page("Es Kotlin Wrapper Client", readmeMd, outputDir = ".")
val indexPage = Page("Es Kotlin Wrapper Client Manual", indexMd)
val createClientPage = Page("How to create the client", createClientMd, parent = indexMd)
val createClientPage = Page("How to create the client", createClientMd, parent = indexMd, emitBookPage = true)
val crudPage =
Page("Working with objects", crudSupportMd, parent = indexMd)
val bulkPage = Page("Bulk Indexing", bulkIndexingMd, parent = indexMd)
val searchPage = Page("Search", searchMd, parent = indexMd)
val queryDslPage = Page("Query DSL", queryDslMd, parent = indexMd)
val coroutinesPage = Page("Co-routines", coroutinesMd, parent = indexMd)
Page("Working with objects", crudSupportMd, parent = indexMd,emitBookPage = true)
val bulkPage = Page("Bulk Indexing", bulkIndexingMd, parent = indexMd,emitBookPage = true)
val searchPage = Page("Search", searchMd, parent = indexMd,emitBookPage = true)
val queryDslPage = Page("Query DSL", queryDslMd, parent = indexMd,emitBookPage = true)
val coroutinesPage = Page("Co-routines", coroutinesMd, parent = indexMd,emitBookPage = true)

val pages=listOf(createClientPage,crudPage,bulkPage,searchPage,queryDslPage,coroutinesPage)

Expand Down Expand Up @@ -49,7 +49,7 @@ class ManualOverviewPageTest {
a.footnote-ref { vertical-align: super; }
em, em em em, em em em em em { font-style: italic;}
em em, em em em em { font-style: normal; }
pre {font-size: 60%;}
pre {font-size: 50%;}
code{ font-family: monospace; white-space: pre-wrap; }
span.smallcaps{ font-variant: small-caps; }
span.underline{ text-decoration: underline; }
Expand All @@ -58,22 +58,24 @@ class ManualOverviewPageTest {
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
""".trimIndent())
File("epub","title.md").writeText("""
File("epub","metadata.yml").writeText("""
---
title: Searching Elasticsearch using Kotlin
author: Jilles van Gurp
rights: Copyright Jilles van Gurp, 2019
language: en-US
...
""".trimIndent())
File("epub","create_ebook.sh").writeText("""
#!/bin/bash
pandoc --css styles.css -t epub2 -o book.epub -f gfm title.md ${pages.joinToString (" ") {it.fileName}}
pandoc --css styles.css -t epub2 -o book.epub -f gfm --metadata-file metadata.yml ${pages.joinToString (" ") {it.fileName}}
""".trimIndent())
}

@Test
fun `generate index md`() {
KotlinForExample.markdownPage(indexPage) {
KotlinForExample.markdownPageWithNavigation(indexPage) {
+"""
The Elasticsearch Kotlin Wrapper Client adds a lot of functionality to the
Elasticsearch Rest High Level Client. This is mostly done through extension functions.
Expand Down Expand Up @@ -112,7 +114,7 @@ class ManualOverviewPageTest {
// very meta
blockWithOutput {
val demoPage = Page("Demo Page", "demo.md")
KotlinForExample.markdownPage(demoPage) {
KotlinForExample.markdownPageWithNavigation(demoPage) {
+"""
A quick example. You can put any markdown here.
Once this code runs, it actually generates the page: [open it here](demo.md)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class QueryDslManualTest : AbstractElasticSearchTest(indexPrefix = "manual") {
}
}

KotlinForExample.markdownPage(queryDslPage) {
KotlinForExample.markdownPageWithNavigation(queryDslPage) {

+"""
Elasticsearch has a Query DSL and the Java Rest High Level Client comes with a very expansive
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class ReadmeTest : AbstractElasticSearchTest(indexPrefix = "manual") {

@Test
fun `generate readme`() {
KotlinForExample.markdownPage(readmePage) {
KotlinForExample.markdownPageWithNavigation(readmePage) {
+"""
[![](https://jitpack.io/v/jillesvangurp/es-kotlin-wrapper-client.svg)](https://jitpack.io/#jillesvangurp/es-kotlin-wrapper-client)
[![Actions Status](https://github.com/jillesvangurp/es-kotlin-wrapper-client/workflows/CI-gradle-build/badge.svg)](https://github.com/jillesvangurp/es-kotlin-wrapper-client/actions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class SearchManualTest: AbstractElasticSearchTest(indexPrefix = "manual") {
""", XContentType.JSON)
}

KotlinForExample.markdownPage(searchPage) {
KotlinForExample.markdownPageWithNavigation(searchPage) {

block(true) {
// lets use a slightly different model class this time
Expand Down

0 comments on commit 75ae94a

Please sign in to comment.