Skip to content
Permalink
Browse files

Moved parsing/joining code to utils.py, and fixed a little bug.

  • Loading branch information
felixfontein committed May 12, 2015
1 parent 374fd3c commit 366a0afbcad1f9894a416e7af2be677e7d7a18bc
Showing with 44 additions and 34 deletions.
  1. +6 −34 nikola/nikola.py
  2. +38 −0 nikola/utils.py
@@ -1357,45 +1357,17 @@ def flatten(task):

def parse_category_name(self, category_name):
if self.config['CATEGORY_ALLOW_HIERARCHIES']:
result = []
current = ''
index = 0
next_backslash = category_name.find('\\', index)
next_slash = category_name.find('/', index)
while index < len(category_name):
if next_backslash == -1 and next_slash == -1:
current += category_name[index:]
index = len(category_name)
elif next_slash >= 0 and (next_backslash == -1 or next_backslash > next_slash):
result.append(current + category_name[index:next_slash])
current = ''
index = next_slash + 1
next_slash = category_name.find('/', index)
else:
if len(category_name) == next_backslash + 1:
utils.LOGGER.error("Unexpected '\\' in '{0}' at last position!".format(category_name))
sys.exit(1)
esc_ch = category_name[next_backslash + 1]
if esc_ch not in {'/', '\\'}:
utils.LOGGER.error("Unknown escape sequence '\\{0}' in '{1}'!".format(esc_ch, category_name))
sys.exit(1)
current += category_name[index:next_backslash] + esc_ch
index = next_backslash + 2
next_backslash = category_name.find('\\', index)
if esc_ch == '/':
next_slash = category_name.find('/', index)
if len(current) > 0:
result.append(current)
return result
try:
return utils.parse_escaped_hierarchical_category_name(category_name)
except Exception as e:
utils.LOGGER.error(str(e))
sys.exit(1)
else:
return [category_name] if len(category_name) > 0 else []

def category_path_to_category_name(self, category_path):
if self.config['CATEGORY_ALLOW_HIERARCHIES']:
def escape(s):
return s.replace('\\', '\\\\').replace('/', '\\/')

return '/'.join([escape(p) for p in category_path])
return utils.join_hierarchical_category_path(category_path)
else:
return ''.join(category_path)

@@ -1573,3 +1573,41 @@ def generate(input_list, indent_levels_so_far):
if last_element is not None:
last_element.indent_change_after = -level
return elements


def parse_escaped_hierarchical_category_name(category_name):
result = []
current = None
index = 0
next_backslash = category_name.find('\\', index)
next_slash = category_name.find('/', index)
while index < len(category_name):
if next_backslash == -1 and next_slash == -1:
current = (current if current else "") + category_name[index:]
index = len(category_name)
elif next_slash >= 0 and (next_backslash == -1 or next_backslash > next_slash):
result.append((current if current else "") + category_name[index:next_slash])
current = ''
index = next_slash + 1
next_slash = category_name.find('/', index)
else:
if len(category_name) == next_backslash + 1:
raise Exception("Unexpected '\\' in '{0}' at last position!".format(category_name))
esc_ch = category_name[next_backslash + 1]
if esc_ch not in {'/', '\\'}:
raise Exception("Unknown escape sequence '\\{0}' in '{1}'!".format(esc_ch, category_name))
current = (current if current else "") + category_name[index:next_backslash] + esc_ch
index = next_backslash + 2
next_backslash = category_name.find('\\', index)
if esc_ch == '/':
next_slash = category_name.find('/', index)
if current is not None:
result.append(current)
return result


def join_hierarchical_category_path(category_path):
def escape(s):
return s.replace('\\', '\\\\').replace('/', '\\/')

return '/'.join([escape(p) for p in category_path])

0 comments on commit 366a0af

Please sign in to comment.
You can’t perform that action at this time.