Skip to content

Commit 27a62d1

Browse files
committed
refactored global object intiialization
1 parent 10fe5b3 commit 27a62d1

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

src/proose/libraries/goose.js

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ try {
66
} catch(error) {
77
application.logger.warn("Google Translate Java API not found: Translation feature unavailable.")
88
}
9+
document.execute('register/')
910

1011
var Goose = Goose || function() {
1112
var Public = {
@@ -21,7 +22,7 @@ var Goose = Goose || function() {
2122
"text": String(StringEscapeUtils.unescapeHtml(article.getCleanedArticleText()))
2223
}
2324
} catch(error) {
24-
application.logger.info(error + ": " + uri)
25+
application.logger.debug(error + ": " + uri)
2526
return null
2627
}
2728
if (srclang && tlang) {
@@ -37,33 +38,14 @@ var Goose = Goose || function() {
3738
}
3839
}
3940
}
40-
41-
// Initialize config
42-
Public.config = application.globals.get('proose.config')
43-
if (!Public.config) {
44-
var config = new Configuration()
45-
config.setEnableImageFetching(false)
46-
Public.config = application.getGlobal('proose.config', config)
41+
// Initialize
42+
Public.config = register(Configuration, null, {'setEnableImageFetching': false})
43+
Public.extractor = register(ContentExtractor, Public.config)
44+
try {
45+
Public.translate = register(Translate, null, {'setHttpReferrer': application.globals.get('proose.settings.httpReferrer')})
46+
} catch(error) {
47+
// Google Translate library is optional
4748
}
48-
49-
// Initialize extractor
50-
Public.extractor = application.globals.get('proose.extractor')
51-
if (!Public.extractor && Public.config) {
52-
Public.extractor = application.getGlobal('proose.extractor', new ContentExtractor(Public.config))
53-
}
54-
55-
// Initialize translate (optional)
56-
Public.translate = application.globals.get('proose.translate')
57-
if (!Public.translate) {
58-
try {
59-
var translate = new Translate()
60-
translate.setHttpReferrer(application.globals.get('proose.settings.httpReferrer'))
61-
Public.translate = application.getGlobal('proose.translate', translate)
62-
}
63-
catch (x) {
64-
// translate is optional
65-
}
66-
}
6749
return Public
6850
}()
6951

src/proose/libraries/register.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
function register(cls, params, attributes) {
2+
namespace = application.globals.get('proose.settings.namespace')
3+
// don't have access to cls.class.getName() in JS, hence regex
4+
clsname = String(cls).match(/ (.*)]/)[1]
5+
globname = namespace + '.' + clsname
6+
globinst = application.globals.get(globname)
7+
if (!globinst) {
8+
if (params) {
9+
var instance = new cls(params)
10+
} else {
11+
var instance = new cls()
12+
}
13+
var cls = instance.getClass()
14+
// can't use java.lang.Class in Rhino, hence no getMethod()
15+
var methods = cls.getMethods()
16+
var method_names = []
17+
for (m in methods) {
18+
method_names[m] = String(methods[m].getName())
19+
}
20+
for (a in attributes) {
21+
method_index = method_names.indexOf(a)
22+
method = methods[method_index]
23+
method.invoke(instance, attributes[a])
24+
}
25+
globinst = application.getGlobal(globname, instance)
26+
}
27+
return globinst
28+
}

src/proose/settings.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ applicationHomeURL = 'http://github.com/mdorn/proose'
88
applicationContactEmail = '[email protected]'
99

1010
predefinedGlobals['proose.settings.httpReferrer'] = 'http://example.com'
11+
predefinedGlobals['proose.settings.namespace'] = 'proose'

0 commit comments

Comments
 (0)