# Path: DelogX/utils/parser.py
# class Markdown(object):
#     '''Include the markdown parser method.'''
# 
#     @classmethod
#     def markdown(cls, text, exts=None):
#         '''Parse markdown text to HTML5.
# 
#         Args:
# 
#             text (str): Raw markdown text.
#             ext (list of str/class): Extensions of markdown module.
# 
#         Returns:
# 
#             str: Parsed HTML text.
#         '''
#         ext_list = [
#             'markdown.extensions.attr_list',
#             'markdown.extensions.tables',
#             'markdown.extensions.fenced_code'
#         ]
#         if exts and isinstance(exts, (list, set, tuple)):
#             ext_list.extend(exts)
#         elif exts:
#             ext_list.append(exts)
#         output = markdown.markdown(
#             text,
#             output_format='html5',
#             tab_length=4,
#             extensions=ext_list
#         )
#         if not output:
#             output = '<p></p>'
#         return output
# 
# Path: DelogX/utils/path.py
# class Path(object):
#     '''Some useful path and URL utils and functions.'''
# 
#     @classmethod
#     def format_url(cls, *args):
#         '''Join strings to an URL and ensure the URL starts with a slash.
# 
#         Args:
# 
#             *args: strings need to be joined.
# 
#         Returns:
# 
#             str: Formatted URL.
#         '''
#         return os.path.join('/', *args).replace(os.path.sep, '/')
# 
#     @classmethod
#     def urlencode(cls, url):
#         '''Return an encoded URL.
# 
#         Args:
# 
#             url (str): URL needs to be encoded.
# 
#         Returns:
# 
#             str: Encoded URL.
#         '''
#         return urllib.parse.quote(url)
# 
#     @classmethod
#     def urldecode(cls, url):
#         '''Return an decoded URL.
# 
#         Args:
# 
#             url (str): URL needs to be decoded.
# 
#         Returns:
# 
#             str: Decoded URL.
#         '''
#         return urllib.parse.unquote(url)
# 
#     @classmethod
#     def abs_path(cls, prefix, path):
#         '''If the path is not an absolute path, convert it to absolute.
# 
#         Args:
# 
#             prefix (str): Directory to be added if the path is not absolute.
#             path (str): Path needs to check.
# 
#         Returns:
# 
#             str: Converted absolute path.
#         '''
#         if not os.path.isabs(path):
#             path = os.path.join(prefix, path)
#         return os.path.abspath(path)
# 
#     @classmethod
#     def read_file(cls, filename):
#         '''Read the content of a file (UTF-8).
# 
#         Args:
# 
#             filename (str): Name of the file.
# 
#         Returns:
# 
#             list of str: Content of the file.
#         '''
#         if not os.path.isfile(filename):
#             return None
#         with codecs.open(filename, encoding='utf-8') as some_file:
#             lines = some_file.readlines()
#         return lines
# 
#     @classmethod
#     def get_first_line(cls, lines):
#         '''Get the line number of the first non-empty line in a list.
# 
#         Args:
# 
#             lines (list of str): A list of text.
# 
#         Returns:
# 
#             int: If non-empty line exists return the line number, otherwise 0.
#         '''
#         for num, line in enumerate(lines):
#             if line.strip():
#                 return num
#         return 0


import os
import re
from DelogX.utils.parser import Markdown
from DelogX.utils.path import Path
    def load_meta(self):
        '''Load the meta of this item, include `hidden`, `url` and `title`.'''
        if not self.valid():
            return
        self.hidden = self.filename.startswith('.')
        self.url = os.path.splitext(self.filename)[0]
        if self.hidden:
            self.url = self.url[1:]
        atx_re = re.compile(r'^\s*#\s*([^#]+)\s*#*\s*$')
        setext_re = re.compile(r'^\s*=+\s*$')
        lines = Path.read_file(self.path)
        first_line = Path.get_first_line(lines)
        line1 = lines[first_line] if first_line < len(lines) else ''
        line2 = lines[first_line + 1] if first_line + 1 < len(lines) else ''
        line1 = line1.strip('\n').strip('\r')
        line2 = line2.strip('\n').strip('\r')
        atx_match = atx_re.match(line1)
        setext_match = setext_re.match(line2)
        if atx_match and atx_match.group(1):
            self.title = atx_match.group(1)
            self.title_line = first_line + 1
        elif setext_match and line1.strip():
            self.title = line1
            self.title_line = first_line + 2
        else:
            self.title = self.url
            self.title_line = 0

    def load_content(self):
        '''Load and parse the content of this item.'''

# Path: DelogX/utils/parser.py
# class Markdown(object):
#     '''Include the markdown parser method.'''
# 
#     @classmethod
#     def markdown(cls, text, exts=None):
#         '''Parse markdown text to HTML5.
# 
#         Args:
# 
#             text (str): Raw markdown text.
#             ext (list of str/class): Extensions of markdown module.
# 
#         Returns:
# 
#             str: Parsed HTML text.
#         '''
#         ext_list = [
#             'markdown.extensions.attr_list',
#             'markdown.extensions.tables',
#             'markdown.extensions.fenced_code'
#         ]
#         if exts and isinstance(exts, (list, set, tuple)):
#             ext_list.extend(exts)
#         elif exts:
#             ext_list.append(exts)
#         output = markdown.markdown(
#             text,
#             output_format='html5',
#             tab_length=4,
#             extensions=ext_list
#         )
#         if not output:
#             output = '<p></p>'
#         return output
# 
# Path: DelogX/utils/path.py
# class Path(object):
#     '''Some useful path and URL utils and functions.'''
# 
#     @classmethod
#     def format_url(cls, *args):
#         '''Join strings to an URL and ensure the URL starts with a slash.
# 
#         Args:
# 
#             *args: strings need to be joined.
# 
#         Returns:
# 
#             str: Formatted URL.
#         '''
#         return os.path.join('/', *args).replace(os.path.sep, '/')
# 
#     @classmethod
#     def urlencode(cls, url):
#         '''Return an encoded URL.
# 
#         Args:
# 
#             url (str): URL needs to be encoded.
# 
#         Returns:
# 
#             str: Encoded URL.
#         '''
#         return urllib.parse.quote(url)
# 
#     @classmethod
#     def urldecode(cls, url):
#         '''Return an decoded URL.
# 
#         Args:
# 
#             url (str): URL needs to be decoded.
# 
#         Returns:
# 
#             str: Decoded URL.
#         '''
#         return urllib.parse.unquote(url)
# 
#     @classmethod
#     def abs_path(cls, prefix, path):
#         '''If the path is not an absolute path, convert it to absolute.
# 
#         Args:
# 
#             prefix (str): Directory to be added if the path is not absolute.
#             path (str): Path needs to check.
# 
#         Returns:
# 
#             str: Converted absolute path.
#         '''
#         if not os.path.isabs(path):
#             path = os.path.join(prefix, path)
#         return os.path.abspath(path)
# 
#     @classmethod
#     def read_file(cls, filename):
#         '''Read the content of a file (UTF-8).
# 
#         Args:
# 
#             filename (str): Name of the file.
# 
#         Returns:
# 
#             list of str: Content of the file.
#         '''
#         if not os.path.isfile(filename):
#             return None
#         with codecs.open(filename, encoding='utf-8') as some_file:
#             lines = some_file.readlines()
#         return lines
# 
#     @classmethod
#     def get_first_line(cls, lines):
#         '''Get the line number of the first non-empty line in a list.
# 
#         Args:
# 
#             lines (list of str): A list of text.
# 
#         Returns:
# 
#             int: If non-empty line exists return the line number, otherwise 0.
#         '''
#         for num, line in enumerate(lines):
#             if line.strip():
#                 return num
#         return 0


# Path: DelogX/entity/item.py
import os
import re
from DelogX.utils.parser import Markdown
from DelogX.utils.path import Path

    def load_meta(self):
        '''Load the meta of this item, include `hidden`, `url` and `title`.'''
        if not self.valid():
            return
        self.hidden = self.filename.startswith('.')
        self.url = os.path.splitext(self.filename)[0]
        if self.hidden:
            self.url = self.url[1:]
        atx_re = re.compile(r'^\s*#\s*([^#]+)\s*#*\s*$')
        setext_re = re.compile(r'^\s*=+\s*$')
        lines = Path.read_file(self.path)
        first_line = Path.get_first_line(lines)
        line1 = lines[first_line] if first_line < len(lines) else ''
        line2 = lines[first_line + 1] if first_line + 1 < len(lines) else ''
        line1 = line1.strip('\n').strip('\r')
        line2 = line2.strip('\n').strip('\r')
        atx_match = atx_re.match(line1)
        setext_match = setext_re.match(line2)
        if atx_match and atx_match.group(1):
            self.title = atx_match.group(1)
            self.title_line = first_line + 1
        elif setext_match and line1.strip():
            self.title = line1
            self.title_line = first_line + 2
        else:
            self.title = self.url
            self.title_line = 0

    def load_content(self):
        '''Load and parse the content of this item.'''

        lines = Path.read_file(self.path)