Skip to content

Commit

Permalink
Support removing ALLCAPS while keeping volume fields as they are (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlamingTempura committed Nov 18, 2022
1 parent d793a6e commit cc94406
Show file tree
Hide file tree
Showing 12 changed files with 49 additions and 22 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ bibtex-tidy references.bib
--drop-all-caps
Where values are all caps, make them title case. For example, {JOURNAL OF
TEA} will become {Journal of Tea}.
TEA} will become {Journal of Tea}. Roman numerals will be left unchanged.
--escape, --no-escape
Escape special characters, such as umlaut. This ensures correct typesetting
Expand Down
1 change: 1 addition & 0 deletions bibtex-tidy.0
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ OPTIONS
--drop-all-caps
Where values are all caps, make them title case. For
example, {JOURNAL OF TEA} will become {Journal of Tea}.
Roman numerals will be left unchanged.

--escape, --no-escape
Escape special characters, such as umlaut. This ensures
Expand Down
2 changes: 1 addition & 1 deletion bibtex-tidy.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export type BibTeXTidyOptions = {
/**
* Drop all caps
*
* Where values are all caps, make them title case. For example, {JOURNAL OF TEA} will become {Journal of Tea}.
* Where values are all caps, make them title case. For example, {JOURNAL OF TEA} will become {Journal of Tea}. Roman numerals will be left unchanged.
*/
dropAllCaps?: boolean;
/**
Expand Down
11 changes: 9 additions & 2 deletions bibtex-tidy.js

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

17 changes: 11 additions & 6 deletions bin/bibtex-tidy

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

11 changes: 9 additions & 2 deletions docs/bundle.js

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

2 changes: 1 addition & 1 deletion src/__generated__/manPage.ts

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

2 changes: 1 addition & 1 deletion src/__generated__/optionsType.ts

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

2 changes: 1 addition & 1 deletion src/optionDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export const optionDefinitions: OptionDefinition[] = [
toCLI: (val) => (val ? '--drop-all-caps' : undefined),
title: 'Drop all caps',
description: [
'Where values are all caps, make them title case. For example, {JOURNAL OF TEA} will become {Journal of Tea}.',
'Where values are all caps, make them title case. For example, {JOURNAL OF TEA} will become {Journal of Tea}. Roman numerals will be left unchanged.',
],
type: 'boolean',
defaultValue: false,
Expand Down
13 changes: 9 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,15 @@ export function escapeSpecialCharacters(str: string): string {
}

export function titleCase(str: string): string {
return str.replace(
/(\w)(\S*)/g,
(_, first, rest) => first.toLocaleUpperCase() + rest.toLocaleLowerCase()
);
return str.replace(/(\w)(\S*)/g, (_, first, rest) => {
const word = first + rest;
if (isRomanNumeral(word)) return word;
return first.toLocaleUpperCase() + rest.toLocaleLowerCase();
});
}

function isRomanNumeral(str: string): boolean {
return /^M{0,4}(CM|CD|D?C{0,3})(XC|XL|L?X{0,3})(IX|IV|V?I{0,3})$/.test(str);
}

/**
Expand Down
6 changes: 4 additions & 2 deletions test/drop-all-caps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ const input = bibtex`
month=dec,
title={{Quantum somethings}},journal={Journal of {B}lah},
booktitle={JOURNAL OF SOMETHINGS},
url={http://example.com/something_with/unusual?characters=faoo#bar}
url={http://example.com/something_with/unusual?characters=faoo#bar},
volume = {VOLUME VII}
}`;

const output = bibtex`
Expand All @@ -20,7 +21,8 @@ const output = bibtex`
title = {{Quantum somethings}},
journal = {Journal of {B}lah},
booktitle = {Journal Of Somethings},
url = {http://example.com/something\_with/unusual?characters=faoo\#bar}
url = {http://example.com/something\_with/unusual?characters=faoo\#bar},
volume = {Volume VII}
}
`;

Expand Down
2 changes: 1 addition & 1 deletion test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { readdirSync } from 'fs';

readdirSync(__dirname)
.filter((file) => file.endsWith('.spec.ts'))
.filter((file) => file.endsWith('drop-all-caps.spec.ts'))
.forEach((file) => require(`./${file}`));

0 comments on commit cc94406

Please sign in to comment.