aboutsummaryrefslogtreecommitdiffstats
path: root/root/$mmm/plugins/cites/converts: text$lua -> table.lua
blob: 78dadfcb125c8e8fca778bacbfce1305f13cc818 (plain)
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
local div, span, sup, a, i, b
do
  local _obj_0 = require('mmm.dom')
  div, span, sup, a, i, b = _obj_0.div, _obj_0.span, _obj_0.sup, _obj_0.a, _obj_0.i, _obj_0.b
end
local parse_bibtex
parse_bibtex = function(src)
  local type, key, kv = src:match('@(%w+){(.-),(.*)}')
  do
    local info = {
      _type = type,
      _key = key
    }
    for key, val in kv:gmatch('([a-z]-)%s*=%s*(%b{})') do
      val:sub(2, -2)
      info[key] = val:gsub('[{}]', '')
    end
    for key, val in kv:gmatch('([a-z]-)%s*=%s*(%d+)') do
      info[key] = val
    end
    return info
  end
end
local title
title = function(self)
  assert(self.title, "cite doesn't have title")
  local inner = i(self.title)
  if self.url then
    return a(inner, {
      href = self.url,
      style = {
        display = 'inline'
      }
    })
  else
    return b(inner)
  end
end
local format_full
format_full = function(self)
  local tt = title(self)
  local dot, com
  if self.title:match('[.?!]$') then
    dot, com = '', ''
  else
    dot, com = '.', ','
  end
  self.author = self.author or 'N. N.'
  local _exp_0 = self._type
  if 'book' == _exp_0 or 'article' == _exp_0 then
    return span((function()
      do
        local _with_0 = setmetatable({ }, {
          __index = table
        })
        _with_0:insert(tostring(self.author) .. " (" .. tostring(self.year) .. "), ")
        _with_0:insert(tt)
        if self.journal then
          _with_0:insert(tostring(dot) .. " ")
          _with_0:insert(i(self.journal))
          if self.volume then
            _with_0:insert(", volume " .. tostring(self.volume))
          end
        else
          if self.series then
            _with_0:insert(tostring(dot) .. " ")
            _with_0:insert(i(self.series))
            if self.number then
              _with_0:insert(", No. " .. tostring(self.number))
            end
          end
        end
        if self.pages then
          _with_0:insert(", pages " .. tostring(self.pages))
        end
        if self.publisher then
          _with_0:insert(tostring(dot) .. " " .. tostring(self.publisher))
        end
        if self.doi then
          _with_0:insert(tostring(dot) .. " ")
          _with_0:insert(a("doi:" .. tostring(self.doi), {
            href = "https://doi.org/" .. tostring(self.doi)
          }))
        end
        return _with_0
      end
    end)())
  elseif 'web' == _exp_0 or 'online' == _exp_0 then
    return span((function()
      do
        local _with_0 = setmetatable({ }, {
          __index = table
        })
        _with_0:insert(tostring(self.author))
        if self.year then
          _with_0:insert(" (" .. tostring(self.year) .. ")")
        end
        _with_0:insert(", ")
        _with_0:insert(tt)
        _with_0:insert(tostring(com) .. " " .. tostring(self.url))
        if self.visited then
          _with_0:insert(" from " .. tostring(self.visited))
        end
        return _with_0
      end
    end)())
  else
    span((function()
      do
        local _with_0 = setmetatable({ }, {
          __index = table
        })
        _with_0:insert(tostring(self.author) .. " (" .. tostring(self.year) .. "), ")
        _with_0:insert(tt)
        if self.publisher then
          _with_0:insert(tostring(dot) .. " " .. tostring(self.publisher))
        end
        return _with_0
      end
    end)())
    return span(tbl)
  end
end
return {
  {
    inp = 'cite/doi',
    out = 'URL -> text/bibtex',
    cost = 0.1,
    transform = function(self, doi)
      doi = doi:match('(10%.%d%d%d%d%d?%d?%d?%d?%d?/[%d%w%.%-_:;%(%)]+)')
      return "http://api.crossref.org/works/" .. tostring(doi) .. "/transform/application/x-bibtex"
    end
  },
  {
    inp = 'text/bibtex',
    out = 'mmm/dom',
    cost = 1,
    transform = function(self, bib)
      return format_full(parse_bibtex(bib))
    end
  }
}