Skip to content

Commit

Permalink
Merge pull request #4950 from mkslanc/bibtex-mode
Browse files Browse the repository at this point in the history
BibTeX mode
  • Loading branch information
andrewnester authored Oct 12, 2022
2 parents 059ff71 + ab9e191 commit 0d86f93
Show file tree
Hide file tree
Showing 11 changed files with 247 additions and 21 deletions.
34 changes: 34 additions & 0 deletions demo/kitchen-sink/docs/bibtex.bib
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
@Comment{
@Book{tes03,
author = {John Smith},
title = {Cooking},
publisher = {Culinary Expert},
year = 1890
}
}
@String(mar = "march")

@Book{sweig-42,
Author = { Stefan Sweig },
title = { The impossible book },
publisher = { Dead Poet Society},
year = 1942,
month = mar
}

@String {firstname = "John"}
@String {lastname = "Smith"}
@String {email = firstname # "." # lastname # "@imag.en"}

@Article{key01,
title = { The history of @ sign }
}

Everything " " outside {entries} is treated as comment in BibTeX.
@Article{key03,
title = "A {bunch {of} braces {in}} title"
}

@preamble {"This bibliography was generated on \today"}
1 change: 1 addition & 0 deletions src/ext/modelist.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ var supportedModes = {
Assembly_x86:["asm|a"],
AutoHotKey: ["ahk"],
BatchFile: ["bat|cmd"],
BibTeX: ["bib"],
C_Cpp: ["cpp|c|cc|cxx|h|hh|hpp|ino"],
C9Search: ["c9search_results"],
Cirru: ["cirru|cr"],
Expand Down
18 changes: 18 additions & 0 deletions src/mode/bibtex.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"use strict";

var oop = require("../lib/oop");
var TextMode = require("./text").Mode;
var BibTeXHighlightRules = require("./bibtex_highlight_rules").BibTeXHighlightRules;
var FoldMode = require("./folding/cstyle").FoldMode;

var Mode = function() {
this.HighlightRules = BibTeXHighlightRules;
this.foldingRules = new FoldMode();
};
oop.inherits(Mode, TextMode);

(function() {
this.$id = "ace/mode/bibtex";
}).call(Mode.prototype);

exports.Mode = Mode;
184 changes: 184 additions & 0 deletions src/mode/bibtex_highlight_rules.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
"use strict";

var oop = require("../lib/oop");
var TextHighlightRules = require("./text_highlight_rules").TextHighlightRules;

var BibTeXHighlightRules = function() {
// regexp must not have capturing parentheses. Use (?:) instead.
// regexps are ordered -> the first match is used

this.$rules = {
start: [
{
token: "comment",
regex: /@Comment\{/,
stateName: "bibtexComment",
push: [
{
token: "comment",
regex: /}/,
next: "pop"
}, {
token: "comment",
regex: /\{/,
push: "bibtexComment"
}, {
defaultToken: "comment"
}
]
}, {
token: [
"keyword", "text", "paren.lparen", "text", "variable", "text", "keyword.operator"
],
regex: /(@String)(\s*)(\{)(\s*)([a-zA-Z]*)(\s*)(=)/,
push: [
{
token: "paren.rparen",
regex: /\}/,
next: "pop"
}, {
include: "#misc"
}, {
defaultToken: "text"
}
]
}, {
token: [
"keyword", "text", "paren.lparen", "text", "variable", "text", "keyword.operator"
],
regex: /(@String)(\s*)(\()(\s*)([a-zA-Z]*)(\s*)(=)/,
push: [
{
token: "paren.rparen",
regex: /\)/,
next: "pop"
}, {
include: "#misc"
}, {
defaultToken: "text"
}
]
}, {
token: [
"keyword", "text", "paren.lparen"
],
regex: /(@preamble)(\s*)(\()/,
push: [
{
token: "paren.rparen",
regex: /\)/,
next: "pop"
}, {
include: "#misc"
}, {
defaultToken: "text"
}
]
}, {
token: [
"keyword", "text", "paren.lparen"
],
regex: /(@preamble)(\s*)(\{)/,
push: [
{
token: "paren.rparen",
regex: /\}/,
next: "pop"
}, {
include: "#misc"
}, {
defaultToken: "text"
}
]
}, {
token: [
"keyword", "text", "paren.lparen", "text", "support.class"
],
regex: /(@[a-zA-Z]+)(\s*)(\{)(\s*)([\w-]+)/,
push: [
{
token: "paren.rparen",
regex: /\}/,
next: "pop"
}, {
token: [
"variable", "text", "keyword.operator"
],
regex: /([a-zA-Z0-9\!\$\&\*\+\-\.\/\:\;\<\>\?\[\]\^\_\`\|]+)(\s*)(=)/,
push: [
{
token: "text",
regex: /(?=[,}])/,
next: "pop"
}, {
include: "#misc"
}, {
include: "#integer"
}, {
defaultToken: "text"
}
]
}, {
token: "punctuation",
regex: /,/
}, {
defaultToken: "text"
}
]
}, {
defaultToken: "comment"
}
],
"#integer": [
{
token: "constant.numeric.bibtex",
regex: /\d+/
}
],
"#misc": [
{
token: "string",
regex: /"/,
push: "#string_quotes"
}, {
token: "paren.lparen",
regex: /\{/,
push: "#string_braces"
}, {
token: "keyword.operator",
regex: /#/
}
],
"#string_braces": [
{
token: "paren.rparen",
regex: /\}/,
next: "pop"
}, {
token: "invalid.illegal",
regex: /@/
}, {
include: "#misc"
}, {
defaultToken: "string"
}
],
"#string_quotes": [
{
token: "string",
regex: /"/,
next: "pop"
}, {
include: "#misc"
}, {
defaultToken: "string"
}
]
};

this.normalizeRules();
};

oop.inherits(BibTeXHighlightRules, TextHighlightRules);

exports.BibTeXHighlightRules = BibTeXHighlightRules;
6 changes: 3 additions & 3 deletions tool/add_mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function main(displayName, extRe) {

/** mode **/
var template = fs.readFileSync(__dirname + "/templates/mode.js", "utf8");
var modePath = lib.AceLib + "ace/mode/" + name + ".js";
var modePath = lib.AceRoot + "src/mode/" + name + ".js";
var text = lib.fillTemplate(template, {
languageHighlightFilename: name,
languagename: name,
Expand All @@ -27,7 +27,7 @@ function main(displayName, extRe) {

/** highlight rules **/
template = fs.readFileSync(__dirname + "/templates/highlight_rules.js", "utf8");
var hlPath = lib.AceLib + "ace/mode/" + name + "_highlight_rules.js";
var hlPath = lib.AceRoot + "src/mode/" + name + "_highlight_rules.js";
template = template.replace(/\/\* THIS[\s\S]*?\*{3}\/\s*/, "");
text = lib.fillTemplate(template, {
language: name,
Expand Down Expand Up @@ -61,7 +61,7 @@ function main(displayName, extRe) {
console.log("Created snippets file at: " + path.normalize(snipetsPath));

/** modelist **/
var modelistPath = lib.AceLib + "ace/ext/modelist.js";
var modelistPath = lib.AceRoot + "src/ext/modelist.js";
var modelist = fs.readFileSync(modelistPath, "utf8").replace(/\r\n?/g, "\n");
modelist = modelist.replace(/(supportedModes = {\n)([\s\S]*?)(\n^};)/m, function(_, m1, m2, m3) {
var langs = m2.split(/,\n/);
Expand Down
4 changes: 1 addition & 3 deletions tool/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ var http = require("http");
exports.parsePlist = function(xmlOrJSON, callback) {
var json;
if (xmlOrJSON[0] == "<") {
plist.parseString(xmlOrJSON, function(_, result) {
json = result[0];
});
json = plist.parse(xmlOrJSON);
} else {
try {
xmlOrJSON = xmlOrJSON.replace(
Expand Down
4 changes: 1 addition & 3 deletions tool/templates/highlight_rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
* fileTypes *
****************************************************************************************/

define(function(require, exports, module) {
"use strict";

var oop = require("../lib/oop");
Expand All @@ -54,5 +53,4 @@ var %language%HighlightRules = function() {

oop.inherits(%language%HighlightRules, TextHighlightRules);

exports.%language%HighlightRules = %language%HighlightRules;
});
exports.%language%HighlightRules = %language%HighlightRules;
6 changes: 2 additions & 4 deletions tool/templates/mode.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
THIS FILE WAS AUTOGENERATED BY mode.tmpl.js
*/

define(function(require, exports, module) {
"use strict";

var oop = require("../lib/oop");
Expand All @@ -51,8 +50,7 @@ oop.inherits(Mode, TextMode);
// this.lineCommentStart = "%lineCommentStart%";
// this.blockComment = {start: "%blockCommentStart%", end: "%blockCommentEnd%"};
// Extra logic goes here.
this.$id = "ace/mode/%languageHighlightFilename%"
this.$id = "ace/mode/%languageHighlightFilename%";
}).call(Mode.prototype);

exports.Mode = Mode;
});
exports.Mode = Mode;
5 changes: 1 addition & 4 deletions tool/templates/snippets.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
define(function(require, exports, module) {
"use strict";

exports.snippetText = require("../requirejs/text!./%modeName%.snippets");
exports.scope = "%modeName%";

});
exports.scope = "%modeName%";
2 changes: 0 additions & 2 deletions tool/templates/theme.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,10 @@
*
* ***** END LICENSE BLOCK ***** */

define(function(require, exports, module) {

exports.isDark = %isDark%;
exports.cssClass = "%cssClass%";
exports.cssText = %css%;

var dom = require("../lib/dom");
dom.importCssString(exports.cssText, exports.cssClass);
});
4 changes: 2 additions & 2 deletions tool/tmlanguage.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,8 @@ function convertTmLanguage(name, langStr) {

require("./add_mode")(languageNameSanitized, (language.fileTypes || []).join("|"));

var highlighterFile = pathlib.normalize(lib.AceLib + "ace/mode/" + highlighterFilename + "_highlight_rules.js");
var modeFile = pathlib.normalize(lib.AceLib + "ace/mode/" + highlighterFilename + ".js");
var highlighterFile = pathlib.normalize(lib.AceRoot + "src/mode/" + highlighterFilename + "_highlight_rules.js");
var modeFile = pathlib.normalize(lib.AceRoot + "src/mode/" + highlighterFilename + ".js");

if (devMode) {
console.log(util.inspect(language.patterns, false, 4));
Expand Down

0 comments on commit 0d86f93

Please sign in to comment.