Skip to content

Commit 1e36c70

Browse files
Merge pull request #40 from amanshuraikwar/ios-client
added blog data json api
2 parents 3995371 + 2715d15 commit 1e36c70

11 files changed

Lines changed: 3716 additions & 2 deletions

File tree

project-files/.DS_Store

6 KB
Binary file not shown.

project-files/Portfolio/buildWebsite.kts

Lines changed: 279 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ println("Creating build...")
99
File(".") exec "mkdir build"
1010

1111
generateEmptyBlogListFile()
12+
generateEmptyBlogListFileJson()
1213

1314
generateEmptyDataStoreFile("PageType.PROJECTS")
1415
buildAndCopyDirectoryContents("projects")
@@ -35,6 +36,7 @@ try {
3536
.forEach { file ->
3637
println("Parsing ${file.name}...")
3738
blogListEntryList.add(generateBlogMdSource(file))
39+
generateBlogMdSourceJson(file)
3840
}
3941
} catch (e: Exception) {
4042
println("Exception while parsing markdown files!")
@@ -44,6 +46,9 @@ try {
4446
println("Creating blog list file...")
4547
createBlogListFile(blogListEntryList)
4648

49+
println("Creating blog list json file...")
50+
createBlogListFileJson(blogListEntryList)
51+
4752
generateEmptyDataStoreFile("PageType.HOME")
4853
buildAndCopyDirectoryContents("home", deleteAssets = false)
4954

@@ -94,6 +99,71 @@ data class BlogListEntry(
9499
val link: String
95100
)
96101

102+
fun createBlogListFileJson(blogListEntryList: List<BlogListEntry>): File {
103+
val outputFile = File(
104+
"web/src/jsMain/resources/api/blog_list.json"
105+
)
106+
if (outputFile.exists()) {
107+
outputFile.delete()
108+
}
109+
110+
outputFile.bufferedWriter().use { out ->
111+
out.write("{\n")
112+
out.write("\t\"blog_list\":[\n")
113+
114+
blogListEntryList
115+
.sortedByDescending {
116+
var formatter = DateTimeFormatter.ofPattern("MMMM d, yyyy")
117+
LocalDate.parse(
118+
it.date,
119+
formatter
120+
)
121+
}
122+
.forEachIndexed { index, entry ->
123+
println("Creating blog entry for ${entry.link}")
124+
out.write(
125+
"""
126+
{
127+
"title": "${entry.title}",
128+
"date": "${entry.date}",
129+
"firstParagraph": "${entry.firstParagraph}",
130+
"link": "${entry.link}",
131+
"id": "${entry.link.split("/").last()}"
132+
}${if (index == blogListEntryList.size - 1) "" else ","}
133+
""".trimIndent()
134+
.split("\n")
135+
.map {
136+
it.prependIndent().prependIndent()
137+
}
138+
.reduce { acc, s -> "$acc\n$s" }
139+
)
140+
141+
out.write("\n")
142+
}
143+
out.write("\t]\n")
144+
out.write("}\n")
145+
}
146+
147+
return outputFile
148+
}
149+
150+
fun generateEmptyBlogListFileJson(): File {
151+
val outputFile = File(
152+
"web/src/jsMain/resources/api/blog_list.json"
153+
)
154+
if (outputFile.exists()) {
155+
outputFile.delete()
156+
}
157+
158+
outputFile.bufferedWriter().use { out ->
159+
out.write("{\n")
160+
out.write("\t\"blog_list\": []\n")
161+
out.write("}\n")
162+
}
163+
164+
return outputFile
165+
}
166+
97167
fun generateEmptyBlogListFile(): File {
98168
val outputFile = File(
99169
"shared/src/jsMain/kotlin/io/github/amanshuraikwar/portfolio/GeneratedBlogListDataStore.kt"
@@ -456,6 +526,215 @@ fun generateBlogMdSource(file: File): BlogListEntry {
456526
)
457527
}
458528

529+
fun generateBlogMdSourceJson(file: File) {
530+
var title = ""
531+
var date = ""
532+
var firstParagraph = ""
533+
534+
val dirName = file.name.dropLast(3)
535+
536+
val outputFile =
537+
File("web/src/jsMain/resources/api/$dirName.json")
538+
outputFile.delete()
539+
540+
outputFile.bufferedWriter().use { out ->
541+
out.write("{\n")
542+
543+
out.write("\t\"blog_data\": [\n")
544+
545+
val lines = file.readLines()
546+
547+
var i = 0
548+
while (i < lines.size) {
549+
val line = lines[i]
550+
when {
551+
line.startsWith("!Btn[") -> {
552+
val (label, url) = line
553+
.drop(5)
554+
.trim()
555+
.split("]", "(", ")")
556+
.filter {
557+
it.trim().isNotBlank()
558+
}
559+
.take(2)
560+
561+
out.write(
562+
"""
563+
{
564+
"text": "${label.trim()}",
565+
"url": "${url.trim()}",
566+
"type": "BTN"
567+
}${if (i == lines.size - 1) "" else ","}
568+
""".trimIndent()
569+
.split("\n")
570+
.map {
571+
it.prependIndent().prependIndent().prependIndent()
572+
}
573+
.reduce { acc, s -> "$acc\n$s" }
574+
)
575+
576+
out.write("\n")
577+
}
578+
line.startsWith("![") -> {
579+
val (label, url) = line
580+
.drop(2)
581+
.trim()
582+
.split("]", "(", ")")
583+
.filter {
584+
it.trim().isNotBlank()
585+
}
586+
.take(2)
587+
588+
out.write(
589+
"""
590+
{
591+
"label": "${label.trim()}",
592+
"url": "${url.trim()}",
593+
"type": "IMG"
594+
}${if (i == lines.size - 1) "" else ","}
595+
""".trimIndent()
596+
.split("\n")
597+
.map {
598+
it.prependIndent().prependIndent().prependIndent()
599+
}
600+
.reduce { acc, s -> "$acc\n$s" }
601+
)
602+
603+
out.write("\n")
604+
}
605+
line.startsWith("###") -> {
606+
out.write(
607+
"""
608+
{
609+
"text": "${line.drop(3).trim()}",
610+
"type": "H3"
611+
}${if (i == lines.size - 1) "" else ","}
612+
""".trimIndent()
613+
.split("\n")
614+
.map {
615+
it.prependIndent().prependIndent().prependIndent()
616+
}
617+
.reduce { acc, s -> "$acc\n$s" }
618+
)
619+
out.write("\n")
620+
}
621+
line.startsWith("#Date") -> {
622+
out.write(
623+
"""
624+
{
625+
"text": "${line.drop(5).trim()}",
626+
"type": "DATE"
627+
}${if (i == lines.size - 1) "" else ","}
628+
""".trimIndent()
629+
.split("\n")
630+
.map {
631+
it.prependIndent().prependIndent().prependIndent()
632+
}
633+
.reduce { acc, s -> "$acc\n$s" }
634+
)
635+
out.write("\n")
636+
637+
if (date == "") {
638+
date = line.drop(5).trim()
639+
}
640+
}
641+
line.startsWith("#") -> {
642+
out.write(
643+
"""
644+
{
645+
"text": "${line.drop(1).trim()}",
646+
"type": "H1"
647+
}${if (i == lines.size - 1) "" else ","}
648+
""".trimIndent()
649+
.split("\n")
650+
.map {
651+
it.prependIndent().prependIndent().prependIndent()
652+
}
653+
.reduce { acc, s -> "$acc\n$s" }
654+
)
655+
out.write("\n")
656+
657+
if (title == "") {
658+
title = line.drop(1).trim()
659+
}
660+
}
661+
line.startsWith("```") -> {
662+
val language = if (line.trim().length > 3) {
663+
line.trim().drop(3).trim()
664+
} else {
665+
""
666+
}
667+
var codeBlockLines = mutableListOf<String>()
668+
while (++i < lines.size && !lines[i].startsWith("```")) {
669+
val lastLine = lines[i]
670+
codeBlockLines.add(lastLine)
671+
}
672+
val codeBlockString = codeBlockLines.fold("") { acc, cur ->
673+
"$acc$cur\\n".trimIndent()
674+
}.replace("\"", "\\\"")
675+
676+
out.write(
677+
"""
678+
{
679+
"language": "$language",
680+
"type": "CODE",
681+
"code": "$codeBlockString"
682+
}${if (i == lines.size - 1) "" else ","}
683+
""".trimIndent()
684+
.split("\n")
685+
.map {
686+
it.prependIndent().prependIndent().prependIndent()
687+
}
688+
.reduce { acc, s -> "$acc\n$s" }
689+
)
690+
}
691+
line.trim().isNotBlank() -> {
692+
out.write(
693+
"""
694+
{
695+
"text": "${line.trim()}",
696+
"type": "P"
697+
}${if (i == lines.size - 1) "" else ","}
698+
""".trimIndent()
699+
.split("\n")
700+
.map {
701+
it.prependIndent().prependIndent().prependIndent()
702+
}
703+
.reduce { acc, s -> "$acc\n$s" }
704+
)
705+
out.write("\n")
706+
707+
if (firstParagraph == "") {
708+
firstParagraph = line.trim()
709+
}
710+
}
711+
i != lines.size - 1 && lines[i + 1].trim().isBlank() -> {
712+
out.write(
713+
"""
714+
{
715+
"type": "SPACER"
716+
}${if (i == lines.size - 1) "" else ","}
717+
""".trimIndent()
718+
.split("\n")
719+
.map {
720+
it.prependIndent().prependIndent().prependIndent()
721+
}
722+
.reduce { acc, s -> "$acc\n$s" }
723+
)
724+
out.write("\n")
725+
i++
726+
}
727+
else -> {
728+
// do nothing if its a single empty line
729+
}
730+
}
731+
i++
732+
}
733+
out.write("\t]\n")
734+
out.write("}\n")
735+
}
736+
}
737+
459738
/**
460739
* Shorthand for [File.execute]. Assumes that all spaces are argument separators,
461740
* so no argument may contain a space.

0 commit comments

Comments
 (0)