Skip to content

Commit

Permalink
feat: Improve syntax highlighting. (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Sneeringer authored Sep 3, 2020
1 parent 7cb22cb commit d5cdee3
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@
// This applies special CSS classes to protobuf patterns that are overlooked
// or insufficiently distinguished by the usual Pygments/Rouge syntax parsers.
$.when($.ready).then(() => {
// -------------
// -- Generic --
// -------------
// De-emphasize semicolon punctuation across the board.
$('.highlight .p:contains(;)').each((_, el) => {
el = $(el);
if (el.text() === ';') {
el.addClass('semi');
} else if (el.text().match(/[;]$/)) {
let txt = el.text();
el.text(txt.substring(0, txt.length - 1));
$('<span class="p semi">;</span>').insertAfter(el);
}
});

// --------------
// -- Protobuf --
// --------------
// RPCs are always followed by three names (the RPC name, the request object,
// and the response object) -- designate those appropriately.
$('.language-proto .k:contains(rpc)').each((_, el) => {
$(el)
.nextAll('.n')
.slice(1, 3)
.addClass('nc');
$(el).nextAll('.n').slice(1, 3).addClass('nc');
});

// Designate message names as such when using them to delcare fields.
$('.language-proto .n + .na + .o:contains(=)')
.prev()
.prev()
.addClass('nc');
$('.language-proto .n + .na + .o:contains(=)').prev().prev().addClass('nc');

// Colons in protocol buffers always come immediately after property keys.
$('.language-proto .n + .o:contains(:)')
Expand All @@ -39,9 +51,20 @@ $.when($.ready).then(() => {

// The option keyword is always followed by the annotation name.
$('.language-proto .k:contains(option)').each((_, el) => {
$(el)
.nextAll('.n')
.eq(0)
.addClass('protobuf-annotation');
$(el).nextAll('.n').eq(0).addClass('protobuf-annotation');
});

// ----------------
// -- TypeScript --
// ----------------
// Interfaces are always followed by what are effectively class names.
for (let lang of ['typescript', 'ts']) {
$(`.language-${lang} .kr:contains(interface)`).each((_, el) => {
if ($(el).nextAll('.nb').length > 0) {
$(el).nextAll('.nb').slice(0, 1).removeClass('nb').addClass('nc');
} else {
$(el).nextAll('.nx').slice(0, 1).removeClass('nx').addClass('nc');
}
});
}
});
9 changes: 8 additions & 1 deletion aip_site/support/scss/imports/syntax.scss
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ code,
}

// Name: Key (.nk)
.nk {
// Name: Other (.nx)
.nk,
.nx {
color: #6a1b9a;
}

Expand Down Expand Up @@ -130,6 +132,11 @@ code,
@extend .k;
}

// Punctuation: Semicolons
.p.semi {
color: $glue-grey-400;
}

// --------
// Generics
// --------
Expand Down
2 changes: 1 addition & 1 deletion aip_site/support/templates/aip.html.j2
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ AIP-{{ aip.id }}: {{ aip.title }}
{%- endblock %}

{% block js %}
<script type="text/javascript" src="{{ site.relative_uri }}/assets/js/proto-syntax.js"></script>
<script type="text/javascript" src="{{ site.relative_uri }}/assets/js/syntax.js"></script>
{% if 'js_scripts' in aip.config -%}
{% for js_script in aip.config.js_scripts -%}
<script type="text/javascript" src="{{ site.relative_uri }}{{ js_script }}?v={{ site.revision }}"></script>
Expand Down

0 comments on commit d5cdee3

Please sign in to comment.