Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

id and classes on code block #23

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/CompileCode.re
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};
4 changes: 4 additions & 0 deletions src/State.re
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ module Model = {
/* openModule: bool, */
sharedAs: option(string),
uses: list(string),
id: option(string),
classes: list(string),
};

let defaultOptions = {
Expand All @@ -104,6 +106,8 @@ module Model = {
},
sharedAs: None,
uses: [],
id: None,
classes: [],
};

/* This doesn't apply if I only want to parse */
Expand Down
49 changes: 31 additions & 18 deletions src/examples/CodeSnippets.re
Original file line number Diff line number Diff line change
Expand Up @@ -44,29 +44,40 @@ 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")) {
| 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}
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 =>
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)
Expand Down Expand Up @@ -111,6 +122,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)
};

Expand Down Expand Up @@ -142,13 +154,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 class='%s' data-block-id='%s' id='block-%s'><code>%s</code></pre>
%s
%s
%s
</div>|},
syntax,
preCode,
(["code", ...options.classes] |> String.concat(" ")),
id,
id,
code,
Expand Down