SemanticMediaWiki / SemanticCite

Allows to manage citation resources using semantic annotations
https://www.semantic-mediawiki.org/wiki/Extension:Semantic_Cite
Other
24 stars 13 forks source link

jabref bibxml to scite conversion #102

Closed WolfgangFahl closed 3 years ago

WolfgangFahl commented 3 years ago

The script below allows to convert jabref bibxml content to be converted to SemanticCite. To use the script use the copy/export to clipboard functionality of JabRef and then run the script on the command line. The "clipboard" npm tool and jq/xq are used by this tool as well as the https://pypi.org/project/pylatexenc/

scite

#!/bin/bash
# convert bibtex xml to #scite format
# WF 2021-06-01

#ansi colors
#http://www.csc.uvic.ca/~sae/seng265/fall04/tips/s265s047-tips/bash-using-colors.html
blue='\033[0;34m'
red='\033[0;31m'
green='\033[0;32m' # '\e[1;32m' is too bright for white bg.
endColor='\033[0m'

#
# a colored message
#   params:
#     1: l_color - the color of the message
#     2: l_msg - the message to display
#
color_msg() {
  local l_color="$1"
  local l_msg="$2"
  echo -e "${l_color}$l_msg${endColor}"
}

#
# error
#
# show the given error message on stderr and exit
#
#   params:
#     1: l_msg - the error message to display
#
error() {
  local l_msg="$1"
  # use ansi red for error
  color_msg $red "Error:" 1>&2
  color_msg $red "\t$l_msg" 1>&2
  exit 1
}

# show usage
#
usage() {
  echo "$0 [-h|--help]"
  echo ""
  echo "-h | --help: show this usage"
  exit 1
}

#https://stackoverflow.com/questions/749544/pipe-to-from-the-clipboard-in-bash-script
which clipboard > /dev/null
if [ $? -ne 0 ]
then
  color_msg $blue "installing clipboard helper"
  npm install -g clipboard-cli
fi

while [  "$1" != ""  ]
do
  option="$1"
  case $option in
    -h|--help) usage;;
  esac
  shift
done

xml=$(clipboard)
echo $xml | grep "<?xml version" > /dev/null
if [ $? -ne 0 ]
then
  error "expected bibtex xml in clipboard"
fi
pcode=/tmp/pcode$$.py
cat << EOF > $pcode
import json
import sys
from pylatexenc.latex2text import LatexNodes2Text

def unlatex(latex):
  '''
  convert latex code back to "normal"
  '''
  utf8=LatexNodes2Text().latex_to_text(latex)
  return utf8

bib=json.load(sys.stdin)
print (bib)
if "file" in bib:
  f=bib["file"]
  if "entry" in f:
    e=f["entry"]
    id=e["@id"].replace("_","")
    types=["article","inproceedings"]
    for type in types:
      if type in e:
        a=e[type]
        print(f"[[CiteRef::{id}]]")
        print(f"{{{{#scite:{id}")
        print("|type=article")
        for key,value in a.items():
          value=unlatex(value)
          print(f"|{key}={value}")
          print("}}")
EOF

echo $xml | xq . | python3 $pcode
rm $pcode
# {
#  "file": {
#    "@xmlns": "http://bibtexml.sf.net/",
#    "entry": {
#      "@id": "Auer_2021",
#      "article": {
#        "author": "Sören Auer and Markus Stocker and Lars Vogt and Grischa Fraumann and Alexandra Garatzogianni",
#        "title": "{ORKG}: Facilitating the Transfer of Research Results with the Open Research Knowledge Graph",
#        "journal": "Research Ideas and Outcomes",
#        "year": "2021",
#        "volume": "7",
#        "month": "May",
#        "doi": "10.3897/rio.7.e68513"
#      }
#    }
#  }
#}