Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bracing for words containing unbraced capital letters #118

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ bibtex-tidy references.bib
Where values are all caps, make them title case. For example, {JOURNAL OF
TEA} will become {Journal of Tea}.

--brace-capital-word
Brace the whole word if a capital letter is not braced.

--escape, --no-escape
Escape special characters, such as umlaut. This ensures correct typesetting
with latex. Enabled by default.
Expand Down
3 changes: 3 additions & 0 deletions bibtex-tidy.0
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ OPTIONS
Where values are all caps, make them title case. For
example, {JOURNAL OF TEA} will become {Journal of Tea}.

--brace-capital-word
Brace the whole word if a capital letter is not braced.

--escape, --no-escape
Escape special characters, such as umlaut. This ensures
correct typesetting with latex. Enabled by default.
Expand Down
6 changes: 6 additions & 0 deletions bibtex-tidy.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@ export declare type BibTeXTidyOptions = {
* Where values are all caps, make them title case. For example, {JOURNAL OF TEA} will become {Journal of Tea}.
*/
dropAllCaps?: boolean;
/**
* Brace words which contain uppercase letters.
*
* Brace the whole word if a capital letter is not braced.
*/
braceCapitalWord?: boolean;
/**
* Escape special characters
*
Expand Down
21 changes: 21 additions & 0 deletions bibtex-tidy.js

Large diffs are not rendered by default.

21 changes: 21 additions & 0 deletions bin/bibtex-tidy

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

22 changes: 22 additions & 0 deletions docs/bundle.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ <h1>BibTeX Tidy</h1>
<input type="checkbox" />
<span class="name"></span>
</label>
<label data-option="braceCapitalWord">
<input type="checkbox" />
<span class="name"></span>
</label>
<label data-option="escape">
<input type="checkbox" />
<span class="name"></span>
Expand Down
1 change: 1 addition & 0 deletions docs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ function getOptions(): Options {
stripComments: options.stripComments.checked,
tidyComments: options.tidyComments.checked,
encodeUrls: options.encodeUrls.checked,
braceCapitalWord: options.braceCapitalWord.checked,
escape: options.escape.checked,
trailingCommas: options.trailingCommas.checked,
removeEmptyFields: options.removeEmptyFields.checked,
Expand Down
3 changes: 3 additions & 0 deletions src/__generated__/manPage.ts

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

6 changes: 6 additions & 0 deletions src/__generated__/optionsType.ts

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

6 changes: 5 additions & 1 deletion src/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
import {
titleCase,
escapeSpecialCharacters,
braceWordsWithCapital,
wrapText,
unwrapText,
addEnclosingBraces,
Expand Down Expand Up @@ -148,6 +149,7 @@ export function formatValue(
align,
stripEnclosingBraces,
dropAllCaps,
braceCapitalWord,
escape,
encodeUrls,
wrap,
Expand Down Expand Up @@ -195,6 +197,9 @@ export function formatValue(
if (nameLowerCase === 'url' && encodeUrls) {
value = escapeURL(value);
}
if (braceCapitalWord) {
value = braceWordsWithCapital(value);
}
// escape special characters like %
if (escape) {
value = escapeSpecialCharacters(value);
Expand All @@ -218,7 +223,6 @@ export function formatValue(
if (type === 'braced' && field.value.concat.length === 1) {
value = value.trim();
}

if (type === 'braced' || curly) {
const lineLength = `${indent}${align}{${value}}`.length;
const multiLine = value.includes('\n\n');
Expand Down
9 changes: 9 additions & 0 deletions src/optionDefinitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,15 @@ export const optionDefinitions: OptionDefinition[] = [
type: 'boolean',
defaultValue: false,
},
{
key: 'braceCapitalWord',
cli: { '--brace-capital-word': true },
toCLI: (val) => (val ? '--brace-capital-word' : undefined),
title: 'Brace words which contain uppercase letters.',
description: ['Brace the whole word if a capital letter is not braced.'],
type: 'boolean',
defaultValue: false,
},
{
key: 'escape',
cli: { '--escape': true, '--no-escape': false },
Expand Down
9 changes: 9 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
import { specialCharacters } from './unicode';

export function braceWordsWithCapital(str: string): string {
return str.replace(
/(?<![\{\p{L}])\p{L}*\p{Lu}\p{L}*(?![\p{L}\}])/gu,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an awesome regexp! Could you add a comment to explain what it does?

(match) => {
return `{${match}}`;
}
);
}

export function escapeSpecialCharacters(str: string): string {
let mathExpressions: string[] = [];

Expand Down