def fill_date_fields(template: Template, date: datetime = DateField.TODAY) -> None: """ Populate all date fields in the template. A 'date' field provides an easy way of putting the current date into a template. A date field uses built-in Python date formats, and should look like this: '{{ date }}' - using default formatting '{{ date '%d, %b %Y' }}' - using custom formatting See all supported format identifiers here http://strftime.org """ def next_date_field(): """ Return the next probable date field. """ return first(fields(template.content, with_name_like='date')) field = next_date_field() while field is not None: # default date format: January 04, 2018 # note that supported format specifiers may be different depending on platform (e.g. Windows or MacOS), # and, as such, neat formatting like using %-d instead of %d to remove zero-padding is not viable date_format = '%B %d, %Y' if field.context is not None: # a date field can have a custom format custom_date_format = dequote(field.context).strip() if len(custom_date_format) > 0: # if found, we'll use that and let date.strftime handle it date_format = custom_date_format formatted_date = date.strftime(date_format) # populate the include field with the content; or blank if unresolved fill(field, formatted_date, template) field = next_date_field()
def fill_date_fields(template: Template, date: datetime=DateField.TODAY) -> None: """ Populate all date fields in the template. A 'date' field provides an easy way of putting the current date into a template. A date field uses built-in Python date formats, and should look like this: '{{ date }}' - using default formatting '{{ date '%d, %b %Y' }}' - using custom formatting See all supported format identifiers here http://strftime.org """ def next_date_field(): """ Return the next probable date field. """ return first(fields(template.content, with_name_like='date')) field = next_date_field() while field is not None: # default date format: 07 Oct, 2016 date_format = '%B %-d, %Y' if field.context is not None: # a date field can have a custom format custom_date_format = dequote(field.context).strip() if len(custom_date_format) > 0: # if found, we'll use that and let date.strftime handle it date_format = custom_date_format formatted_date = date.strftime(date_format) # populate the include field with the content; or blank if unresolved fill(field, formatted_date, template) field = next_date_field()
def fill_include_fields(template: Template) -> None: """ Populate all include fields in the template. An 'include' field provides a way of putting re-usable template content into a separate file, and including it in place of the field. An include field should look like this: '{{ include 'path/to/file.html' }}' """ original_template_content = template.content def next_include_field(): """ Return the next probable include/inline field. """ return first(fields(template.content, with_name_like='include|inline')) field = next_include_field() while field is not None: is_include_command = field.name == TemplateFields.INCLUDE is_inline_command = field.name == TemplateFields.INLINE # default to blank include_content = '' include_path = None if field.context is not None: # the field should contain a path include_path = dequote(field.context).strip() if include_path is not None and len(include_path) > 0: if not os.path.isabs(include_path): # it's not an absolute path, so we should make it a relative path if template.path is not None: # make the path relative to the path of the containing template include_path = os.path.join( os.path.dirname(template.path), include_path) if os.path.isfile(include_path): # we've ended up with a path that can be opened with open(include_path) as include_file: if is_include_command: # open it and read in the entire contents as is include_content = include_file.read().strip() elif is_inline_command: # read each line for line in include_file.readlines(): # stripping excess whitespace and newline in the process include_content += line.strip() else: WarningDisplay.included_file_not_found_error( WarningContext(os.path.basename(template.path)), include_path) include_content = '<strong><included file not found></strong>' else: WarningDisplay.include_should_specify_file( WarningContext('{0}:{1}'.format( os.path.basename(template.path), # todo: the line number could potentially be incorrect, as we might not be going # through the original template anymore- the lineno can only serve as a hint get_line_number(field.indices.start, original_template_content))), is_inline=is_inline_command) # populate the include field with the content; or blank if unresolved fill(field, include_content, template, indenting=is_include_command) field = next_include_field()
def fill_include_fields(template: Template) -> dict: """ Populate all include fields in the template. An 'include' field provides a way of putting re-usable template content into a separate file, and including it in place of the field. An include field should look like this: '{{ include 'path/to/file.html' }}' """ original_template_content = template.content def next_include_field(): """ Return the next probable include/inline field. """ return first(fields(template.content, with_name_like='include|inline')) field = next_include_field() stripped_styles = {} while field is not None: is_include_command = field.name == TemplateFields.INCLUDE is_inline_command = field.name == TemplateFields.INLINE # default to blank include_content = '' include_path = None if field.context is not None: # the field should contain a path include_path = dequote(field.context).strip() if include_path is not None and len(include_path) > 0: if not os.path.isabs(include_path): # it's not an absolute path, so we should make it a relative path if template.path is not None: # make the path relative to the path of the containing template include_path = os.path.join(os.path.dirname(template.path), include_path) if os.path.isfile(include_path): # we've ended up with a path that can be opened with open(include_path) as include_file: if is_include_command: # open it and read in the entire contents as is include_content = include_file.read().strip() elif is_inline_command: # read each line for line in include_file.readlines(): # stripping excess whitespace and newline in the process include_content += line.strip() else: WarningDisplay.included_file_not_found_error( WarningContext(os.path.basename(template.path)), include_path) include_content = '<strong><included file not found></strong>' stripped_template = Template(include_content) stripped_styles[include_path] = strip_styles(stripped_template) include_content = stripped_template.content else: WarningDisplay.include_should_specify_file( WarningContext('{0}:{1}'.format( os.path.basename(template.path), # todo: the line number could potentially be incorrect, as we might not be going # through the original template anymore- the lineno can only serve as a hint get_line_number(field.indices.start, original_template_content))), is_inline=is_inline_command) # populate the include field with the content; or blank if unresolved fill(field, include_content, template, indenting=is_include_command) field = next_include_field() return stripped_styles