|
1 | 1 | package io.writeopia.sdk.import.markdown |
2 | 2 |
|
| 3 | +import io.writeopia.sdk.manager.InTextMarkdownHandler |
3 | 4 | import io.writeopia.sdk.models.story.StoryTypes |
4 | 5 | import io.writeopia.sdk.models.story.Tag |
5 | 6 | import io.writeopia.sdk.serialization.data.StoryStepApi |
6 | 7 | import io.writeopia.sdk.serialization.data.StoryTypeApi |
7 | 8 | import io.writeopia.sdk.serialization.data.TagInfoApi |
| 9 | +import io.writeopia.sdk.serialization.extensions.toApi |
| 10 | +import io.writeopia.sdk.serialization.extensions.toModel |
8 | 11 |
|
9 | 12 | object MarkdownParser { |
10 | 13 |
|
11 | 14 | fun parse(lines: List<String>): List<StoryStepApi> { |
12 | 15 | var acc = -1 |
13 | | - // Take care when moving the code, order matters for comparations! |
14 | 16 | return lines.map { it.trim() } |
15 | 17 | .mapIndexed { i, trimmed -> |
16 | 18 | acc++ |
17 | | - when { |
18 | | - i == 0 && trimmed.startsWith("#") -> { |
19 | | - val type = StoryTypes.TITLE.type |
20 | | - StoryStepApi( |
21 | | - type = StoryTypeApi(type.name, type.number), |
22 | | - text = trimmed.drop(1).trimStart(), |
23 | | - position = acc |
24 | | - ) |
25 | | - } |
| 19 | + val stepApi = parseLine(acc, i, trimmed) |
26 | 20 |
|
27 | | - i == 0 && !trimmed.startsWith("#") -> { |
28 | | - val type = StoryTypes.TITLE.type |
29 | | - StoryStepApi( |
30 | | - type = StoryTypeApi(type.name, type.number), |
31 | | - text = "", |
32 | | - position = acc |
33 | | - ) |
34 | | - } |
| 21 | + InTextMarkdownHandler.handleMarkdown(stepApi.toModel()).toApi(acc) |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + private fun parseLine(acc: Int, i: Int, trimmed: String) = |
| 26 | + // Take care when moving the code, order matters! |
| 27 | + when { |
| 28 | + i == 0 && trimmed.startsWith("#") -> { |
| 29 | + val type = StoryTypes.TITLE.type |
| 30 | + StoryStepApi( |
| 31 | + type = StoryTypeApi(type.name, type.number), |
| 32 | + text = trimmed.drop(1).trimStart(), |
| 33 | + position = acc |
| 34 | + ) |
| 35 | + } |
35 | 36 |
|
36 | | - trimmed.startsWith("####") -> { |
37 | | - val type = StoryTypes.TEXT.type |
38 | | - StoryStepApi( |
39 | | - type = StoryTypeApi(type.name, type.number), |
40 | | - text = trimmed.drop(4).trimStart(), |
41 | | - tags = setOf(TagInfoApi(Tag.H4.name, 0)), |
42 | | - position = acc |
43 | | - ) |
44 | | - } |
| 37 | + i == 0 && !trimmed.startsWith("#") -> { |
| 38 | + val type = StoryTypes.TITLE.type |
| 39 | + StoryStepApi( |
| 40 | + type = StoryTypeApi(type.name, type.number), |
| 41 | + text = "", |
| 42 | + position = acc |
| 43 | + ) |
| 44 | + } |
| 45 | + |
| 46 | + trimmed.startsWith("####") -> { |
| 47 | + val type = StoryTypes.TEXT.type |
| 48 | + StoryStepApi( |
| 49 | + type = StoryTypeApi(type.name, type.number), |
| 50 | + text = trimmed.drop(4).trimStart(), |
| 51 | + tags = setOf(TagInfoApi(Tag.H4.name, 0)), |
| 52 | + position = acc |
| 53 | + ) |
| 54 | + } |
45 | 55 |
|
46 | | - trimmed.startsWith("###") -> { |
47 | | - val type = StoryTypes.TEXT.type |
48 | | - StoryStepApi( |
49 | | - type = StoryTypeApi(type.name, type.number), |
50 | | - text = trimmed.drop(3).trimStart(), |
51 | | - tags = setOf(TagInfoApi(Tag.H3.name, 0)), |
52 | | - position = acc |
53 | | - ) |
54 | | - } |
| 56 | + trimmed.startsWith("###") -> { |
| 57 | + val type = StoryTypes.TEXT.type |
| 58 | + StoryStepApi( |
| 59 | + type = StoryTypeApi(type.name, type.number), |
| 60 | + text = trimmed.drop(3).trimStart(), |
| 61 | + tags = setOf(TagInfoApi(Tag.H3.name, 0)), |
| 62 | + position = acc |
| 63 | + ) |
| 64 | + } |
55 | 65 |
|
56 | | - trimmed.startsWith("##") -> { |
57 | | - val type = StoryTypes.TEXT.type |
58 | | - StoryStepApi( |
59 | | - type = StoryTypeApi(type.name, type.number), |
60 | | - text = trimmed.drop(2).trimStart(), |
61 | | - tags = setOf(TagInfoApi(Tag.H2.name, 0)), |
62 | | - position = acc |
63 | | - ) |
64 | | - } |
| 66 | + trimmed.startsWith("##") -> { |
| 67 | + val type = StoryTypes.TEXT.type |
| 68 | + StoryStepApi( |
| 69 | + type = StoryTypeApi(type.name, type.number), |
| 70 | + text = trimmed.drop(2).trimStart(), |
| 71 | + tags = setOf(TagInfoApi(Tag.H2.name, 0)), |
| 72 | + position = acc |
| 73 | + ) |
| 74 | + } |
65 | 75 |
|
66 | | - trimmed.startsWith("#") -> { |
67 | | - val type = StoryTypes.TEXT.type |
68 | | - StoryStepApi( |
69 | | - type = StoryTypeApi(type.name, type.number), |
70 | | - text = trimmed.drop(1).trimStart(), |
71 | | - tags = setOf(TagInfoApi(Tag.H1.name, 0)), |
72 | | - position = acc |
73 | | - ) |
74 | | - } |
| 76 | + trimmed.startsWith("#") -> { |
| 77 | + val type = StoryTypes.TEXT.type |
| 78 | + StoryStepApi( |
| 79 | + type = StoryTypeApi(type.name, type.number), |
| 80 | + text = trimmed.drop(1).trimStart(), |
| 81 | + tags = setOf(TagInfoApi(Tag.H1.name, 0)), |
| 82 | + position = acc |
| 83 | + ) |
| 84 | + } |
75 | 85 |
|
76 | 86 | // trimmed.matches(Regex("^\\d+\\.\\s+.*")) -> { |
77 | 87 | // StoryType.OrderedListItem to trimmed.replace(Regex("^\\d+\\.\\s+"), "") |
78 | 88 | // } |
79 | 89 |
|
80 | | - trimmed.startsWith("---") -> { |
81 | | - val type = StoryTypes.DIVIDER.type |
82 | | - StoryStepApi( |
83 | | - type = StoryTypeApi(type.name, type.number), |
84 | | - position = acc |
85 | | - ) |
86 | | - } |
| 90 | + trimmed.startsWith("---") -> { |
| 91 | + val type = StoryTypes.DIVIDER.type |
| 92 | + StoryStepApi( |
| 93 | + type = StoryTypeApi(type.name, type.number), |
| 94 | + position = acc |
| 95 | + ) |
| 96 | + } |
87 | 97 |
|
88 | | - trimmed.startsWith("[] ") || trimmed.startsWith("-[] ") -> { |
89 | | - val type = StoryTypes.CHECK_ITEM.type |
90 | | - StoryStepApi( |
91 | | - type = StoryTypeApi(type.name, type.number), |
92 | | - text = trimmed.drop(3).trimStart(), |
93 | | - position = acc |
94 | | - ) |
95 | | - } |
| 98 | + trimmed.startsWith("[] ") || trimmed.startsWith("-[] ") -> { |
| 99 | + val type = StoryTypes.CHECK_ITEM.type |
| 100 | + StoryStepApi( |
| 101 | + type = StoryTypeApi(type.name, type.number), |
| 102 | + text = trimmed.drop(3).trimStart(), |
| 103 | + position = acc |
| 104 | + ) |
| 105 | + } |
96 | 106 |
|
97 | | - trimmed.startsWith("- ") || trimmed.startsWith("* ") -> { |
98 | | - val type = StoryTypes.UNORDERED_LIST_ITEM.type |
99 | | - StoryStepApi( |
100 | | - type = StoryTypeApi(type.name, type.number), |
101 | | - text = trimmed.drop(2).trimStart(), |
102 | | - position = acc |
103 | | - ) |
104 | | - } |
| 107 | + trimmed.startsWith("- ") || trimmed.startsWith("* ") -> { |
| 108 | + val type = StoryTypes.UNORDERED_LIST_ITEM.type |
| 109 | + StoryStepApi( |
| 110 | + type = StoryTypeApi(type.name, type.number), |
| 111 | + text = trimmed.drop(2).trimStart(), |
| 112 | + position = acc |
| 113 | + ) |
| 114 | + } |
105 | 115 |
|
106 | | - else -> { |
107 | | - val type = StoryTypes.TEXT.type |
108 | | - StoryStepApi( |
109 | | - type = StoryTypeApi(type.name, type.number), |
110 | | - text = trimmed, |
111 | | - position = acc |
112 | | - ) |
113 | | - } |
114 | | - } |
| 116 | + else -> { |
| 117 | + val type = StoryTypes.TEXT.type |
| 118 | + StoryStepApi( |
| 119 | + type = StoryTypeApi(type.name, type.number), |
| 120 | + text = trimmed, |
| 121 | + position = acc |
| 122 | + ) |
115 | 123 | } |
116 | | - } |
| 124 | + } |
117 | 125 | } |
0 commit comments