File: import.R

package info (click to toggle)
r-cran-rbibutils 2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,100 kB
  • sloc: ansic: 99,989; xml: 4,509; sh: 13; makefile: 2
file content (102 lines) | stat: -rw-r--r-- 3,946 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
convert_bib2be <- function(infile, outfile, ..., tex = NULL, encoding = NULL,
                           options, extra = FALSE){
    stopifnot(length(list(...)) == 0) # no ... arguments allowed

    argv_2be <- "bib2be"    # prg

    if(!is.null(encoding)){
        if(length(encoding) == 1)
            encoding <- rep(encoding, 2)

        if(encoding[1] == "UTF-8" || encoding[1] == "default")
            encoding[1] <- "utf8"
        if(encoding[2] == "UTF-8" || encoding[2] == "default")
            encoding[2] <- "utf8"
                                             
        argv_2be <- c(argv_2be, "-i", encoding[1])
        argv_2be <- c(argv_2be, "-o", encoding[2])
    }

    if(!is.null(tex)){
        for(tex_op in tex){
            switch(tex_op,
                   keep_tex_chars = {  #  Georgi
                       argv_2be <- c(argv_2be, "--keep-tex-chars") 
                   },
                   no_latex = { # accents to letters    # TODO: this needs further work
                       argv_2be <- c(argv_2be, "-nl") 
                   },
                   export_tex_chars = {
                       argv_2be <- c(argv_2be, "--export_tex_chars")
                   },
                   ## uppercase = {
                   ##     argv_2be <- c(argv_2be, "-U")
                   ## },
                   brackets = {
                       argv_2be <- c(argv_2be, "-b")
                   },
                   dash = {
                       argv_2be <- c(argv_2be, "-sd")
                   },
                   comma = {
                       argv_2be <- c(argv_2be, "-fc")
                   },
                   Rdpack = {
                       argv_2be <- c(argv_2be, "--Rdpack")
                   },
                   convert_latex_escapes = {
                       argv_2be <- c(argv_2be, "--convert_latex_escapes")
                   },
                   ## default
                   stop("unsupported 'tex' option")
                   )
        }
    }
    
    if(!missing(options)){
        nams <- names(options)
        ## options <- as.vector(options)
        for(j in seq_along(options)){
            switch(nams[j],
                   i = { argv_2be <- c(argv_2be, "-i", options[j]) },
                   o = { argv_2be <- c(argv_2be, "-o", options[j]) },
                   ## oxml = {argv_2xml <- c(argv_2xml, "-o", options[j])},
                   h = {argv_2be <- c(argv_2be, "-h")},
                   v = {argv_2be <- c(argv_2be, "-v")},
                   a = {argv_2be <- c(argv_2be, "-a")},
                   s = {argv_2be <- c(argv_2be, "-s")},
                   u = {argv_2be <- c(argv_2be, "-u")},
                   U = {argv_2be <- c(argv_2be, "-U")},
                   un = {argv_2be <- c(argv_2be, "-un")},
                   x = {argv_2be <- c(argv_2be, "-x")},
                   nl = { argv_2be <- c(argv_2be, "-nl") },
                   d = { argv_2be <- c(argv_2be, "-d") },
                   c = {argv_2be <- c(argv_2be, "-c", options[j])},
                   ## as = {argv_2be <- c(argv_2be, "-as", options[j])},
                   nt = {argv_2be <- c(argv_2be, "-nt")},
                   verbose = {argv_2be <- c(argv_2be, "--verbose")},
                   debug = {
                       argv_2be <- c(argv_2be, "--debug")
                   },
                   
                   ##default
                   stop("unsupported option '", nams[j])
                   )
        }
    }

    argv_2be <- c(argv_2be, infile)
    argc_2be <- as.integer(length(argv_2be))

    n_2be <- as.double(0)  # for the number of references (double)

    .C(C_bib2be_main, argc_2be, argv_2be, outfile, nref_in = n_2be)
}

bibtexImport <- function(infile, ..., extra = FALSE, fbibentry = NULL){
    outfile <- tempfile(fileext = ".R")
    on.exit(unlink(outfile))

    convert_bib2be(infile, outfile, ...)
    readBibentry(outfile, extra = extra, fbibentry = fbibentry)
}