Skip to content
This repository was archived by the owner on Feb 15, 2022. It is now read-only.

Commit fa1b70d

Browse files
author
kanishka-work
authored
Language: books - factor out data (#268)
* book data read from data file * also: initial js-yaml binding
1 parent 8b31b90 commit fa1b70d

File tree

7 files changed

+117
-60
lines changed

7 files changed

+117
-60
lines changed

bindings/JsYaml.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@module("js-yaml") external load: (string, ~options: 'a=?, unit) => Js.Json.t = "load"

common/Book.res

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
type t = {
2+
id: int,
3+
name: string,
4+
link: string,
5+
image: string,
6+
}
7+
8+
let readAll = () => {
9+
let booksDatabasePath = "data/books.yaml"
10+
let fileContents = Fs.readFileSync(booksDatabasePath)
11+
let jsonRes = JsYaml.load(fileContents, ())
12+
let jsonArr = Js.Option.getExn(Js.Json.decodeArray(jsonRes))
13+
Js.Array.map(o => {
14+
let dict = Js.Option.getExn(Js.Json.decodeObject(o))
15+
let id = Belt.Int.fromFloat(
16+
Js.Option.getExn(Js.Json.decodeNumber(Js.Dict.unsafeGet(dict, "id"))),
17+
)
18+
let name = Js.Option.getExn(Js.Json.decodeString(Js.Dict.unsafeGet(dict, "name")))
19+
let link = Js.Option.getExn(Js.Json.decodeString(Js.Dict.unsafeGet(dict, "link")))
20+
let image = Js.Option.getExn(Js.Json.decodeString(Js.Dict.unsafeGet(dict, "image")))
21+
{id: id, name: name, link: link, image: image}
22+
}, jsonArr)
23+
}

data/books.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
- id: 1
2+
name: "Real World OCaml"
3+
link: https://dev.realworldocaml.org/
4+
image: real-world-ocaml.jpg
5+
- id: 2
6+
name: "OCaml from the very beginning"
7+
link: https://ocaml-book.com/
8+
image: oc-beg.png
9+
- id: 3
10+
name: "More OCaml: Algorithms, Methods, and Diversions"
11+
link: https://ocaml-book.com/more-ocaml-algorithms-methods-diversions/
12+
image: more-oc.png
13+

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"bs-platform": "9.0.0",
1515
"gray-matter": "^4.0.2",
16+
"js-yaml": "^4.0.0",
1617
"next": "^10.0.6",
1718
"next-mdx-remote": "^2.1.3",
1819
"next-transpile-modules": "^4.1.0",
@@ -43,9 +44,9 @@
4344
"devDependencies": {
4445
"@tailwindcss/typography": "^0.4.0",
4546
"autoprefixer": "^10.2.4",
47+
"esy": "^0.6.8",
4648
"postcss": "^8.2.5",
47-
"tailwindcss": "^2.0.3",
48-
"esy": "^0.6.8"
49+
"tailwindcss": "^2.0.3"
4950
},
5051
"devDependenciesDescription": {},
5152
"postcss": {

pages/resources/language.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import LanguageRes from "res_pages/resources/ResourcesLanguage";
2-
3-
export default function Language(props) {
4-
return <LanguageRes {...props} />
5-
}
1+
export {
2+
default,
3+
getStaticProps,
4+
} from "res_pages/resources/ResourcesLanguage.js"

res_pages/resources/ResourcesLanguage.res

Lines changed: 61 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,24 @@ module UserLevelIntroduction = {
1717
}
1818

1919
module Books = {
20+
type t = {
21+
booksLabel: string,
22+
books: array<Book.t>,
23+
}
24+
2025
@react.component
21-
let make = (~margins) =>
26+
let make = (~margins, ~content) =>
2227
// TODO: define content type; extract content
2328
<div
2429
className={"bg-white overflow-hidden shadow rounded-lg mx-10 mx-auto max-w-5xl " ++ margins}>
2530
<div className="px-4 py-5 sm:px-6 sm:py-9">
26-
<h2 className="text-center text-orangedark text-7xl font-bold mb-8"> {s(`BOOKS`)} </h2>
31+
<h2 className="text-center text-orangedark text-7xl font-bold mb-8">
32+
{s(content.booksLabel)}
33+
</h2>
2734
<div className="grid grid-cols-5 items-center mb-8 px-6">
35+
// TODO: define state to track location within books list, activate navigation
2836
<div className="flex justify-center">
29-
// TODO: define state to track location within books list, activate navigation
37+
// TODO: make navigation arrows accesssible
3038
<svg
3139
className="h-24 center"
3240
viewBox="0 0 90 159"
@@ -40,30 +48,16 @@ module Books = {
4048
/>
4149
</svg>
4250
</div>
43-
<div className="flex justify-center">
44-
// TODO: visual indicator that link opens new tab
45-
<a href="https://dev.realworldocaml.org/" target="_blank">
46-
<img
47-
className="h-36 w-28" src="/static/real-world-ocaml.jpg" alt="Real World OCaml book"
48-
/>
49-
</a>
50-
</div>
51-
<div className="flex justify-center">
52-
<a href="https://ocaml-book.com/" target="_blank">
53-
<img
54-
className="h-36 w-28"
55-
src="/static/oc-beg.png"
56-
alt="OCaml from the very beginning book"
57-
/>
58-
</a>
59-
</div>
60-
<div className="flex justify-center">
61-
<a
62-
href="http://ocaml-book.com/more-ocaml-algorithms-methods-diversions/"
63-
target="_blank">
64-
<img className="h-36 w-28" src="/static/more-oc.png" />
65-
</a>
66-
</div>
51+
{content.books
52+
|> Js.Array.mapi((b: Book.t, idx) =>
53+
<div className="flex justify-center" key={Js.Int.toString(idx)}>
54+
// TODO: visual indicator that link opens new tab
55+
<a href=b.link target="_blank">
56+
<img className="h-36 w-28" src={"/static/" ++ b.image} alt={b.name ++ " book"} />
57+
</a>
58+
</div>
59+
)
60+
|> React.array}
6761
<div className="flex justify-center">
6862
<svg
6963
className="h-24" viewBox="0 0 90 159" fill="none" xmlns="http://www.w3.org/2000/svg">
@@ -214,38 +208,16 @@ type t = {
214208
pageDescription: string,
215209
beginning: UserLevelIntroduction.t,
216210
growing: UserLevelIntroduction.t,
211+
booksContent: Books.t,
217212
expanding: UserLevelIntroduction.t,
218213
diversifying: UserLevelIntroduction.t,
219214
researching: UserLevelIntroduction.t,
220215
}
221216

222-
let contentEn = {
223-
title: `Language`,
224-
pageDescription: `This is the home of learning and tutorials. Whether you're a beginner, a teacher, or a seasoned researcher, this is where you can find the resources you need to accomplish your goals in OCaml.`,
225-
beginning: {
226-
level: `Beginning`,
227-
introduction: `Are you a beginner? Or just someone who wants to brush up on the fundamentals? In either case, the OFronds tutorial system has you covered!`,
228-
},
229-
growing: {
230-
level: `Growing`,
231-
introduction: `Familiar with the basics and looking to get a more robust understanding of OCaml? Or just curious? Check out the books available on OCaml:`,
232-
},
233-
expanding: {
234-
level: `Expanding`,
235-
introduction: `Have a strong foundation in OCaml? Time to get involved! Prepare by getting familiar with the OCaml Manual:`,
236-
},
237-
diversifying: {
238-
level: `Diversifying`,
239-
introduction: `Now that you're familiar with the building blocks of OCaml, you may want to diversify your portfolio and have a look at the many applications that operate using OCaml.`,
240-
},
241-
researching: {
242-
level: `Researching`,
243-
introduction: `Aspiring towards greater understanding of the language? Want to push the limits and discover brand new things? Check out papers written by leading OCaml researchers:`,
244-
},
245-
}
217+
type props = {content: t}
246218

247219
@react.component
248-
let make = (~content=contentEn) => <>
220+
let make = (~content) => <>
249221
<ConstructionBanner
250222
figmaLink=`https://www.figma.com/file/36JnfpPe1Qoc8PaJq8mGMd/V1-Pages-Next-Step?node-id=1085%3A121`
251223
playgroundLink=`/play/resources/language`
@@ -259,7 +231,7 @@ let make = (~content=contentEn) => <>
259231
/>
260232
<UserLevelIntroduction content=content.beginning margins=`mb-20` />
261233
<UserLevelIntroduction content=content.growing margins=`mb-20` />
262-
<Books margins=`mb-16` />
234+
<Books margins=`mb-16` content=content.booksContent />
263235
<UserLevelIntroduction content=content.expanding margins=`mb-20` />
264236
<Manual margins=`mb-20` />
265237
<UserLevelIntroduction content=content.diversifying margins=`mb-20` />
@@ -268,4 +240,40 @@ let make = (~content=contentEn) => <>
268240
<Papers margins=`mb-16` />
269241
</>
270242

243+
let getStaticProps = _ctx => {
244+
let books = Book.readAll()
245+
// TODO: read book sorting and filtering information and adjust array
246+
let props = {
247+
content: {
248+
title: `Language`,
249+
pageDescription: `This is the home of learning and tutorials. Whether you're a beginner, a teacher, or a seasoned researcher, this is where you can find the resources you need to accomplish your goals in OCaml.`,
250+
beginning: {
251+
level: `Beginning`,
252+
introduction: `Are you a beginner? Or just someone who wants to brush up on the fundamentals? In either case, the OFronds tutorial system has you covered!`,
253+
},
254+
growing: {
255+
level: `Growing`,
256+
introduction: `Familiar with the basics and looking to get a more robust understanding of OCaml? Or just curious? Check out the books available on OCaml:`,
257+
},
258+
booksContent: {
259+
booksLabel: "Books",
260+
books: books,
261+
},
262+
expanding: {
263+
level: `Expanding`,
264+
introduction: `Have a strong foundation in OCaml? Time to get involved! Prepare by getting familiar with the OCaml Manual:`,
265+
},
266+
diversifying: {
267+
level: `Diversifying`,
268+
introduction: `Now that you're familiar with the building blocks of OCaml, you may want to diversify your portfolio and have a look at the many applications that operate using OCaml.`,
269+
},
270+
researching: {
271+
level: `Researching`,
272+
introduction: `Aspiring towards greater understanding of the language? Want to push the limits and discover brand new things? Check out papers written by leading OCaml researchers:`,
273+
},
274+
},
275+
}
276+
{"props": props}
277+
}
278+
271279
let default = make

yarn.lock

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,11 @@ argparse@^1.0.7:
12071207
dependencies:
12081208
sprintf-js "~1.0.2"
12091209

1210+
argparse@^2.0.1:
1211+
version "2.0.1"
1212+
resolved "https://registry.yarnpkg.com/argparse/-/argparse-2.0.1.tgz#246f50f3ca78a3240f6c997e8a9bd1eac49e4b38"
1213+
integrity sha512-8+9WqebbFzpX9OR+Wa6O29asIogeRMzcGtAINdpMHHyAg10f05aSFVBbcEqGf/PXw1EjAZ+q2/bEBg3DvurK3Q==
1214+
12101215
array-flatten@^3.0.0:
12111216
version "3.0.0"
12121217
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-3.0.0.tgz#6428ca2ee52c7b823192ec600fa3ed2f157cd541"
@@ -2519,6 +2524,13 @@ js-yaml@^3.11.0:
25192524
argparse "^1.0.7"
25202525
esprima "^4.0.0"
25212526

2527+
js-yaml@^4.0.0:
2528+
version "4.0.0"
2529+
resolved "https://registry.yarnpkg.com/js-yaml/-/js-yaml-4.0.0.tgz#f426bc0ff4b4051926cd588c71113183409a121f"
2530+
integrity sha512-pqon0s+4ScYUvX30wxQi3PogGFAlUyH0awepWvwkj4jD4v+ova3RiYw8bmA6x2rDrEaj8i/oWKoRxpVNW+Re8Q==
2531+
dependencies:
2532+
argparse "^2.0.1"
2533+
25222534
jsesc@^2.5.1:
25232535
version "2.5.2"
25242536
resolved "https://registry.yarnpkg.com/jsesc/-/jsesc-2.5.2.tgz#80564d2e483dacf6e8ef209650a67df3f0c283a4"

0 commit comments

Comments
 (0)