Commit b3c91872 authored by Denis Salem's avatar Denis Salem
Browse files

fix escaping bug.

parent 8e863c0d
......@@ -150,7 +150,7 @@
DONE | Fix pattern (i.e .:GetEntryTitle:.) access in template's metadata.
DONE | Infinite scroll use html anchor instead of harcoded indexing.
DONE | Prevent Infinite scroll to block when ressource isn't available.
WIP | rewrite unit tests.
WIP | Rewrite unit tests.
TODO | Open with navigator manual.
TODO | Client-side search engine.
TODO | Reorganize themes dependencies in setup (adding default templates, scripts, and pages to include).
......
......@@ -35,12 +35,19 @@ var VENC_INFINITE_SCROLL = {
v = document.querySelectorAll('[data-venc-api-infinite-scroll-hook]')
if (v.length == 1) {
this.pageHook = v[0].dataset.vencApiInfiniteScrollHook;
console.log("VenC: Hook", this.pageHook);
}
else if (v.length > 1) {
console.log("VenC: There is more than one infinite scroll hook in DOM. Aborting...")
this.end = true;
return
}
console.log("VenC: Hook", this.pageHook);
else {
console.log("VenC: Infinite Scroll hook not found. Exiting...");
this.end = true;
return;
}
},
getContent : function() {
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
......@@ -150,7 +157,7 @@ function VENC_INFINITE_SCROLL_RUN() {
function VENC_INFINITE_SCROLL_ON_LOAD() {
VENC_INFINITE_SCROLL.currentLocation = window.location.pathname.split('/')[window.location.pathname.split('/').length-1]
VENC_INFINITE_SCROLL.getPageHook()
if (VENC_INFINITE_SCROLL.hideVenCNavigation && (VENC_INFINITE_SCROLL.currentLocation.replace( /[.html0123456789]+/g, '') == "index" || VENC_INFINITE_SCROLL.currentLocation == "")) {
if (VENC_INFINITE_SCROLL.hideVenCNavigation) {
try {
document.getElementById("__VENC_NAVIGATION__").setAttribute("style","display: none;");
}
......@@ -158,7 +165,10 @@ function VENC_INFINITE_SCROLL_ON_LOAD() {
console.log("VenC: There is no __VENC_NAVIGATION__ element.");
}
}
VENC_INFINITE_SCROLL.domUpdate = VENC_INFINITE_SCROLL_UPDATE_DOM;
VENC_INFINITE_SCROLL.timer = setInterval(VENC_INFINITE_SCROLL_RUN, VENC_INFINITE_SCROLL.interval);
if (VENC_INFINITE_SCROLL.end == false) {
console.log("there")
VENC_INFINITE_SCROLL.domUpdate = VENC_INFINITE_SCROLL_UPDATE_DOM;
VENC_INFINITE_SCROLL.timer = setInterval(VENC_INFINITE_SCROLL_RUN, VENC_INFINITE_SCROLL.interval);
}
};
......@@ -207,6 +207,7 @@ def export_blog(argv=list()):
# Starting second pass and exporting
thread = MainThread(messages.export_main_thread, datastore, theme, contextual_pattern_names, non_contextual_pattern_names_entry_keys)
thread.do()
if not datastore.blog_configuration["disable_archives"]:
thread = DatesThread(messages.export_archives, datastore, theme, contextual_pattern_names, non_contextual_pattern_names_entry_keys)
thread.do()
......
......@@ -138,8 +138,8 @@ class ProcessedString():
string = string[:o]+string[o+10:]
string = string[:c-10]+string[c+3:]
escapes[i] = (o, c-10)
self.open_pattern_pos = [(v-23 if v > o+23 else v) for v in self.open_pattern_pos]
self.close_pattern_pos = [(v-23 if v > o+23 else v) for v in self.close_pattern_pos]
self.open_pattern_pos = [(v-10 if v > o+10 and v <= o+23 else (v-23 if v > o+23 else v)) for v in self.open_pattern_pos]
self.close_pattern_pos = [(v-10 if v > o+10 and v <= o+23 else (v-23 if v > o+23 else v)) for v in self.close_pattern_pos]
if i == 0:
escapes = escapes[:1]+[ (p[0]-23, p[1]-23) for p in escapes[i+1:] ]
......@@ -149,7 +149,6 @@ class ProcessedString():
if len(escapes):
self.open_pattern_pos = [v for v in self.open_pattern_pos if not index_in_range(v, escapes, process_escapes)]
self.close_pattern_pos = [v for v in self.close_pattern_pos if not index_in_range(v, escapes, process_escapes)]
self.len_open_pattern_pos = len(self.open_pattern_pos)
self.len_close_pattern_pos = len(self.close_pattern_pos)
......@@ -212,8 +211,6 @@ class ProcessedString():
new_chunk
)
if self.ressource == "4__12-14-2018-23-58__escape":
print(self.string)
self.__init__(self.string, self.ressource, process_escapes=True)
class Processor():
......@@ -344,11 +341,7 @@ class Processor():
def process(self, pre_processed, escape=False, safe_process=False):
op, cp, lo, lc, string = pre_processed.open_pattern_pos, pre_processed.close_pattern_pos, pre_processed.len_open_pattern_pos, pre_processed.len_close_pattern_pos, pre_processed.string
if safe_process and pre_processed.backup == None:
try:
pre_processed.backup = (list(op), list(cp), lo, lc, str(pre_processed.string))
except Exception as e:
print(op, cp, lo, lc, string)
pre_processed.backup = (list(op), list(cp), lo, lc, str(pre_processed.string))
if lo == 0 and lc == 0:
op, cp = sorted(op+pre_processed.bop), sorted(cp+pre_processed.bcp)
......
......@@ -49,8 +49,6 @@ class Thread:
self.column_opening = '<div id="__VENC_COLUMN_{0}__" class="__VENC_COLUMN__">'
self.column_closing = "</div>"
self.columns_number = self.datastore.blog_configuration["columns"]
self.current_page = 0
# Setup pattern processor
self.processor = Processor()
for pattern_name in patterns.keys():
......@@ -63,7 +61,6 @@ class Thread:
def return_page_around(self, string, params):
return string.format(**params)
# Must be called in child class
def get_relative_location(self, argv):
return self.export_path[5:]
......@@ -272,6 +269,7 @@ class Thread:
# Must be called in child class
def do(self):
self.current_page = 0
self.page_number = 0
if self.pages_count == 0:
self.processor.process(self.header)
......
......@@ -120,10 +120,10 @@ class EntriesThread(Thread):
)
stream.write(output)
stream.close()
def do(self):
self.page_number = 0
self.current_page = 0
if len(self.pages):
for page in self.pages:
for entry in page:
......
#! /usr/bin/python
import time
iteration = 10000
string = ".:foo::bar:. .:IfInThread:: .:boo:: .:foo::bar:. :. :: lolilol :. .:END:. .:TEST AGAIN:: .:WITHIN:. :."
string = ".:IfInThread::<link rel=\"stylesheet\" href=\".:GetRelativeOrigin:.styleThread.css\" type=\"text/css\" />::<link rel=\"stylesheet\" href=\".:GetRelativeOrigin:.styleEntry.css\" type=\"text/css\" />:."
def algo1():
op = []
cp = []
i = 0
l = len(string)
output = string
offset=0
while i < l:
i = output.find(".:", i)
if i == -1:
i=0
break
op.append(i)
i+=2
while i < l:
i = output.find(":.", i)
if i == -1:
i=0
break
cp.append(i)
i+=2
lo = len(op)
lc = len(cp)
if lo != lc:
raise Exception
while lo:
diff = 18446744073709551616
i = 0
j = 0
for io in range(0, lo):
for ic in range(0, lc):
d = cp[ic] - op[io]
if d > 0 and d < diff:
diff = d
i = io
j = ic
vop, vcp = op[i], cp[j]
fields = [field.strip() for field in output[vop+2:vcp].split("::") if field != '']
new_string = "["+'---'.join(fields)+"]"
# d0 shit there
output = output[0:vop] + new_string + output[vcp+2:]
offset = len(new_string) - (vcp + 2 - vop)
for k in range(1,lo):
if k >= i+1:
op[k]+=offset
cp[k]+=offset
op.pop(i)
cp.pop(j)
lo -= 1
lc -= 1
return output
def algo2(string):
op = []
cp = []
l = len(string)
fields=[]
for i in range(0, l):
if string[i:i+2] == ".:":
op.append(i)
elif string[i:i+2] == ":.":
cp.append(i)
if len(cp) <= len(op) and len(cp) != 0 and len(op) != 0:
if op[-1] < cp[-1]:
fields = [field.strip() for field in string[op[-1]+2:cp[-1]].split("::") if field != '']
return algo2(string[:op[-1]]+"["+'---'.join(fields)+"]"+string[cp[-1]+2:])
return string
t = time.time()
for i in range(0,iteration):
c = algo1()
print(c)
print("ALGO1", time.time()-t)
t = time.time()
for j in range(0,iteration):
s = algo2(string)
print(s)
print("ALGO2", time.time()-t)
#! /usr/bin/python3
print("Testing Patterns processing...")
from venc2.patterns.processor import ProcessedString # The object holding the string and its states
from venc2.patterns.processor import Processor # The actual string processor, holding binded methods.
testname_input_output = [
(
"Simple pattern detection",
"moo .:add::1::1:. foo",
"moo 2 foo",
False
),
(
"Match two patterns",
"moo .:add::1::1:. foo .:mul::2::2:. bar",
"moo 2 foo 4 bar",
False
),
(
"Recursive Patterns",
"moo .:greater:: .:add::1::1:. :: .:mul::2::2:. :. bar",
"moo 4 bar",
False
),
(
"Recursive Patterns plus extra trailing pattern",
"moo .:mul::3::3:. .:greater:: .:add::1::1:. :: .:mul::2::2:. :. foo .:greater::1::2:. bar",
"moo 9 4 foo 2 bar",
False
),
(
"Recursive Patterns (same call)",
"moo .:greater:: .:greater::1::2:. :: 0 :. bar",
"moo 2 bar",
False
),
(
"Test against blacklisted pattern",
"moo .:blacklisted:. foo .:greater::3::5:. bar",
"moo .:blacklisted:. foo 5 bar",
False
),
(
"Simple Escaping surrounded by simple patterns. Escape/EndEscape left intact.",
"moo .:add::1::1:. foo .:Escape:: .:mul::1::1:. ::EndEscape:. boo .:mul::2::2:. bar",
"moo 2 foo .:Escape:: .:mul::1::1:. ::EndEscape:. boo 4 bar",
False
),
(
"Simple Escaping surrounded by simple patterns.",
"moo .:add::1::1:. foo .:Escape:: .:mul::1::1:. ::EndEscape:. boo .:mul::2::2:. bar",
"moo 2 foo .:mul::1::1:. boo 4 bar",
True
)
]
def add(argv):
a, b = tuple(argv)
return str( int(a) + int(b) )
def mul(argv):
a, b = tuple(argv)
return str( int(a) * int(b) )
def greater(argv):
a, b = tuple(argv)
return a if float(a) > float(b) else b
processor = Processor()
processor.set_function("add", add)
processor.set_function("mul", mul)
processor.set_function("greater", greater)
processor.blacklist.append("blacklisted")
for test in testname_input_output:
test_name, i, o, process_escapes = test
ps = ProcessedString(i, test_name, process_escapes)
processor.process(ps)
assert o == ps.string, test_name
print("\t", test_name, "pass")
print("Done.")
#! /usr/bin/python3
print("Testing Patterns processing...", end=' ')
from venc2.patterns.processor import ProcessedString # The object holding the string and its states
from venc2.patterns.processor import Processor # The actual string processor, holding binded methods.
def add(argv):
a, b = tuple(argv)
return str( int(a) + int(b) )
def mul(argv):
a, b = tuple(argv)
return str( int(a) * int(b) )
def greater(argv):
a, b = tuple(argv)
return a if float(a) > float(b) else b
def blacklisted(argv):
return "blacklisted pattern is now running!"
def forbidden(argv):
return "forbidden pattern is now running!"
processor = Processor()
processor.set_function("add", add)
processor.set_function("mul", mul)
processor.set_function("greater", greater)
processor.set_function("blacklisted", greater)
# Simple pattern detection
ps = ProcessedString("moo .:add::1::1:. foo", "Test 1")
processor.process(ps)
assert("moo 2 foo" == ps.string)
# Match two patterns
ps = ProcessedString("moo .:add::1::1:. foo .:mul::2::2:. bar", "Test 2")
processor.process(ps)
assert("moo 2 foo 4 bar" == ps.string)
# Recursive Patterns
ps = ProcessedString("moo .:greater:: .:add::1::1:. :: .:mul::2::2:. :. bar", "Test 3")
processor.process(ps)
assert("moo 4 bar" == ps.string)
# Recursive Patterns
ps = ProcessedString("moo .:greater:: .:add::1::1:. :: .:mul::2::2:. :. foo .:greater::1::2:. bar", "Test 4")
processor.process(ps)
assert("moo 4 foo 2 bar" == ps.string)
# Recursive Patterns 2
ps = ProcessedString("moo .:greater:: .:greater::1::2:. :: 0 :. bar", "Test 5")
processor.process(ps)
assert("moo 2 bar" == ps.string)
# Test against blacklisted pattern
processor.blacklist.append("blacklisted")
ps = ProcessedString("moo .:blacklisted:. foo .:greater::3::5:. bar", "Test 5")
processor.process(ps)
assert("moo .:blacklisted:. foo 5 bar" == ps.string)
print("Done.")
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment