GEOUNIVERSAL .:.


log - geocad - iching

log.py v1

here is what i have so far for this log making machine.

import markdown
md = markdown.Markdown(extensions = ['meta', 'attr_list'])
import os

class WebPage:
    _header_template = '../templates/header.txt'
    _footer_template = '../templates/footer.txt'
    _save_path = '../../log/{0}.html'

    def __init__(self, page_name):
        self.tags = []
        with open(WebPage._header_template, 'r') as template:
            self._header = template.read()
        with open(WebPage._footer_template, 'r') as template:
            self._footer = template.read()
        self._page_name = page_name
        print(self._page_name)
        self._body = ''

    def _generate_body(self):
        with open(logpath + self._page_name + '.txt', 'r') as file:
            content = md.convert(file.read())
        link_tags = self._generate_link_tags()
        date = self._get_date()
        return '<section>' + content + '<a href="' + self._page_name + '.html">.:.</a> ' + link_tags + date + '</section>'

    def _generate_link_tags(self):
        if 'tags' in md.Meta:
            self.tags = md.Meta['tags'][0].split(', ')
            tag_list.update(self.tags)
            print(self.tags)
            anchor_tag = '<a href="{0}.html">{0}</a>'
            anchors = [anchor_tag.format(tag) for tag in self.tags]
            return ', '.join(anchors)
        else:
            return ''

    def _get_date(self):
        if 'date' in md.Meta:
            return ' ' + md.Meta['date'][0]
        else:
            return ''

    @property
    def as_html(self):
        return self._header + self._body + self._footer

    @property
    def body(self):
        return self._body

    def write_to_file(self):
        with open(WebPage._save_path.format(self._page_name), 'w') as file:
            file.write(self.as_html)


def search_page(list, key, value):
  for item in list:
    if item[key] == value:
      return item['webpage']

logpath = '../md/'
logpile = sorted(os.listdir(logpath))
logbook = []
blog = ''
tag_list = set()

# convert each log

for log in logpile:
  fullpath = os.path.join(logpath, log)

  if os.path.isdir(fullpath): 
    continue
  else: 
    filetype = log.split('.')[-1]
    pagename = log.split('.')[0]

  if filetype == 'txt':
    logbook.append({'name': pagename, 'webpage': WebPage(pagename)})

# make each individual page

logbook.reverse()
for page in logbook:
    page['webpage']._body = page['webpage']._generate_body()
    page['webpage'].write_to_file()
    page['page_tags'] = page['webpage'].tags
    blog += '\n\n' + page['webpage']._body

# add all blogs to an index

linked_tags = ['<a href="{0}.html">{0}</a>'.format(tag) for tag in tag_list]
index_intro = '<h2>logs of geouniversal</h2>view by tag: ' + ', '.join((linked_tags))
logbook.append({'name': 'index', 'webpage': WebPage('index')})
index = search_page(logbook, 'name', 'index')
index._body = '<section>' + index_intro + '</section>' + blog
index.write_to_file()

# make page for each tag

for tag in tag_list:
  logbook.append({'name': tag, 'webpage': WebPage(tag)})
  new_tag_page = search_page(logbook, 'name', tag)
  for page in logbook:
    if tag in page['webpage'].tags:
      new_tag_page._body += page['webpage']._body 
  new_tag_page.write_to_file()
.:. 2021-12-06 12:05:58 webmaster