From 65393f7fc82e6db28302241aeeccf9531c751cc7 Mon Sep 17 00:00:00 2001 From: Matt Russell <russell.d.matt@gmail.com> Date: Wed, 17 Oct 2018 15:24:45 -0700 Subject: [PATCH 1/6] added the ability to specify an id for a code block --- .gitignore | 1 + package-lock.json | 2 +- src/State.re | 37 +++++++++++++++++++++--------------- src/examples/CodeSnippets.re | 25 ++++++++++++++---------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index b739eeb..6cc0396 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ log_types.txt *.install _build static/codemirror-5.36.0 +/docs/ diff --git a/package-lock.json b/package-lock.json index bf6fdbc..6cce1ff 100644 --- a/package-lock.json +++ b/package-lock.json @@ -43,7 +43,7 @@ "integrity": "sha512-JIkwFOvGIqd9SLs9vwI6YvQTHCNjg091LBulGjtY14DfzhoWT+feLk1AXIisG6yOo5v2ya5vqmSStureYA2nyA==" }, "pack.re": { - "version": "github:jaredly/pack.re#826d6761f0de88502027058a830cd67b0f3ab99d", + "version": "github:jaredly/pack.re#f7373039dfd26bc0d05ec3b9e5c09949d47b932f", "requires": { "minimist.re": "0.0.2", "reason-cli-tools": "1.0.4", diff --git a/src/State.re b/src/State.re index 02ad338..1959b9d 100644 --- a/src/State.re +++ b/src/State.re @@ -16,21 +16,26 @@ type bucklescriptOptions = { packageJsonName: string, }; -let showBucklescriptOptions = ({ - packageRoot, - bsRoot, - refmt, - version, - browserCompilerPath, - silentFailures, - tmp, - compiledDependencyDirectories, - packageJsonName, -}) => Printf.sprintf({|packgeRoot: %s -bsRoot: %s -compiledDependencyDirectories: -%s -|}, packageRoot, bsRoot, +let showBucklescriptOptions = + ( + { + packageRoot, + bsRoot, + refmt, + version, + browserCompilerPath, + silentFailures, + tmp, + compiledDependencyDirectories, + packageJsonName, + } +) => + Printf.sprintf( + {|packgeRoot: %s + bsRoot: %s + compiledDependencyDirectories: + %s + |}, packageRoot, bsRoot, compiledDependencyDirectories |> List.map(((cmt, js)) => " " ++ cmt ++ " : " ++ js) |> String.concat("\n") ); @@ -88,6 +93,7 @@ module Model = { /* openModule: bool, */ sharedAs: option(string), uses: list(string), + id: option(string), }; let defaultOptions = { @@ -104,6 +110,7 @@ module Model = { }, sharedAs: None, uses: [], + id: None, }; /* This doesn't apply if I only want to parse */ diff --git a/src/examples/CodeSnippets.re b/src/examples/CodeSnippets.re index 99ec0c6..b2552ef 100644 --- a/src/examples/CodeSnippets.re +++ b/src/examples/CodeSnippets.re @@ -51,16 +51,19 @@ let parseCodeOptions = (lang, defaultOptions) => { | None => switch (matchOption(item, "prefix")) { | Some(content) => {...options, codeDisplay: {...options.codeDisplay, prefix: int_of_string(content)}} | None => switch (matchOption(item, "suffix")) { - | Some(content) => {...options, codeDisplay: {...options.codeDisplay, suffix: int_of_string(content)}} - | None => { - if (parts == [item]) { - {...options, lang: OtherLang(item)} - } else { - print_endline(Printf.sprintf("Unexpected code option: %S. Assuming it's text", item)); - {...options, lang: Txt} - } + | Some(content) => {...options, codeDisplay: {...options.codeDisplay, suffix: int_of_string(content)}} + | None => switch (matchOption(item, "id")) { + | Some(id) => {...options, id: Some(id) } + | None => { + if (parts == [item]) { + {...options, lang: OtherLang(item)} + } else { + print_endline(Printf.sprintf("Unexpected code option: %S. Assuming it's text", item)); + {...options, lang: Txt} + } + } + } } - } } } @@ -111,6 +114,7 @@ let highlight = (~editingEnabled, id, content, options, status, bundle) => { let (pre, _, code, post, _) = CodeHighlight.codeSections(content); (pre, html(code), post) } + | Some(cmt) => CodeHighlight.highlight(content, cmt) }; @@ -142,13 +146,14 @@ let highlight = (~editingEnabled, id, content, options, status, bundle) => { sprintf( {|<div class='code-block' data-block-syntax=%S> %s - <pre class='code' data-block-id='%s' id='block-%s'><code>%s</code></pre> + <pre %s class='code' data-block-id='%s' id='block-%s'><code>%s</code></pre> %s %s %s </div>|}, syntax, preCode, + (switch (options.id) { | None => "" | Some(id) => sprintf("id='%s'", id) }), id, id, code, From 0f6d321d0265fb5b41841a38fb91b2b2ab63ed80 Mon Sep 17 00:00:00 2001 From: Matt Russell <russell.d.matt@gmail.com> Date: Wed, 17 Oct 2018 15:36:51 -0700 Subject: [PATCH 2/6] can specify classes for code blocks --- src/State.re | 2 ++ src/examples/CodeSnippets.re | 44 +++++++++++++++++++++--------------- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/src/State.re b/src/State.re index 1959b9d..77ea9d8 100644 --- a/src/State.re +++ b/src/State.re @@ -94,6 +94,7 @@ module Model = { sharedAs: option(string), uses: list(string), id: option(string), + classes: list(string), }; let defaultOptions = { @@ -111,6 +112,7 @@ module Model = { sharedAs: None, uses: [], id: None, + classes: [], }; /* This doesn't apply if I only want to parse */ diff --git a/src/examples/CodeSnippets.re b/src/examples/CodeSnippets.re index b2552ef..49c56f1 100644 --- a/src/examples/CodeSnippets.re +++ b/src/examples/CodeSnippets.re @@ -44,32 +44,39 @@ let parseCodeOptions = (lang, defaultOptions) => { | "ocaml" | "ml" => {...options, lang: OCaml, inferred: false} | "txt" | "md" => {...options, lang: Txt} | _ => { - switch (matchOption(item, "shared")) { - | Some(name) => {...options, sharedAs: Some(name)} - | None => switch (matchOption(item, "use")) { - | Some(name) => {...options, uses: [name, ...options.uses]} - | None => switch (matchOption(item, "prefix")) { - | Some(content) => {...options, codeDisplay: {...options.codeDisplay, prefix: int_of_string(content)}} - | None => switch (matchOption(item, "suffix")) { + switch (matchOption(item, "shared")) { + | Some(name) => {...options, sharedAs: Some(name)} + | None => + switch (matchOption(item, "use")) { + | Some(name) => {...options, uses: [name, ...options.uses]} + | None => + switch (matchOption(item, "prefix")) { + | Some(content) => {...options, codeDisplay: {...options.codeDisplay, prefix: int_of_string(content)}} + | None => + switch (matchOption(item, "suffix")) { | Some(content) => {...options, codeDisplay: {...options.codeDisplay, suffix: int_of_string(content)}} - | None => switch (matchOption(item, "id")) { - | Some(id) => {...options, id: Some(id) } - | None => { - if (parts == [item]) { - {...options, lang: OtherLang(item)} - } else { + | None => + switch (matchOption(item, "id")) { + | Some(id) => {...options, id: Some(id) } + | None => + switch (matchOption(item, "class")) { + | Some(class_) => {...options, classes: [class_, ...options.classes]} + | None => + if (parts == [item]) {{...options, lang: OtherLang(item)}} + else { print_endline(Printf.sprintf("Unexpected code option: %S. Assuming it's text", item)); {...options, lang: Txt} } - } + } } + } } - + } } } - } - } + } + }, defaultOptions, parts); if (options.lang == Reason || options.lang == OCaml) { Some(options) @@ -146,7 +153,7 @@ let highlight = (~editingEnabled, id, content, options, status, bundle) => { sprintf( {|<div class='code-block' data-block-syntax=%S> %s - <pre %s class='code' data-block-id='%s' id='block-%s'><code>%s</code></pre> + <pre %s class='%s' data-block-id='%s' id='block-%s'><code>%s</code></pre> %s %s %s @@ -154,6 +161,7 @@ let highlight = (~editingEnabled, id, content, options, status, bundle) => { syntax, preCode, (switch (options.id) { | None => "" | Some(id) => sprintf("id='%s'", id) }), + (["code", ...options.classes] |> String.concat(" ")), id, id, code, From 23c69e5c8fc2f9f0087177ff077573b68e42ab93 Mon Sep 17 00:00:00 2001 From: Matt Russell <russell.d.matt@gmail.com> Date: Wed, 17 Oct 2018 15:55:21 -0700 Subject: [PATCH 3/6] revert that change --- .gitignore | 1 - 1 file changed, 1 deletion(-) diff --git a/.gitignore b/.gitignore index 6cc0396..b739eeb 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,3 @@ log_types.txt *.install _build static/codemirror-5.36.0 -/docs/ From 059dde2fb0660bb8b1f144b140daca0a1a94387b Mon Sep 17 00:00:00 2001 From: Matt Russell <russell.d.matt@gmail.com> Date: Wed, 17 Oct 2018 15:56:12 -0700 Subject: [PATCH 4/6] revert that change --- src/State.re | 35 +++++++++++++++-------------------- 1 file changed, 15 insertions(+), 20 deletions(-) diff --git a/src/State.re b/src/State.re index 77ea9d8..eddc835 100644 --- a/src/State.re +++ b/src/State.re @@ -16,26 +16,21 @@ type bucklescriptOptions = { packageJsonName: string, }; -let showBucklescriptOptions = - ( - { - packageRoot, - bsRoot, - refmt, - version, - browserCompilerPath, - silentFailures, - tmp, - compiledDependencyDirectories, - packageJsonName, - } -) => - Printf.sprintf( - {|packgeRoot: %s - bsRoot: %s - compiledDependencyDirectories: - %s - |}, packageRoot, bsRoot, +let showBucklescriptOptions = ({ + packageRoot, + bsRoot, + refmt, + version, + browserCompilerPath, + silentFailures, + tmp, + compiledDependencyDirectories, + packageJsonName, +}) => Printf.sprintf({|packgeRoot: %s +bsRoot: %s +compiledDependencyDirectories: +%s +|}, packageRoot, bsRoot, compiledDependencyDirectories |> List.map(((cmt, js)) => " " ++ cmt ++ " : " ++ js) |> String.concat("\n") ); From 755b9517495324d1aba0b363da422d600eaa27f2 Mon Sep 17 00:00:00 2001 From: Matt Russell <russell.d.matt@gmail.com> Date: Wed, 17 Oct 2018 15:57:28 -0700 Subject: [PATCH 5/6] manual formatting since refmt isnt working --- src/examples/CodeSnippets.re | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/examples/CodeSnippets.re b/src/examples/CodeSnippets.re index 49c56f1..ba334c6 100644 --- a/src/examples/CodeSnippets.re +++ b/src/examples/CodeSnippets.re @@ -62,11 +62,12 @@ let parseCodeOptions = (lang, defaultOptions) => { switch (matchOption(item, "class")) { | Some(class_) => {...options, classes: [class_, ...options.classes]} | None => - if (parts == [item]) {{...options, lang: OtherLang(item)}} - else { - print_endline(Printf.sprintf("Unexpected code option: %S. Assuming it's text", item)); - {...options, lang: Txt} - } + if (parts == [item]) { + {...options, lang: OtherLang(item)} + } else { + print_endline(Printf.sprintf("Unexpected code option: %S. Assuming it's text", item)); + {...options, lang: Txt} + } } } } From 24be039ae47c3fc05c37e59d058bbbe7f846773a Mon Sep 17 00:00:00 2001 From: Matt Russell <russell.d.matt@gmail.com> Date: Wed, 17 Oct 2018 16:10:27 -0700 Subject: [PATCH 6/6] moved where we put id so that it could be unique --- src/CompileCode.re | 9 ++++++++- src/examples/CodeSnippets.re | 3 +-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/CompileCode.re b/src/CompileCode.re index ab6c05c..c946435 100644 --- a/src/CompileCode.re +++ b/src/CompileCode.re @@ -126,5 +126,12 @@ let block = ( bundle ); - {State.Model.langLine, html: html ++ htmlAlt, raw, page, filePath: name, compilationResult} + let html = + Printf.(sprintf("<div %s class='code-block-pair'>", + (switch (options.id) { | None => "" | Some(id) => sprintf("id='%s'", id) }))) + ++ html + ++ htmlAlt + ++ "</div>"; + + {State.Model.langLine, html, raw, page, filePath: name, compilationResult} }; diff --git a/src/examples/CodeSnippets.re b/src/examples/CodeSnippets.re index ba334c6..dee290d 100644 --- a/src/examples/CodeSnippets.re +++ b/src/examples/CodeSnippets.re @@ -154,14 +154,13 @@ let highlight = (~editingEnabled, id, content, options, status, bundle) => { sprintf( {|<div class='code-block' data-block-syntax=%S> %s - <pre %s class='%s' data-block-id='%s' id='block-%s'><code>%s</code></pre> + <pre class='%s' data-block-id='%s' id='block-%s'><code>%s</code></pre> %s %s %s </div>|}, syntax, preCode, - (switch (options.id) { | None => "" | Some(id) => sprintf("id='%s'", id) }), (["code", ...options.classes] |> String.concat(" ")), id, id,