def parse(self): super(simpleDefsExamplesSection, self).parse() word = self.get_property('word') lines = self.text.splitlines() definitions = [] examples = [] example_list = None early_examples = False # We expect example sentences to come after the appropriate definitions. # If they come before any definitions they are lumped in with the first # and an alert is raised. for line in lines: # Extract the definitions and examples if line.startswith('#:'): if example_list is None: early_examples = True example_list = [] example_list.append(line[2:]) elif line.startswith('#'): if definitions: examples.append(example_list) definitions.append(line[1:]) if not (early_examples and not examples): example_list = [] else: # FIX ME: better error raise StandardError('Each line should begin with # or #:') if definitions: examples.append(example_list) if early_examples: page_title = self.get_property('page').title message = '%s: A example preceded the first definition.' % ( page_title) alert = EarlyExample(message=message, title=page_title) self.alerts.append(alert) # Convert wikitext to plain text. plain_definitions = [] for definition in definitions: text, alerts = w2p(definition, template_block=simpleTemplateBlock) self.alerts += alerts plain_definitions.append(text.strip(' &')) plain_examples = [] for example_list in examples: plain_example_list = [] for example in example_list: text, alerts = w2p(example, template_block=simpleTemplateBlock) self.alerts += alerts plain_example_list.append(text.strip(' &')) plain_examples.append(plain_example_list) word.definitions = plain_definitions word.examples = plain_examples return self
def summary(self): out = [] heading = '%s (%s)' % (self.title, self.typeslug) out.append('*' * len(heading)) out.append(heading) out.append('*' * len(heading)) if self.definitions: out.append('Definitions:') for i, defi in enumerate(self.definitions): out.append('[%s] ' % (i+1) + w2p(defi)) if self.examples: out.append('Examples:') for i, exams in enumerate(self.examples): for ex in exams: out.append('[%s] ' % (i+1) + w2p(ex)) return '\n'.join(out)
def str_definitions_and_examples(self, template_block={}): # Should return alerts out = [] example_lists = self.examples len_diff = len(self.definitions) - len(example_lists) if len_diff > 0: example_lists += [[]] * len_diff for definition, examples in zip(self.definitions, example_lists): definition = w2p(definition, template_block=template_block) out.append(definition.strip()) for example in examples: example = w2p(example, template_block=template_block) out.append(' - %s' % example.strip()) out = '\n'.join(out) out = w2p(out) return out
def parse(self): super(BeispieleSection, self).parse() word = self.get_property('word') if word is None: return self content = self.text.lstrip(' \n\r\t').rstrip(' \n\r\t') examples = [] for line in content.split('\n'): if word is not None: # FIXME (try to associate examples with definitions). text, alerts = w2p(content) self.alerts += alerts examples.append(line) # Work out which meaning the examples go with. pattern = '^:\[(?P<defnum>\d+)\](?P<remainder>.*)' w_examples = [[] for d in word.definitions] for example in examples: match = re.match(pattern, example) if match: gd = match.groupdict() defnum = int(gd['defnum']) - 1 if defnum >= len(word.definitions): message = u'The example "{0}" started with a number with no matching definition.' message.format(example) alert = BedeutungenNumberAlert(message, word.title) self.alerts.append(alert) else: w_examples[defnum].append(gd['remainder']) else: message = u'The example "{0}" did not start with ":[number]".' message.format(example) alert = BedeutungenNumberAlert(message, word.title) self.alerts.append(alert) word.examples = w_examples return self
def summary(self): out = [] heading = '%s (%s)' % (self.title, self.typeslug) out.append('*' * len(heading)) out.append(heading) out.append('*' * len(heading)) if self.definitions: out.append('Definitions:') for i, defi in enumerate(self.definitions): out.append('[%s] ' % (i + 1) + w2p(defi)) if self.examples: out.append('Examples:') for i, exams in enumerate(self.examples): for ex in exams: out.append('[%s] ' % (i + 1) + w2p(ex)) return '\n'.join(out)
def parse(self): super(BedeutungenSection, self).parse() word = self.get_property('word') content = self.text.lstrip(' \n\r\t').rstrip(' \n\r\t') definitions = [] for line in content.split('\n'): if word is not None: text, alerts = w2p(line) self.alerts += alerts # Check that the definition starts with the appropriate number. defnum = len(definitions)+1 expected_start = ":[{0}]".format(defnum) if text.startswith(expected_start): text = text[len(expected_start):] definitions.append(text) else: message = u'The meaning is "{0}" and did not start with "{1}" as expected.' message = message.format(text, expected_start) alert = BedeutungenNumberAlert(message, word.title) self.alerts.append(alert) definitions.append(text) if word is not None: word.definitions = definitions return self
def parse(self): super(BedeutungenSection, self).parse() word = self.get_property('word') content = self.text.lstrip(' \n\r\t').rstrip(' \n\r\t') definitions = [] for line in content.split('\n'): if word is not None: text, alerts = w2p(line) self.alerts += alerts # Check that the definition starts with the appropriate number. defnum = len(definitions) + 1 expected_start = ":[{0}]".format(defnum) if text.startswith(expected_start): text = text[len(expected_start):] definitions.append(text) else: message = u'The meaning is "{0}" and did not start with "{1}" as expected.' message = message.format(text, expected_start) alert = BedeutungenNumberAlert(message, word.title) self.alerts.append(alert) definitions.append(text) if word is not None: word.definitions = definitions return self