tex2refer 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. #! /bin/sh
  2. #
  3. # tex2refer - converts bibtex entries to refer entries
  4. #
  5. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  6. # This software comes on a 'as is'-basis.
  7. # No guarantee for the correctness is given and no 'service' is provided
  8. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  9. #
  10. # This program (an awk skript) converts bibliographic references from the
  11. # bibtex-format to the refer-format.
  12. # it reads from stdin and writes to stdout:
  13. #
  14. # usage: tex2refer < file.bib > file.refer
  15. #
  16. # Be aware, that some information is neccessarily lost, because
  17. #
  18. # * several bibtex field names are mapped to the same refer filed name
  19. # e.g. publisher, organization and school are all mapped to %I
  20. # * refer doesn't support types for references (like @inproceedings, @article)
  21. # (therefore the inverse mapping refer2tex is mostly based on heuristics)
  22. #
  23. # In this program are only the more important (I.M.H.O.) field names covered.
  24. # If tex2refer encounters unknown field names, it will ignore them but store their
  25. # names in a list, which is displayed after the conversion process.
  26. #
  27. # With this list the program can easily be extended by adding entries to the
  28. # associative array 'refer'
  29. #
  30. #
  31. # Thanks to Lee, who provided the main part of the program and added
  32. # some useful comments for readability
  33. #
  34. # Bernd Fritzke (fritzke@immd2.informatik.uni-erlangen.de)
  35. # August 1990
  36. #
  37. gawk ' #gnu awk, but works probably also with other versions
  38. BEGIN {
  39. FS = " "
  40. refer["book"] = "%K"
  41. refer["author"] = "%A"
  42. refer["AUTHOR"] = "%A"
  43. refer["address"] = "%C"
  44. refer["ADDRESS"] = "%C"
  45. refer["year"] = "%D"
  46. refer["YEAR"] = "%D"
  47. refer["publisher"] = "%I"
  48. refer["PUBLISHER"] = "%I"
  49. refer["journal"] = "%J"
  50. refer["JOURNAL"] = "%J"
  51. refer["keywords"] = "%K"
  52. refer["KEYWORDS"] = "%K"
  53. refer["pages"] = "%P"
  54. refer["PAGES"] = "%P"
  55. refer["title"] = "%T"
  56. refer["TITLE"] = "%T"
  57. refer["volume"] = "%V"
  58. refer["VOLUME"] = "%V"
  59. refer["city"] = "%C"
  60. refer["CITY"] = "%C"
  61. refer["booktitle"] = "%B"
  62. refer["BOOKTITLE"] = "%B"
  63. refer["note"] = "%o"
  64. refer["NOTE"] = "%o"
  65. refer["organization"] = "%I"
  66. refer["ORGANIZATION"] = "%I"
  67. refer["school"] = "%I"
  68. refer["SCHOOL"] = "%I"
  69. }
  70. /^@/ {next} # reference type (not supported by refer) next line
  71. # ---> "next" makes awk goto the start of the awk script & read the next
  72. # line of input, so here it is making it ignore lines starting
  73. # with an @ sign.
  74. # ensure that an = signs is surrounded by space:
  75. /\=/ { # "=" must be preceeded by `\` , also in line below
  76. gsub(/\=/, " & ") # this may cause NF to be updated...
  77. # Warning -- do not put single quotes in comments! This does not
  78. # work reliably in a shell script.
  79. }
  80. ($1 in refer && $2 == "=") { # Begin of a bibtex field definition
  81. gsub(/[{}]/, "") # deleting curly brackets
  82. gsub(/,$/, "") # deleting commas at end of line
  83. printf "%s ", refer[$1]
  84. for (i = 3; i <= NF; i++) { # Loop over the keywords ($2 is "=")
  85. printf " %s", $i # and print them
  86. }
  87. printf "\n" # newline on the end of the refer entry
  88. }
  89. (!($1 in refer) && $2 == "=") { #collect unknown keywords
  90. unknown[$1] = 1
  91. next
  92. }
  93. /[^ ]/ {
  94. if ($2 != "=") { # This is not a first line of an entry
  95. # In this case, we are dealing with a continuation line.
  96. gsub(/[{}]/, "") # deleting curly braces
  97. gsub(/,$/, "") # deleting commas at end of line
  98. printf " "
  99. for (i = 1; i <= NF; i++) { # Loop over all the keywords
  100. printf " %s", $i # and print them
  101. }
  102. printf "\n" # newline on the end of the refer entry
  103. }
  104. }
  105. END {
  106. }
  107. ' ${@+"$@"}