aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authors-ol <s-ol@users.noreply.github.com>2019-12-18 15:28:50 +0000
committers-ol <s-ol@users.noreply.github.com>2019-12-18 15:30:43 +0000
commita032d749b5819eeb7f9604dc352e8aef225c153c (patch)
tree61c77d24c655d553f98b8ac7c53a0894870a8dfd
parentprotect print/deep_tostring from recursion (diff)
downloadmmm-a032d749b5819eeb7f9604dc352e8aef225c153c.tar.gz
mmm-a032d749b5819eeb7f9604dc352e8aef225c153c.zip
fix server-side CORS HTTP requests
-rw-r--r--mmm/mmmfs/plugins/cites.moon8
-rw-r--r--mmm/mmmfs/plugins/init.moon26
2 files changed, 28 insertions, 6 deletions
diff --git a/mmm/mmmfs/plugins/cites.moon b/mmm/mmmfs/plugins/cites.moon
index 01f5b3f..b26241d 100644
--- a/mmm/mmmfs/plugins/cites.moon
+++ b/mmm/mmmfs/plugins/cites.moon
@@ -8,7 +8,11 @@ import div, i from require 'mmm.dom'
cost: 0.5
transform: (url) =>
id = assert (url\match '//dl%.acm%.org/citation%.cfm%?id=(%d+)'), "couldn't parse cite/acm URL: '#{url}'"
- "https://cors-anywhere.herokuapp.com/https://dl.acm.org/exportformats.cfm?id=#{id}&expformat=bibtex"
+ uri = "https://dl.acm.org/downformats.cfm?id=#{id}&parent_id=&expformat=bibtex"
+ if MODE == 'CLIENT'
+ "https://cors-anywhere.herokuapp.com/#{uri}"
+ else
+ uri
}
{
inp: 'text/bibtex'
@@ -20,7 +24,7 @@ import div, i from require 'mmm.dom'
for key, val in kv\gmatch '([a-z]-)%s*=%s*{(.-)}'
info[key] = val
- div "#{info.author} (#{info.year}),", (i info.title), ". #{info.publisher}"
+ div "#{info.author} (#{info.year}), ", (i info.title), ". #{info.publisher}"
}
}
}
diff --git a/mmm/mmmfs/plugins/init.moon b/mmm/mmmfs/plugins/init.moon
index 1650c3a..ba633a8 100644
--- a/mmm/mmmfs/plugins/init.moon
+++ b/mmm/mmmfs/plugins/init.moon
@@ -18,6 +18,23 @@ loadwith = (_load) -> (val, fileder, key) =>
func = assert _load val, "#{fileder}##{key}"
func!
+string.yieldable_gsub = (str, pat, f) ->
+ -- escape percent signs
+ str = str\gsub '%%', '%%|'
+
+ matches = {}
+ str\gsub pat, (...) ->
+ table.insert matches, { ... }
+ "%#{#matches}"
+
+ for match in *matches
+ match.replacement = f table.unpack match
+
+ str\gsub '%%(%d+)', (i) -> matches[i].replacement
+
+ -- unescape escaped percent signs
+ str\gsub '%%|', '%%'
+
-- list of converts, editors
-- converts each have
-- * inp - input type. can capture subtypes using `(.+)`
@@ -59,7 +76,7 @@ converts = {
cost: 0.1
transform: if MODE == 'SERVER'
(html, fileder) =>
- html = html\gsub '<mmm%-link%s+(.-)>(.-)</mmm%-link>', (attrs, text) ->
+ html = html\yieldable_gsub '<mmm%-link%s+(.-)>(.-)</mmm%-link>', (attrs, text) ->
text = nil if #text == 0
path = ''
while attrs and attrs != ''
@@ -76,7 +93,7 @@ converts = {
link_to path, text, fileder
- html = html\gsub '<mmm%-embed%s+(.-)>(.-)</mmm%-embed>', (attrs, desc) ->
+ html = html\yieldable_gsub '<mmm%-embed%s+(.-)>(.-)</mmm%-embed>', (attrs, desc) ->
path, facet = '', ''
opts = {}
if #desc != 0
@@ -137,7 +154,7 @@ converts = {
out: '%1',
cost: 1
transform: (source, fileder) =>
- source\gsub '{{(.-)}}', (expr) ->
+ source\yieldable_gsub '{{(.-)}}', (expr) ->
path, facet = expr\match '^([%w%-_%./]*)%+(.*)'
assert path, "couldn't match TPL expression '#{expr}'"
@@ -201,7 +218,8 @@ converts = {
(uri) =>
request = require 'http.request'
req = request.new_from_uri uri
- headers, stream = req\go 8
+ req.headers\upsert 'origin', 'null'
+ headers, stream = assert req\go 8
assert stream\get_body_as_string!
}
{