项目作者: juliankotrba

项目描述 :
:black_nib: The library behind my introduction code block on my website
高级语言: Kotlin
项目地址: git://github.com/juliankotrba/code-writer.git
创建时间: 2018-07-26T15:16:50Z
项目社区:https://github.com/juliankotrba/code-writer

开源协议:MIT License

下载


Code writer

This is the library I use for my code introduction block on my website. Please see juliankotrba.xyz.

Usage

Creating a code block using a DSL

Styles

A code sequences’ style set consists of two styles. A style textStyleBefore, which is used when writing and
a style textStyleAfter that is applied after the last character of the code string has been written.

For example, if you write any keyword into your IDE, it will not get highlighted until the word is fully written (obviously).
That’s why two different styles are needed.

  1. object Styles {
  2. private const val DEFAULT_COLOR = "#E8EAF6"
  3. private const val KEYWORD_COLOR = "#2196F3"
  4. private const val COMMENT_COLOR = "#00E676"
  5. private const val VARIABLE_COLOR = "#FF9800"
  6. private const val STRING_COLOR = "#00E676"
  7. val DFLT_STYLES = StyleSet(CodeSequenceStyle(DEFAULT_COLOR), CodeSequenceStyle(DEFAULT_COLOR))
  8. val KW_STYLES = StyleSet(CodeSequenceStyle(DEFAULT_COLOR), CodeSequenceStyle(KEYWORD_COLOR, WeightValue.BOLD))
  9. val CMNT_STYLES = StyleSet(CodeSequenceStyle(DEFAULT_COLOR), CodeSequenceStyle(COMMENT_COLOR))
  10. val VAR_STYLES = StyleSet(CodeSequenceStyle(VARIABLE_COLOR), CodeSequenceStyle(VARIABLE_COLOR))
  11. val STRING_STYLES = StyleSet(CodeSequenceStyle(STRING_COLOR), CodeSequenceStyle(STRING_COLOR))
  12. val ENUM_CONSTANT_STYLES = StyleSet(CodeSequenceStyle(DEFAULT_COLOR), CodeSequenceStyle(VARIABLE_COLOR))
  13. }

Code block DSL

The following code example shows how to create a new code block. First you need to call the codeBock
function. Inside the passed lambda you need to define code lines with the codeLine function. Last but not least, the
text to be written can be defined using one of the codeSequence functions.

Note that there are several ways to create (a) code sequence(s)!

  1. codeBlock {
  2. codeLine {
  3. codeSequence {
  4. text = "// "
  5. styleSet = CMNT_STYLES
  6. }
  7. codeSequence {
  8. text = "Person.kt"
  9. styleSet = STRING_STYLES
  10. }
  11. }
  12. codeLine {
  13. codeSequence("data ", KW_STYLES)
  14. codeSequence("class ", KW_STYLES)
  15. codeSequence("Person(", DFLT_STYLES)
  16. }
  17. codeLine {
  18. codeSequenceWithTabs(2, "val ", KW_STYLES)
  19. codeSequence("name", VAR_STYLES)
  20. codeSequence(": String = ", DFLT_STYLES, "breakable")
  21. codeSequence("\"Ash Ketchum\"", STRING_STYLES, "cssTab")
  22. codeSequence(",", DFLT_STYLES)
  23. }
  24. codeLine {
  25. codeSequenceWithTabs(2, "val ", KW_STYLES)
  26. codeSequence("job", VAR_STYLES)
  27. codeSequence(": String? = ", DFLT_STYLES, "breakable")
  28. codeSequence("\"Pokémon Trainer\"", STRING_STYLES, "cssTab")
  29. codeSequence(",", DFLT_STYLES)
  30. }
  31. codeLine {
  32. multipleCodeSequencesWithLeadingTabs(2) {
  33. charSequences = listOf("val ", "favPkm", ": ", "Pkm", " = ", "Pkm", ".", "PIKACHU")
  34. styleSets = listOf(
  35. KW_STYLES,
  36. VAR_STYLES,
  37. DFLT_STYLES,
  38. DFLT_STYLES,
  39. DFLT_STYLES,
  40. DFLT_STYLES,
  41. DFLT_STYLES,
  42. ENUM_CONSTANT_STYLES
  43. )
  44. elementClasses = listOf(4 to "unknown", 6 to "unknown") // 1-indexed !
  45. }
  46. }
  47. codeLine {
  48. codeSequence(")", DFLT_STYLES)
  49. }
  50. }

Every code sequence also stores an element class (default value is an empty string). This class can be used if additional
styling is needed. In the above example this property is used to mark an error because of the missing enum class Pkm.
It can also be used to make the code responsive as you can see inside the lambdas of the codeSequenceWithTabs functions.

Writing

A container manager needs so be created first. This example uses the a manager for a \ tag container. The second
argument of the DivContainerManager is a function, which generates a write delay in milliseconds.
The default value is 0 milliseconds.

  1. val introductionCode: CodeBlock = ...
  2. val divContainer = document.getElementById("...") as HTMLDivElement
  3. val divContainerManager = DivContainerManager(divContainer) {
  4. listOf(40, 40, 40, 50, 60, 100, 150).shuffled().first()
  5. }
  6. CodeWriter(GlobalScope, divContainerManager).write(introductionCode)

Web directory

You can create a Gradle task to put all generated JavaScript files into a specific directory.
In the example module, all JavaScript files get put into web/js after building.
Also the HTML/CSS files in the resource folder are copied to the web folder. For further information check out the build.gradle file.

License

MIT