Skip to content

Commit

Permalink
v0.13.0: built-in syntax highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
mDuo13 committed Sep 4, 2020
1 parent c611118 commit adab9d1
Show file tree
Hide file tree
Showing 31 changed files with 493 additions and 347 deletions.
3 changes: 3 additions & 0 deletions dactyl/default-config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ preprocessor_allow_undefined: true
## the input md files.
flatten_default_html_paths: true

## Set this to true to disable Dactyl's built-in syntax highlighting
no_highlighting: false

## Default templates.
default_template: doc.html
default_pdf_template: simple.html
Expand Down
11 changes: 8 additions & 3 deletions dactyl/page.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,14 @@ def html_content(self, context, regen=False, save=True):

if md:
logger.info("... parsing markdown...")
html = markdown(md, extensions=["markdown.extensions.extra",
"markdown.extensions.sane_lists",
"codehilite"])
extensions = ["markdown.extensions.extra",
"markdown.extensions.sane_lists"]
no_highlighting = self.config.get("no_highlighting", False)
if "no_highlighting" in self.data:
no_highlighting = self.data["no_highlighting"]
if not no_highlighting:
extensions.append("codehilite")
html = markdown(md, extensions=extensions)
else:
html = ""

Expand Down
73 changes: 73 additions & 0 deletions dactyl/styles/_codehilite.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/* Derived from https://github.com/richleland/pygments-css */

.codehilite {
color: $code-color;
.hll { background-color: #ffffcc }
.c { color: #0099FF; font-style: italic } /* Comment */
.err { color: #AA0000; } /* Error */
.k { color: #006699; font-weight: bold } /* Keyword */
.o { color: #555555 } /* Operator */
.ch { color: #0099FF; font-style: italic } /* Comment.Hashbang */
.cm { color: #0099FF; font-style: italic } /* Comment.Multiline */
.cp { color: #009999 } /* Comment.Preproc */
.cpf { color: #0099FF; font-style: italic } /* Comment.PreprocFile */
.c1 { color: #0099FF; font-style: italic } /* Comment.Single */
.cs { color: #0099FF; font-weight: bold; font-style: italic } /* Comment.Special */
.gd { background-color: #FFCCCC; border: 1px solid #CC0000 } /* Generic.Deleted */
.ge { font-style: italic } /* Generic.Emph */
.gr { color: #FF0000 } /* Generic.Error */
.gh { color: #003300; font-weight: bold } /* Generic.Heading */
.gi { background-color: #CCFFCC; border: 1px solid #00CC00 } /* Generic.Inserted */
.go { color: #AAAAAA } /* Generic.Output */
.gp { color: #000099; font-weight: bold } /* Generic.Prompt */
.gs { font-weight: bold } /* Generic.Strong */
.gu { color: #003300; font-weight: bold } /* Generic.Subheading */
.gt { color: #99CC66 } /* Generic.Traceback */
.kc { color: #006699; font-weight: bold } /* Keyword.Constant */
.kd { color: #006699; font-weight: bold } /* Keyword.Declaration */
.kn { color: #006699; font-weight: bold } /* Keyword.Namespace */
.kp { color: #006699 } /* Keyword.Pseudo */
.kr { color: #006699; font-weight: bold } /* Keyword.Reserved */
.kt { color: #007788; font-weight: bold } /* Keyword.Type */
.m { color: #FF6600 } /* Literal.Number */
.s { color: #CC3300 } /* Literal.String */
.na { color: #330099 } /* Name.Attribute */
.nb { color: #336666 } /* Name.Builtin */
.nc { color: #00AA88; font-weight: bold } /* Name.Class */
.no { color: #336600 } /* Name.Constant */
.nd { color: #9999FF } /* Name.Decorator */
.ni { color: #999999; font-weight: bold } /* Name.Entity */
.ne { color: #CC0000; font-weight: bold } /* Name.Exception */
.nf { color: #CC00FF } /* Name.Function */
.nl { color: #9999FF } /* Name.Label */
.nn { color: #00CCFF; font-weight: bold } /* Name.Namespace */
.nt { color: #330099; font-weight: bold } /* Name.Tag */
.nv { color: #003333 } /* Name.Variable */
.ow { color: #000000; font-weight: bold } /* Operator.Word */
.w { color: #bbbbbb } /* Text.Whitespace */
.mb { color: #FF6600 } /* Literal.Number.Bin */
.mf { color: #FF6600 } /* Literal.Number.Float */
.mh { color: #FF6600 } /* Literal.Number.Hex */
.mi { color: #FF6600 } /* Literal.Number.Integer */
.mo { color: #FF6600 } /* Literal.Number.Oct */
.sa { color: #CC3300 } /* Literal.String.Affix */
.sb { color: #CC3300 } /* Literal.String.Backtick */
.sc { color: #CC3300 } /* Literal.String.Char */
.dl { color: #CC3300 } /* Literal.String.Delimiter */
.sd { color: #CC3300; font-style: italic } /* Literal.String.Doc */
.s2 { color: #CC3300 } /* Literal.String.Double */
.se { color: #CC3300; font-weight: bold } /* Literal.String.Escape */
.sh { color: #CC3300 } /* Literal.String.Heredoc */
.si { color: #AA0000 } /* Literal.String.Interpol */
.sx { color: #CC3300 } /* Literal.String.Other */
.sr { color: #33AAAA } /* Literal.String.Regex */
.s1 { color: #CC3300 } /* Literal.String.Single */
.ss { color: #FFCC33 } /* Literal.String.Symbol */
.bp { color: #336666 } /* Name.Builtin.Pseudo */
.fm { color: #CC00FF } /* Name.Function.Magic */
.vc { color: #003333 } /* Name.Variable.Class */
.vg { color: #003333 } /* Name.Variable.Global */
.vi { color: #003333 } /* Name.Variable.Instance */
.vm { color: #003333 } /* Name.Variable.Magic */
.il { color: #FF6600 } /* Literal.Number.Integer.Long */
}
19 changes: 19 additions & 0 deletions dactyl/styles/_font.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Font Stacks =================================================================
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
font-display: swap;
src: local("Roboto"), url("../font/Roboto-Regular.woff2") format('woff2'), url(https://fonts.gstatic.com/s/roboto/v19/KFOmCnqEu92Fr1Mu4mxK.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 700;
font-display: swap;
src: local('Roboto Bold'), local('Roboto-Bold'), url("../font/Roboto-Bold.woff2") format('woff2'), url(https://fonts.gstatic.com/s/roboto/v19/KFOlCnqEu92Fr1MmWUlfBBc4.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
$font-family-monospace: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
$font-family-sans-serif: -apple-system, system-ui, 'Roboto', sans-serif;
54 changes: 8 additions & 46 deletions dactyl/styles/dactyl.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,6 @@
// source files. You can install these using yarn or npm and the included
// package.json file.

// Font Stacks =================================================================
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 400;
font-display: swap;
src: local("Roboto"), url("../font/Roboto-Regular.woff2") format('woff2'), url(https://fonts.gstatic.com/s/roboto/v19/KFOmCnqEu92Fr1Mu4mxK.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
@font-face {
font-family: 'Roboto';
font-style: normal;
font-weight: 700;
font-display: swap;
src: local('Roboto Bold'), local('Roboto-Bold'), url("../font/Roboto-Bold.woff2") format('woff2'), url(https://fonts.gstatic.com/s/roboto/v19/KFOlCnqEu92Fr1MmWUlfBBc4.woff2) format('woff2');
unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD;
}
$font-family-monospace: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
$font-family-sans-serif: -apple-system, system-ui, 'Roboto', sans-serif;

// Theme Colors ================================================================

$white: #FFFFFF;
Expand All @@ -37,25 +17,18 @@ $gray-700: #4D4D4D;
$gray-800: #333333;
$gray-900: #1A1A1A;
$black: #000000;
//
// $primary: #0000ff; // 100% blue
// $secondary: #404080;
// $light: $gray-200;
// $dark: $black;
// $success: #2BCB96; // light green
// $info: #7699bb; // bluish
// $warning: #D0DB60; // yellowish -- "caution" callout style
// $danger: #AA4949; // wine red -- "warning" callout style

$code-color: $gray-800;
$code-bg-color: $gray-100;
$body-color: $black;
$navbar-light-color: $gray-600;
$breadcrumb-bg: $white;

$navbar-padding-y: 0;
$navbar-nav-link-padding-x: 1rem;

$code-bg-color: $gray-100;
@import "_font.scss"; // webfont and font stack definitions
@import "_codehilite.scss"; // Styles for built-in code syntax highlighting

// Bootstrap v4 ================================================================
@import "node_modules/bootstrap/scss/bootstrap.scss";
Expand Down Expand Up @@ -309,17 +282,18 @@ $code-bg-color: $gray-100;
padding-bottom: 7px;
}

.code_sample pre code {
background-color: $code-bg-color;
}

pre {
overflow: visible;
word-wrap: normal;
}
pre code {
white-space: pre;
border-radius: $border-radius;
padding: 24px;
display: block;
overflow: auto;
max-height: 24em;
border-radius: 4px;
}
pre code, p code {
color: $code-color;
Expand All @@ -328,18 +302,6 @@ $code-bg-color: $gray-100;
p code {
padding: 0.2rem;
}
.code_sample pre {
background: none;
border: none;
border-radius: 0;
}
.code_sample pre code {
overflow: auto;
max-height: 24em;
border-radius: 4px;
display: block;
padding: 24px;
}
.code_sample pre code.expanded {
overflow: visible;
max-height: none;
Expand Down
15 changes: 0 additions & 15 deletions dactyl/templates/doc.html
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
{% extends "base.html" %}
{% block head %}

<!-- syntax highlighting -->
{% if currentpage.highlightcss is undefined %}
{% set highlightcss = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/styles/default.min.css" %}
{% else %}
{% set highlightcss = currentpage.highlightjs %}
{% endif %}
<link rel="stylesheet" href="{{highlightcss}}" />
{% if currentpage.highlightjs is undefined %}
{% set highlightjs = "https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.0.3/highlight.min.js" %}
{% else %}
{% set highlightjs = currentpage.highlightjs %}
{% endif %}
<script src="{{highlightjs}}"></script>

<script>
$(document).ready(function() {
$(".multicode").minitabs();
hljs.initHighlighting();
make_code_expandable();
});
</script>
Expand Down
2 changes: 1 addition & 1 deletion dactyl/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.13.0-a2'
__version__ = '0.13.0'
64 changes: 64 additions & 0 deletions examples/content/code-highlighting.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Code Highlighting

Dactyl automatically adds syntax highlighting to code blocks when it parses Markdown, using the [Pygments](https://pygments.org/)-derived [CodeHilite](https://python-markdown.github.io/extensions/code_hilite/) extension. Parsing the syntax highlighting at compile time like this is faster and less work for readers' computers than in-browser syntax highlighting such as using [highlight.js](https://highlightjs.readthedocs.io/en/latest/api.html). (If you prefer highlight.js's output, though, you can still run it to overwrite Dactyl's syntax highlighting.)

Example of a code segment colored by Dactyl's built-in highlighting:

```py
unacceptable_chars = re.compile(r"[^A-Za-z0-9._ ]+")
whitespace_regex = re.compile(r"\s+")
def slugify(s):
s = re.sub(unacceptable_chars, "", s)
s = re.sub(whitespace_regex, "_", s)
if not s:
s = "_"
return s
```

## Requirements

The highlighting requires a stylesheet to define the colors and styles used. Dactyl's default stylesheet includes an example or you can define your own. You can output one of Pygments' default stylesheets from the commandline as in the following example:

```sh
$ pygmentize -S default -f html -a .codehilite > default.css
```

## Languages

Dactyl's code highlighting supports the same [programming languages that Pygments supports](https://pygments.org/languages/). By default it attempts to auto-detect the language, but you can add a language code to the first line of a fenced code block to specify the language.

Example code:

```js
function slugify(s) {
const unacceptable_chars = /[^A-Za-z0-9._ ]+/
const whitespace_regex = /\s+/
s = s.replace(unacceptable_chars, "")
s = s.replace(whitespace_regex, "_")
s = s.toLowerCase()
if (!s) {
s = "_"
}
return s
}
```

Output:

```js
function slugify(s) {
const unacceptable_chars = /[^A-Za-z0-9._ ]+/
const whitespace_regex = /\s+/
s = s.replace(unacceptable_chars, "")
s = s.replace(whitespace_regex, "_")
s = s.toLowerCase()
if (!s) {
s = "_"
}
return s
}
```

## Disabling

If for some reason you want to turn off syntax highlighting, you can add `no_highlighting: true` to your config file at the global, target, or page level.
File renamed without changes.
11 changes: 11 additions & 0 deletions examples/content/extending/extend-templates.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
template: template-extends.html
related_links:
- with-frontmatter.html
- code-highlighting.html
---
# Template Extension

You can extend or include the base templates built into Dactyl.

For example, this page replaces the right-hand sidebar with a "related pages" module.
28 changes: 28 additions & 0 deletions examples/content/extending/hierarchy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Hierarchy Demo

This page demonstrates how you can use Dactyl's built-in hierarchy features to generate a table of contents automatically.

Source:

```jinja
<ul>
{{"{%"}} macro page_w_children(pg) {{"%}"}}
<li class="dactyl-toc-entry"><a href="{{"{{"}} pg.html {{"}}"}}">{{"{{"}} pg.name {{"}}"}}</a></li>
{{"{%"}} if pg.children {{"%}"}}
<ul>
{{"{%"}} for child in pg.children {{"%}"}}
{{"{{"}} page_w_children(child) {{"}}"}}
{{"{%"}} endfor {{"%}"}}
</ul>
{{"{%"}} endif {{"%}"}}
{{"{%"}} endmacro {{"%}"}}
{{"{%"}} for page in pages {{"%}"}}
{{"{%"}} if page.parent is undefined {{"%}"}}
{{"{{"}} page_w_children(page) {{"}}"}}
{{"{%"}} endif {{"%}"}}
{{"{%"}} endfor {{"%}"}}
</ul>
```

Results:
File renamed without changes.
3 changes: 3 additions & 0 deletions examples/content/extending/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Extending Dactyl

Dactyl is designed to be customized and extended to each individual project's needs. These pages demonstrate some ways you can extend Dactyl.
File renamed without changes.
13 changes: 0 additions & 13 deletions examples/content/extends.md

This file was deleted.

Loading

0 comments on commit adab9d1

Please sign in to comment.