Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
vue-cdbp
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Markus Opolka
vue-cdbp
Commits
faa601ea
Commit
faa601ea
authored
7 years ago
by
Markus Opolka
Browse files
Options
Downloads
Patches
Plain Diff
Remove legacy code
parent
14c36fe9
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
api.legacy.js
+0
-62
0 additions, 62 deletions
api.legacy.js
lib.legacy.js
+0
-253
0 additions, 253 deletions
lib.legacy.js
with
0 additions
and
315 deletions
api.legacy.js
deleted
100644 → 0
+
0
−
62
View file @
14c36fe9
const
express
=
require
(
'
express
'
)
const
path
=
require
(
'
path
'
)
const
lib
=
require
(
'
./lib.legacy.js
'
)
// Setting
const
dir
=
'
letters
'
const
app
=
express
()
const
PORT
=
3000
// CORS Settings
app
.
use
(
function
(
req
,
res
,
next
)
{
res
.
header
(
'
Access-Control-Allow-Origin
'
,
'
*
'
)
res
.
header
(
'
Access-Control-Allow-Headers
'
,
'
Origin, X-Requested-With, Content-Type, Accept
'
)
next
()
})
app
.
get
(
'
/api/search
'
,
function
(
req
,
res
)
{
// Search Endpoint with query parameters
// <element attribute="value">content</element>
var
query
=
{
element
:
req
.
query
.
element
,
content
:
req
.
query
.
content
,
attribute
:
req
.
query
.
attribute
,
value
:
req
.
query
.
value
}
var
data
=
lib
.
forAllFiles
(
lib
.
searchLetters
,
dir
,
{
query
:
query
})
res
.
send
(
data
)
})
app
.
get
(
'
/api/features/
'
,
function
(
req
,
res
)
{
// Feature Search Endpoint with query parameters
// <feature category="diakritika" type="akut" subtype="akutstattgravis" ref="26">
var
query
=
{
category
:
req
.
query
.
category
,
type
:
req
.
query
.
type
,
subtype
:
req
.
query
.
subtype
}
var
data
=
lib
.
forAllFiles
(
lib
.
searchFeatures
,
dir
,
{
query
:
query
})
res
.
send
(
data
)
})
app
.
get
(
'
/api/letters/:filename?
'
,
function
(
req
,
res
)
{
// Document Endpoint with optional Document name
// Lists either all or one given document
if
(
req
.
params
.
filename
){
// Load specified document
var
filename
=
req
.
params
.
filename
+
'
.xml
'
res
.
send
(
lib
.
getXMLasJSON
(
path
.
join
(
dir
,
filename
)))
}
else
{
// Load all documents
var
data
=
lib
.
forAllFiles
(
lib
.
listLetters
,
dir
)
res
.
send
(
data
)
}
})
// Where the magic happens
app
.
listen
(
PORT
,
function
()
{
console
.
log
(
'
> Listening at http://localhost:
'
+
PORT
)
})
This diff is collapsed.
Click to expand it.
lib.legacy.js
deleted
100644 → 0
+
0
−
253
View file @
14c36fe9
const
fs
=
require
(
'
fs
'
)
const
path
=
require
(
'
path
'
)
const
convert
=
require
(
'
xml-js
'
)
const
flat
=
require
(
'
flat
'
)
const
flatten
=
require
(
'
flatten
'
)
const
dir
=
'
letters
'
function
findKeyRecursive
(
object
,
keytofind
,
ret
=
[])
{
/*
* Finds all keys in a nested object and returns them as an array
*/
var
value
Object
.
keys
(
object
).
forEach
(
function
(
key
)
{
if
(
key
===
keytofind
)
{
ret
.
push
(
object
[
key
])
return
true
}
if
(
object
[
key
]
&&
typeof
object
[
key
]
===
'
object
'
)
{
value
=
findKeyRecursive
(
object
[
key
],
keytofind
,
ret
)
return
value
!==
undefined
}
else
{
return
true
}
})
return
flatten
(
ret
)
}
function
searchInFeatures
(
features
,
query
)
{
/*
* Searches for a query in an array of features
* Attributes must be in _attributes
*/
var
results
=
[]
var
reCat
=
new
RegExp
(
query
.
category
)
var
reType
=
new
RegExp
(
query
.
type
)
var
reSub
=
new
RegExp
(
query
.
subtype
)
for
(
var
idx
=
0
;
idx
<
features
.
length
;
idx
++
){
var
match_category
=
reCat
.
test
(
features
[
idx
].
_attributes
.
category
)
var
match_type
=
reType
.
test
(
features
[
idx
].
_attributes
.
type
)
var
match_subtype
=
reSub
.
test
(
features
[
idx
].
_attributes
.
subtype
)
if
(
match_category
&&
match_type
&&
match_subtype
)
{
results
.
push
(
features
[
idx
])
}
}
return
results
}
function
transformFilename
(
filename
,
url
=
false
)
{
/*
* Transforms a full filepath:
* from example_1.xml to example 1
* URL: from example_1.xml to example_1
*/
filename
=
filename
.
slice
(
0
,
-
4
)
if
(
!
url
)
{
filename
=
filename
.
replace
(
/_/g
,
'
'
)
}
return
filename
}
function
searchInObject
(
obj
,
query
)
{
/*
* Search for query in a fiven flatobject
*/
var
results
=
[]
Object
.
keys
(
obj
).
forEach
(
function
(
key
)
{
// Attributes
if
(
typeof
query
.
attribute
!==
"
undefined
"
){
var
re_attr
=
new
RegExp
(
'
_attributes.
'
+
query
.
attribute
)
if
(
!
re_attr
.
test
(
key
)){
return
}
}
// Values
if
(
typeof
query
.
value
!==
"
undefined
"
){
var
re_valu
=
new
RegExp
(
query
.
value
)
if
(
!
re_valu
.
test
(
obj
[
key
])){
return
}
}
// Elements
if
(
typeof
query
.
element
!==
"
undefined
"
){
var
re_elem
=
new
RegExp
(
'
[^_]
'
+
query
.
element
,
'
g
'
)
if
(
!
re_elem
.
test
(
key
)){
return
}
}
// Content
if
(
typeof
query
.
content
!==
"
undefined
"
){
var
re_cont
=
new
RegExp
(
query
.
content
)
if
(
!
re_cont
.
test
(
obj
[
key
])){
return
}
}
// Add hit to results as object
res
=
{}
res
[
key
]
=
obj
[
key
]
results
.
push
(
res
)
})
return
results
}
function
getXMLasJSON
(
filepath
)
{
/*
* TODO: This is duplicate Code. Needs to go
* TODO: Maybe check if XML is valid?
* Reads XML file and transforms it to JSON
*/
var
data
=
{}
try
{
var
xml
=
fs
.
readFileSync
(
filepath
,
'
utf8
'
)
data
=
convert
.
xml2js
(
xml
,
{
compact
:
true
})
}
catch
(
err
)
{
console
.
log
(
'
Error while loading
'
+
filepath
)
console
.
log
(
err
)
return
data
}
return
data
}
function
countReferences
(
features
)
{
/*
* Count the references in a feature
*/
var
count
=
0
for
(
idx
=
0
;
idx
<
features
.
length
;
idx
++
)
{
count
=
count
+
features
[
idx
].
_attributes
.
ref
.
trim
().
split
(
"
"
).
length
}
return
count
}
module
.
exports
=
{
getXMLasJSON
:
function
(
filepath
)
{
/*
* Reads XML file and transforms it to JSON
*/
var
data
=
{}
try
{
var
xml
=
fs
.
readFileSync
(
filepath
,
'
utf8
'
)
data
=
convert
.
xml2js
(
xml
,
{
compact
:
true
})
}
catch
(
err
)
{
console
.
log
(
'
Error while loading
'
+
filepath
)
console
.
log
(
err
)
return
data
}
return
data
},
listLetters
:
function
(
file
,
params
)
{
/*
* Callback function
* Transforms a given parsed XML file into the list view
*/
return
{
link
:
'
/letters/
'
+
file
.
slice
(
0
,
-
4
),
name
:
transformFilename
(
file
),
filepath
:
path
.
join
(
dir
,
file
),
url
:
'
/api/letters/
'
+
transformFilename
(
file
,
true
)
}
},
searchFeatures
:
function
(
file
,
params
)
{
/*
* Callback function
* Search for features in file
*/
var
query
=
params
.
query
var
result
=
undefined
var
letterJS
=
getXMLasJSON
(
path
.
join
(
dir
,
file
))
var
features
=
findKeyRecursive
(
letterJS
,
'
feature
'
)
if
(
typeof
query
.
category
==
"
undefined
"
){
query
.
category
=
'
.*
'
}
if
(
typeof
query
.
type
==
"
undefined
"
){
query
.
type
=
'
.*
'
}
if
(
typeof
query
.
subtype
==
"
undefined
"
){
query
.
subtype
=
'
.*
'
}
var
res
=
searchInFeatures
(
features
,
query
)
var
count
=
countReferences
(
res
)
if
(
res
.
length
>
0
)
{
result
=
{
collection
:
letterJS
.
TEI
.
teiHeader
.
fileDesc
.
titleStmt
.
collection
.
_text
,
count
:
count
,
name
:
transformFilename
(
file
),
link
:
'
/letters/
'
+
file
.
slice
(
0
,
-
4
),
url
:
'
/api/letters/
'
+
transformFilename
(
file
,
true
),
results
:
res
}
}
return
result
},
searchLetters
:
function
(
file
,
params
){
/*
* Callback function
* Search for query in file
*/
var
query
=
params
.
query
var
result
=
undefined
var
letterJS
=
getXMLasJSON
(
path
.
join
(
dir
,
file
))
var
flatobject
=
flat
(
letterJS
.
TEI
)
var
res
=
searchInObject
(
flatobject
,
query
)
if
(
res
.
length
>
0
)
{
result
=
{
collection
:
letterJS
.
TEI
.
teiHeader
.
fileDesc
.
titleStmt
.
collection
.
_text
,
name
:
transformFilename
(
file
),
link
:
'
/letters/
'
+
file
.
slice
(
0
,
-
4
),
url
:
'
/api/letters/
'
+
transformFilename
(
file
,
true
),
results
:
res
}
}
return
result
},
forAllFiles
:
function
(
callback
,
filepath
,
params
=
{})
{
/*
* Load files from filepath and perform callback on each with given parameters
*/
var
ret
=
[]
try
{
var
files
=
fs
.
readdirSync
(
filepath
)
}
catch
(
err
)
{
console
.
log
(
'
> Error while loading files from
'
+
filepath
)
console
.
log
(
err
)
return
[]
}
for
(
var
i
in
files
)
{
// We just want the XML documents
if
(
!
files
[
i
].
endsWith
(
'
.xml
'
))
{
continue
}
// Call callback function on each file
var
val
=
callback
(
files
[
i
],
params
)
if
(
typeof
val
!==
"
undefined
"
){
ret
.
push
(
val
)
}
}
return
ret
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment