LoginSignup
0
0

More than 3 years have passed since last update.

JavaScriptでBibTeXを連想配列に分解

Last updated at Posted at 2019-11-07

BibTeXを連想配列に分解するスクリプトを書きました。

BibTeXとは?

$\TeX$で文献を引用する際に利用するツールです。
GoogleScholarからデータをコピペで持ってくる事ができるなど、便利な点が色々あります。
BibTeXについては以下がとても参考になります。
- BiBTeXとは
- Texで書く論文の引用にはbibtexがオススメな4つの理由

背景

Google Spread Sheetで論文を整理しようとした際、「BibTeXの項目で分解できれば表の管理ラクじゃないか?」と思ったことがキッカケです。
ということで、BibTeX形式を分解することにしました。

スクリプト

元々はGAS(Google App Script)のために書いたものですが、互換性があるJavaScriptとして記載します。

resoluteBibtex.js

function resoluteBibtex(bibtex_source){
  // BibTeXソースを要素ごとの連想配列に分解
  var bibtex_source_tailcut = bibtex_source.replace(/}[ \n]*}/,''); // 末尾の'} }'を削除
  var bibtex_splited = bibtex_source_tailcut.split(/},[ \n]*/);

  var bibtex_contents = {};
  var value, value_splited;

  for (var index in bibtex_splited){
    value = bibtex_splited[index];

    // @xxxxx{label, となっている部分の対応
    if( value.charAt(0) == '@' ){
      value_splited = value.split('{');
      bibtex_contents['@'] = value_splited[0];
      bibtex_contents['label'] = value_splited[1].substring(0,value_splited[1].indexOf(','));
      var next_content_by_label = value_splited[1].replace(/[ \w]*, */,'');
      bibtex_contents[next_content_by_label.replace('=','')] = value_splited[2];
    } else {
      value_splited = value.split('={');
      bibtex_contents[value_splited[0]] = value_splited[1];
    }
  }

  return bibtex_contents;
}

使用例

入力:BibTeX形式のデータ
出力:連想配列


// BibTeX形式のデータ
var bibtex_source = "@inproceedings{shvachko2010hadoop,
  title={The hadoop distributed file system.},
  author={Shvachko, Konstantin and Kuang, Hairong and Radia, Sanjay and Chansler, Robert and others},
  booktitle={MSST},
  volume={10},
  pages={1--10},
  year={2010}
}";

var bibtex_contents = resoluteBibtex(bibtex_source);

実行結果

変数bibtex_contentsの中身は以下のようになっています。


// キー:値 で表示
"@"        :"inproceedings",
"label"    :"shvachko2010hadoop",
"title"    :"The hadoop distributed file system.",
"author"   :"Shvachko, Konstantin and Kuang, Hairong and Radia, Sanjay and Chansler, Robert and others",
"booktitle":"MSST",
"volume"   :"10",
"pages"    :"1--10",
"year"     :"2010"

titleauthorといった項目名は上記の例以外でも対応しています。

まとめ

BibTeX形式を連想配列に分解するスクリプトを書きました。
JavaScriptやGoogle App ScriptでBibTeXを扱う場合にご利用ください。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0