def test_get_formatted_date(): default_date_format = {'format': 'MMMM Do YYYY, h:mm:ss a Z'} formatted_date = get_formatted_date('2012-12-18', default_date_format) assert formatted_date == "December 18th 2012, 12:00:00 am +0000" default_date_format = {'format': 'DD MMM Y'} formatted_date = get_formatted_date('2012-12-18', default_date_format) assert formatted_date == "18 Dec Y"
def insert(self): if DEBUG: print('Adding date...') try: formatted_date = get_formatted_date(self.section.contents, self.section.layout) except ParserError as e: formatted_date = 'n/a' insert_text(self.cell_object, formatted_date)
def test_get_formatted_date_invalid(): default_date_format = '' with pytest.raises(ParserError): get_formatted_date('wowowowow', default_date_format)
def test_get_formatted_date_now_iso(): default_date_format = '' formatted_date = get_formatted_date('', default_date_format) now = arrow.now() assert formatted_date[0:10] == now.isoformat()[0:10]
def test_get_formatted_date_now(): default_date_format = {'format': 'DD MMM YYYY'} formatted_date = get_formatted_date('', default_date_format) now = arrow.now() assert formatted_date == now.format(default_date_format['format'])
def test_get_formatted_date_iso(): default_date_format = '' formatted_date = get_formatted_date('2012-12-18', default_date_format) assert formatted_date == "2012-12-18T00:00:00+00:00"
def wrap(self, invoked_from_wrapper=False): # Handle called from another wrapper. md_section_list = None if isinstance(self.section.contents, list): md_section_list = self.section.contents elif invoked_from_wrapper and \ isinstance(self.section.contents.contents, str): md_section_list = [self.section.contents] if not isinstance(md_section_list, list): raise ValueError('Markdown section does not have valid contents ' + '(must be a list)') for section in md_section_list: # === Start wrappers === if section.type == MD_TYPE_DIV: temp_section = MarkdownSection('markdown', section.contents, {}, {}) invoke(self.cell_object, temp_section) continue if section.type == MD_TYPE_CODE: md_code.invoke(self.cell_object, section) self.cell_object.update_paragraph() continue if section.type == MD_TYPE_QUOTE: md_blockquote.invoke(self.cell_object, section) self.cell_object.update_paragraph() continue if section.type == MD_TYPE_UNORDERED_LIST: md_ul.invoke(self.cell_object, section) self.cell_object.update_paragraph() continue if section.type == MD_TYPE_ORDERED_LIST: md_ol.invoke(self.cell_object, section) self.cell_object.update_paragraph() continue if section.type == MD_TYPE_LIST_ITEM: md_li.invoke(self.cell_object, section) continue if section.type == MD_TYPE_TABLE: table_html = section.extra['original_html'] t = PyQuery(table_html) headers = [i.find('th') for i in t.find('tr').items()][0] headers = [c.text() for c in headers.items()] rows = [ i.find('td') for i in t.find('tr').items() if i.find('td') ] data = [] for row in rows: r = { headers[i]: c.text() for i, c in enumerate(row.items()) } data.append(r) s = Section("table", data, {"tableColumns": headers}, {}) table.invoke(self.cell_object, s) continue # Fix wrapped: # (Some times there are elements which contain other elements, # but are not considered one of the declared wrappers) if isinstance(section.contents, list): is_inside_wrapper = False if 'inline' in section.extra: is_inside_wrapper = True if section.type == 'span': section.propagate_extra('check_newline', True, only_multiple_children=False) # TODO: Fix problem with H1 no newline even if in span. temp_section = MarkdownSection('markdown', section.contents, {}, section.extra, section.attrs) invoke(self.cell_object, temp_section, invoked_from_wrapper=is_inside_wrapper) continue # === Elements === if section.type in SHOULD_NEW_LINE and section.get_extra( 'check_newline'): self.cell_object.add_paragraph() if section.type == MD_TYPE_HORIZONTAL_LINE: md_hr.invoke(self.cell_object, section) continue # Add a block (newline) if not called from a wrapper # (Should come after hr) if not invoked_from_wrapper: self.cell_object.add_paragraph() if section.type in MD_TYPES_HEADERS: # We want to keep the h{1...6} for styling insert_header(self.cell_object, section.contents, header=section.type) continue if section.type in [MD_TYPE_TEXT, MD_TYPE_INLINE_TEXT]: if invoked_from_wrapper: self.cell_object.add_run() if not section.contents: continue if '{date}' in section.contents: try: formatted_date = get_formatted_date('', section.layout) except ParserError as e: formatted_date = 'n/a' section.contents = section.contents.replace( '{date}', formatted_date) insert_text(self.cell_object, section) continue if section.type == MD_TYPE_LINK: md_link.invoke(self.cell_object, section) continue if section.type == MD_TYPE_IMAGE: md_image.invoke(self.cell_object, section) continue raise ValueError(f'Section type is not defined: {section.type}')