Skip to content

Commit

Permalink
Fix #3472 by finishing implementation of urls in bib as verbatim
Browse files Browse the repository at this point in the history
  • Loading branch information
slideclimb committed Mar 24, 2024
1 parent ff086f2 commit db9fd0d
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/nl/hannahsten/texifyidea/grammar/BibtexLexer.flex
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ QUOTES="\""

WHITE_SPACE=[ \t\n\x0B\f\r]+
TYPE_TOKEN=@[a-zA-Z_]+
// BibTeX itself doesn't support % as a comment character (using it it the start of a line in an entry is illegal), but
// biber explicitly supports it as a comment, so we do as well.
COMMENT_TOKEN=%[^\r\n]*
// Characters disallowed by bibtex or biber (non-ascii or not depends on LaTeX compiler)
IDENTIFIER=[^,{}\(\)\"#%'=~\\ \n]+
Expand Down
24 changes: 23 additions & 1 deletion src/nl/hannahsten/texifyidea/util/Bibtex.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ fun BibtexContent.evaluate(): String {
for (string in childrenOfType(BibtexString::class)) {
val braced = string.bracedString
val quoted = string.quotedString
val bracedVerbatim = string.bracedVerbatim
val quotedVerbatim = string.quotedVerbatim
val defined = string.definedString

if (braced != null) {
Expand All @@ -89,6 +91,12 @@ fun BibtexContent.evaluate(): String {
else if (defined != null) {
result += defined.evaluate()
}
else if (bracedVerbatim != null) {
result += bracedVerbatim.evaluate()
}
else if (quotedVerbatim != null) {
result += quotedVerbatim.evaluate()
}
}

if (result.isEmpty()) {
Expand Down Expand Up @@ -163,4 +171,18 @@ fun BibtexBracedString.evaluate(): String {
fun BibtexQuotedString.evaluate(): String {
val text = text
return text.substring(1 until text.length - 1)
}
}

/**
* Returns the contents of the quoted verbatim string.
*
* E.g. `"Test Hello"` becomes `Test Hello`.
*/
fun BibtexQuotedVerbatim.evaluate(): String = text.substring(1 until text.length - 1)

/**
* Returns the contents of the braced verbatim string.
*
* E.g. `{Test Hello}` becomes `Test Hello`.
*/
fun BibtexBracedVerbatim.evaluate(): String = text.substring(1 until text.length - 1)
8 changes: 1 addition & 7 deletions src/nl/hannahsten/texifyidea/util/parser/BibtexPsiUtil.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,7 @@ fun BibtexEntry.getTagContent(tagName: String?): String {

entryContent.tagList.forEach {
if (tagName.equals(it.key.text, ignoreCase = true)) {
val text = it.content?.evaluate() ?: return ""

// Deal with braced strings.
return if (text.first() == '{' && text.last() == '}') {
text.substring(1, text.length - 1)
}
else text
return it.content?.evaluate() ?: ""
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class BibtexEntryImplUtilTest : BasePlatformTestCase() {
journal = {GitHub},
year = {2020},
url = {$url},
biburl = {$url}
biburl = "$url"
}"""

private val entryElement by lazy {
Expand Down

0 comments on commit db9fd0d

Please sign in to comment.