Ejemplo n.º 1
0
def submit_slot_with_text(request):
    short_title = request.POST['short_title']
    if not  short_title:
        return "ERROR: Short Title darf nicht leer sein."
    parent_id = int(request.POST['parent_id'])
    # lookup if slot exists
    slots = Slot.objects.filter(parent__id=parent_id, short_title=short_title)
    if slots:
        s = slots[0]
    else :
        s = Slot()
        s.short_title = request.POST['short_title']
        s.parent_id = parent_id
        s.save()

    t = TextNode()
    t.text = request.POST['text']
    t.parent = s
    t.save()
    add_auto_upvote(request.user, t)
    return HttpResponseRedirect(t.getTextPath())
Ejemplo n.º 2
0
def createSlot(parent, short_title, text=None):
    global nodeDict
    s = Slot()
    s.short_title = short_title
    s.parent = lookupNode(parent, StructureNode)
    s.save()
    nodeDict[s.getTextPath()] = s
    if text is not None:
        return s, createText(s, text)
    else :
        return s
Ejemplo n.º 3
0
 def setUp(self):
     self.root = getRootNode()
     self.test_slot = Slot()
     self.test_slot.short_title = "TestSlot"
     self.test_slot.parent = self.root
     self.test_slot.save()
Ejemplo n.º 4
0
class StructureParserTest(unittest.TestCase):
    def setUp(self):
        self.root = getRootNode()
        self.test_slot = Slot()
        self.test_slot.short_title = "TestSlot"
        self.test_slot.parent = self.root
        self.test_slot.save()

    def test_single_heading_results_in_TextNode(self):
        s = """
        = Heading1 =
        Some Text.
        """
        n = parse(s, self.test_slot)
        self.assertEqual(n.getType(), "TextNode")

    def test_three_headings_results_in_TextNode(self):
        s = "Introduction\n==== Heading 1 ====\nMy name is Harry.\n=== Heading 2 ===\nI'm quite an idiot.\n==  Heading 3   ==\n\nI reversed the ordering of the headings."
        titles = ["Einleitung", "Heading_3"]
        textparts = ["= TestSlot =\nIntroduction\n~==== Heading 1 ====\nMy name is Harry.\n~=== Heading 2 ===\nI'm quite an idiot.\n",
                     "= Heading 3 =\nI reversed the ordering of the headings."]
        n = parse(s, self.test_slot)
        self.assertEqual(n.getType(), "StructureNode")
        for i, slot in enumerate(n.slot_set.all()):
            self.assertEqual(slot.getType(), "Slot")
            self.assertEqual(slot.short_title, titles[i])
            self.assertEqual(slot.getText(), textparts[i])

    def test_multilayer(self):
        s = """
        = Long Title =
        Banana.
        == Holla ==
        Elmtree
        === Hiracical ===
        Exceptionally good.
        == Julliosh ==
        Pineapple
        """
        n = parse(s, self.test_slot)
        self.assertEqual(n.getType(), "StructureNode")
        titles = ["Einleitung", "Holla", "Julliosh"]
        textparts = ["Banana", "Elmtree", "Pineapple"]
        for i, slot in enumerate(n.slot_set.all()):
            self.assertEqual(slot.getType(), "Slot")
            self.assertEqual(slot.short_title, titles[i])
            self.assertGreaterEqual(slot.getText().find(textparts[i]), 0)
            if i == 1:
                for x in slot.node_set.all():
                    self.assertEqual(x.as_leaf_class().getType(), "StructureNode")
                    self.assertEqual(x.as_leaf_class().slot_set.all()[1].short_title, "Hiracical")
                    self.assertGreaterEqual(x.as_leaf_class().getText().find("Exceptionally good."), 0)

    def test_long_title_short_title(self):
        s = """
        = Heading1§Mama =
        Some text.
        == Heading 2§Papa ==
        More text.
        """
        n = parse(s, self.test_slot)
        self.assertEqual(n.getType(), "StructureNode")
        self.assertNotEqual(n.getShortTitle(), "Mama")
        self.assertEqual(n.slot_set.all()[0].short_title, "Einleitung")
        self.assertEqual(n.slot_set.all()[1].short_title, "Papa")
        self.assertGreaterEqual(n.slot_set.all()[1].getText().find("Heading 2"), 0)
Ejemplo n.º 5
0
def parse(s, parent_slot):
    #make sure we start with a heading 1
    m = h1_start.match(s) # TODO: match short titles and warn about
    if m :
        title = m.groups("title")[0]
        title = title.partition("§")[0] # silently remove attempt to set short_title in H1
        s = h1_start.sub("", s)
    else :
        # TODO: warn about missing title
        title = parent_slot.short_title

    # do we need a StructureNode or will a TextNode do?
    if not general_h.search(s) :
        # TextNode
        t = TextNode()
        t.text = "= %s =\n"%title.strip() + s.strip()
        t.parent = parent_slot
        t.save()
        return t
    # else : StructureNode

    node = StructureNode()
    node.parent = parent_slot
    node.save()

    # determine used header depth:
    level = 0
    for i in range(2, 7):
        m = getHeadingMatcher(i)
        if m.search(s):
            level = i
            break
    assert 1 < level < 7

    split_doc = m.split(s)
    # now the text before, between and after headings is split_doc[0::3]
    # the text of the headings are split_doc[1::3]
    # and the short titles (or None if omitted) are split_doc[2::3]
    # what do we do now?

    # leading text is used to construct an "Einleitung" Slot and TextNode
    introduction = Slot()
    introduction.parent = node
    introduction.short_title = "Einleitung"
    introduction.save()
    introduction_text = TextNode()
    introduction_text.parent = introduction
    intro_text = split_doc[0]
    # assert that no headings are in intro-text
    if general_h.search(intro_text):
        # TODO: Warn!
        intro_text = general_h.sub(r"~\1", intro_text)
        #general_h.
    introduction_text.text = "= %s =\n"%title + intro_text
    introduction_text.save()


    # iterate the headings, short_titles, and corresponding texts:
    short_title_set = set()
    for title, short_title, text in zip(split_doc[1::3], split_doc[2::3], split_doc[3::3]):
        # check if short_title is valid/unique/exists
        if not short_title or len(short_title.strip()) == 0:
            short_title=title[:min(15, len(title))]

        # make short titles valid
        short_title = short_title[:min(20, len(short_title))]
        short_title = strip_accents(short_title)
        short_title = invalid_symbols.sub('',short_title)
        if short_title in short_title_set:
            i = 1
            while short_title + str(i) in short_title_set:
                i += 1
            short_title += str(i)

        short_title_set.add(short_title)
        slot = Slot()
        slot.parent = node
        slot.short_title = short_title.strip().replace(" ", "_")
        slot.save()
        parse("= %s =\n"%title.strip() + text.strip(), slot)
    return node