Skip to content

Instantly share code, notes, and snippets.

@mimoo
Last active December 5, 2019 01:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mimoo/a9e28fc565dddb369477e8bd34e6a1ae to your computer and use it in GitHub Desktop.
Save mimoo/a9e28fc565dddb369477e8bd34e6a1ae to your computer and use it in GitHub Desktop.
HLJS browser shims for different languages (to be used with gitraw)
/*
Language: Protocol Buffers
Author: Dan Tao <daniel.tao@gmail.com>
Description: Protocol buffer message definition format
Website: https://developers.google.com/protocol-buffers/docs/proto3
Category: protocols
*/
var module = module ? module : {}; // shim for browser use
function hljsDefineProto(hljs) {
return {
keywords: {
keyword: 'package import option optional required repeated group oneof',
built_in: 'double float int32 int64 uint32 uint64 sint32 sint64 ' +
'fixed32 fixed64 sfixed32 sfixed64 bool string bytes',
literal: 'true false'
},
contains: [
hljs.QUOTE_STRING_MODE,
hljs.NUMBER_MODE,
hljs.C_LINE_COMMENT_MODE,
{
className: 'class',
beginKeywords: 'message enum service', end: /\{/,
illegal: /\n/,
contains: [
hljs.inherit(hljs.TITLE_MODE, {
starts: { endsWithParent: true, excludeEnd: true } // hack: eating everything after the first title
})
]
},
{
className: 'function',
beginKeywords: 'rpc',
end: /;/, excludeEnd: true,
keywords: 'rpc returns'
},
{
begin: /^\s*[A-Z_]+/,
end: /\s*=/, excludeEnd: true
}
]
};
}
module.exports = function (hljs) {
hljs.registerLanguage('proto', hljsDefineProto);
};
module.exports.definer = hljsDefineProto;
/*
Language: Protocol Buffers
Author: Dan Tao <daniel.tao@gmail.com>
Description: Protocol buffer message definition format
Website: https://developers.google.com/protocol-buffers/docs/proto3
Category: protocols
*/
var module = module ? module : {}; // shim for browser use
function hljsDefineRust(hljs) {
var NUM_SUFFIX = '([ui](8|16|32|64|128|size)|f(32|64))\?';
var KEYWORDS =
'abstract as async await become box break const continue crate do dyn ' +
'else enum extern false final fn for if impl in let loop macro match mod ' +
'move mut override priv pub ref return self Self static struct super ' +
'trait true try type typeof unsafe unsized use virtual where while yield';
var BUILTINS =
// functions
'drop ' +
// types
'i8 i16 i32 i64 i128 isize ' +
'u8 u16 u32 u64 u128 usize ' +
'f32 f64 ' +
'str char bool ' +
'Box Option Result String Vec ' +
// traits
'Copy Send Sized Sync Drop Fn FnMut FnOnce ToOwned Clone Debug ' +
'PartialEq PartialOrd Eq Ord AsRef AsMut Into From Default Iterator ' +
'Extend IntoIterator DoubleEndedIterator ExactSizeIterator ' +
'SliceConcatExt ToString ' +
// macros
'assert! assert_eq! bitflags! bytes! cfg! col! concat! concat_idents! ' +
'debug_assert! debug_assert_eq! env! panic! file! format! format_args! ' +
'include_bin! include_str! line! local_data_key! module_path! ' +
'option_env! print! println! select! stringify! try! unimplemented! ' +
'unreachable! vec! write! writeln! macro_rules! assert_ne! debug_assert_ne!';
return {
aliases: ['rs'],
keywords: {
keyword:
KEYWORDS,
literal:
'true false Some None Ok Err',
built_in:
BUILTINS
},
lexemes: hljs.IDENT_RE + '!?',
illegal: '</',
contains: [
hljs.C_LINE_COMMENT_MODE,
hljs.COMMENT('/\\*', '\\*/', { contains: ['self'] }),
hljs.inherit(hljs.QUOTE_STRING_MODE, { begin: /b?"/, illegal: null }),
{
className: 'string',
variants: [
{ begin: /r(#*)"(.|\n)*?"\1(?!#)/ },
{ begin: /b?'\\?(x\w{2}|u\w{4}|U\w{8}|.)'/ }
]
},
{
className: 'symbol',
begin: /'[a-zA-Z_][a-zA-Z0-9_]*/
},
{
className: 'number',
variants: [
{ begin: '\\b0b([01_]+)' + NUM_SUFFIX },
{ begin: '\\b0o([0-7_]+)' + NUM_SUFFIX },
{ begin: '\\b0x([A-Fa-f0-9_]+)' + NUM_SUFFIX },
{
begin: '\\b(\\d[\\d_]*(\\.[0-9_]+)?([eE][+-]?[0-9_]+)?)' +
NUM_SUFFIX
}
],
relevance: 0
},
{
className: 'function',
beginKeywords: 'fn', end: '(\\(|<)', excludeEnd: true,
contains: [hljs.UNDERSCORE_TITLE_MODE]
},
{
className: 'meta',
begin: '#\\!?\\[', end: '\\]',
contains: [
{
className: 'meta-string',
begin: /"/, end: /"/
}
]
},
{
className: 'class',
beginKeywords: 'type', end: ';',
contains: [
hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, { endsParent: true })
],
illegal: '\\S'
},
{
className: 'class',
beginKeywords: 'trait enum struct union', end: '{',
contains: [
hljs.inherit(hljs.UNDERSCORE_TITLE_MODE, { endsParent: true })
],
illegal: '[\\w\\d]'
},
{
begin: hljs.IDENT_RE + '::',
keywords: { built_in: BUILTINS }
},
{
begin: '->'
}
]
};
}
module.exports = function (hljs) {
hljs.registerLanguage('rust', hljsDefineRust);
};
module.exports.definer = hljsDefineRust;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment