diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-05-11 12:01:52 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-05-11 12:01:52 -0400 |
commit | 5e7629184d97a17b35ff2bb14415c291fcd7b573 (patch) | |
tree | 4c7ce48f3ca1877733eea9c939e6e203b33f46c8 | |
parent | 2bd9d7db2e724dbc544a5f7578171e9d2da6b2d0 (diff) | |
download | askbot-5e7629184d97a17b35ff2bb14415c291fcd7b573.tar.gz askbot-5e7629184d97a17b35ff2bb14415c291fcd7b573.tar.bz2 askbot-5e7629184d97a17b35ff2bb14415c291fcd7b573.zip |
parser for a simple category tree seems to work
-rw-r--r-- | askbot/conf/flatpages.py | 4 | ||||
-rw-r--r-- | askbot/views/writers.py | 62 |
2 files changed, 58 insertions, 8 deletions
diff --git a/askbot/conf/flatpages.py b/askbot/conf/flatpages.py index 6879e27c..543da67d 100644 --- a/askbot/conf/flatpages.py +++ b/askbot/conf/flatpages.py @@ -58,7 +58,7 @@ settings.register(#this field is not editable manually FLATPAGES, 'CATEGORY_TREE', description = 'Category tree',#no need to translate - default = '[[]]',#empty array of arrays in json - hidden = True + default = '',#empty array of arrays in json + #hidden = True ) ) diff --git a/askbot/views/writers.py b/askbot/views/writers.py index d4dea8f0..4fcdf81c 100644 --- a/askbot/views/writers.py +++ b/askbot/views/writers.py @@ -260,11 +260,66 @@ def ask(request):#view used to ask a new question 'is_anonymous': request.REQUEST.get('is_anonymous', False), } + def get_subtree(tree, path): + if len(path) == 1: + assert(path[0] == 0) + return tree + else: + import copy + parent_path = copy.copy(path) + leaf_index = parent_path.pop() + branch_index = parent_path[-1] + parent_tree = get_subtree(tree, parent_path) + return parent_tree[branch_index][1] + + def parse_tree(text): + """parse tree represented as indented text + one item per line, with two spaces per level of indentation + """ + lines = text.split('\n') + import re + in_re = re.compile(r'^([ ]*)') + + tree = [['dummy', []]] + subtree_path = [0] + clevel = 0 + + for line in lines: + if line.strip() == '': + continue + match = in_re.match(line) + level = len(match.group(1))/2 + 1 + + if level > clevel: + import pdb + pdb.set_trace() + subtree_path.append(0)# + elif level < clevel: + subtree_path = subtree_path[:level+1] + leaf_index = subtree_path.pop() + subtree_path.append(leaf_index + 1) + else: + leaf_index = subtree_path.pop() + subtree_path.append(leaf_index + 1) + + clevel = level + try: + subtree = get_subtree(tree, subtree_path) + except: + return tree + subtree.append([line.strip(), []]) + + return tree + + if askbot_settings.TAG_SOURCE == 'category-tree': - category_tree_data = askbot_settings.CATEGORY_TREE + cat_tree = parse_tree(askbot_settings.CATEGORY_TREE) + category_tree_data = simplejson.dumps(cat_tree) else: category_tree_data = None + """ + example of desired structure cat_tree = [ ['dummy', [ @@ -286,11 +341,6 @@ def ask(request):#view used to ask a new question ] ] """ - cat_tree = [ - ['dummy', []] - ] - """ - category_tree_data = simplejson.dumps(cat_tree) data = { 'active_tab': 'ask', |