Skip to content

Commit

Permalink
Initial support for SM2013
Browse files Browse the repository at this point in the history
  • Loading branch information
sillerud committed Feb 22, 2019
1 parent 9e8093d commit 7ca8faa
Show file tree
Hide file tree
Showing 5 changed files with 488 additions and 3 deletions.
28 changes: 28 additions & 0 deletions resources/arbeidsgiver.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions resources/checkbox.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 18 additions & 2 deletions src/main/kotlin/no/nav/pdfgen/Bootstrap.kt
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ val templateRoot: Path = Paths.get("templates/")
val imagesRoot: Path = Paths.get("resources/")
val fontsRoot: Path = Paths.get("fonts/")
val images = loadImages()
val resources = loadResources()
val handlebars: Handlebars = Handlebars(FileTemplateLoader(templateRoot.toFile())).apply {
registerNavHelpers(this)
infiniteLoops(true)
Expand Down Expand Up @@ -216,15 +217,30 @@ fun loadTemplates() = Files.list(templateRoot)

fun loadImages() = Files.list(imagesRoot)
.filter {
val validExtensions = setOf("jpg", "jpeg", "png", "bmp")
val validExtensions = setOf("jpg", "jpeg", "png", "bmp", "svg")
!Files.isHidden(it) && it.fileName.extension in validExtensions
}
.map {
val fileName = it.fileName.toString()
val extension = if (it.fileName.extension == "jpg") "jpeg" else it.fileName.extension // jpg is not a valid mime-type
val extension = when(it.fileName.extension) {
"jpg" -> "jpeg"// jpg is not a valid mime-type
"svg" -> "svg+xml"
else -> it.fileName.extension
}
val base64string = base64encoder.encodeToString(Files.readAllBytes(it))
val base64 = "data:image/$extension;base64,$base64string"
fileName to base64
}
.toList()
.toMap()

fun loadResources() = Files.list(imagesRoot)
.filter {
val validExtensions = setOf("svg")
!Files.isHidden(it) && it.fileName.extension in validExtensions
}
.map {
it.fileName.toString() to Files.readAllBytes(it)
}
.toList()
.toMap()
25 changes: 24 additions & 1 deletion src/main/kotlin/no/nav/pdfgen/Helpers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,29 @@ import com.github.jknack.handlebars.Handlebars
import com.github.jknack.handlebars.Helper
import no.nav.pdfgen.domain.syfosoknader.Periode
import no.nav.pdfgen.domain.syfosoknader.PeriodeMapper
import java.time.LocalDate
import java.time.format.DateTimeFormatter
import java.time.temporal.ChronoUnit

fun registerNavHelpers(handlebars: Handlebars) {
handlebars.apply {
registerHelper("iso_to_nor_date", Helper<String> { context, _ ->
if (context == null) return@Helper ""
dateFormat.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parse(context))
try {
dateFormat.format(DateTimeFormatter.ISO_OFFSET_DATE_TIME.parseBest(context))
} catch (e: Exception) {
dateFormat.format(DateTimeFormatter.ISO_DATE_TIME.parse(context))
}
})
registerHelper("iso_to_date", Helper<String> { context, _ ->
if (context == null) return@Helper ""
dateFormat.format(DateTimeFormatter.ISO_DATE.parse(context))
})

registerHelper("duration", Helper<String> { context, options ->
ChronoUnit.DAYS.between(LocalDate.from(DateTimeFormatter.ISO_DATE.parse(context)), LocalDate.from(DateTimeFormatter.ISO_DATE.parse(options.param(0))))
})

// Expects json-objects of the form { "fom": "2018-05-20", "tom": "2018-05-29" }
registerHelper("json_to_period", Helper<String> { context, _ ->
if (context == null) {
Expand Down Expand Up @@ -50,6 +60,10 @@ fun registerNavHelpers(handlebars: Handlebars) {
if (context == null) "" else images[context]
})

registerHelper("resource", Helper<String> { context, _ ->
resources[context]?.toString(Charsets.UTF_8) ?: ""
})

registerHelper("capitalize", Helper<String> { context, _ ->
if (context == null) "" else context.toLowerCase().capitalize()
})
Expand All @@ -61,5 +75,14 @@ fun registerNavHelpers(handlebars: Handlebars) {
registerHelper("formatComma", Helper<Any> { context, _ ->
if (context == null) "" else context.toString().replace(".", ",")
})

registerHelper("doubleIf", Helper<Any> { a, options ->
val operator = options.param(0, null as Any?)
val b = options.param(1, null as Any?)
when(operator) {
"||" -> if (options.isFalsy(a) && options.isFalsy(b)) { options.inverse() } else { options.fn() }
else -> options.inverse()
}
})
}
}
Loading

0 comments on commit 7ca8faa

Please sign in to comment.