Browse Source

Meta data plugin added

main
Lakshmi Vyasarajan 14 years ago
parent
commit
6ef606cf5b
4 changed files with 114 additions and 0 deletions
  1. +0
    -0
      hyde/ext/plugins/__init__.py
  2. +58
    -0
      hyde/ext/plugins/meta.py
  3. +0
    -0
      hyde/tests/ext/__init__.py
  4. +56
    -0
      hyde/tests/ext/test_meta.py

+ 0
- 0
hyde/ext/plugins/__init__.py View File


+ 58
- 0
hyde/ext/plugins/meta.py View File

@@ -0,0 +1,58 @@
"""
Contains classes and utilities related to meta data in hyde.
"""
import re
from hyde.model import Expando
from hyde.plugin import Plugin
import yaml

import logging
from logging import NullHandler
logger = logging.getLogger('hyde.engine')
logger.addHandler(NullHandler())


class Metadata(Expando):
"""
Container class for yaml meta data.
"""
def __init__(self, text):
super(Metadata, self).__init__(yaml.load(text))



class MetaPlugin(Plugin):
"""
Metadata plugin for hyde. Loads meta data in the following order:

1. meta.yaml: files in any folder
2. frontmatter: any text file with content enclosed within three dashes.
Example:
---
abc: def
---

Supports YAML syntax.
"""

def __init__(self, site):
super(MetaPlugin, self).__init__(site)


def begin_text_resource(self, resource, text):
"""
Load meta data by looking for the marker.
Once loaded, remove the meta area from the text.
"""
logger.info("Trying to load metadata from resource [%s]" % resource)
# re from spjwebster's lanyon
yaml_finder = re.compile( r"^\s*---\s*\n((?:.|\n)+?)\n---\s*\n", re.MULTILINE)
match = re.match(yaml_finder, text)
if not match:
logger.info("No metadata found in resource [%s]" % resource)
return text
text = text[match.end():]
resource.meta = Metadata(match.group(1))
logger.info("Successfully loaded metadata from resource [%s]"
% resource)
return text

+ 0
- 0
hyde/tests/ext/__init__.py View File


+ 56
- 0
hyde/tests/ext/test_meta.py View File

@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""
Use nose
`$ pip install nose`
`$ nosetests`
"""
from hyde.ext.plugins.meta import MetaPlugin
from hyde.fs import File, Folder
from hyde.generator import Generator
from hyde.site import Site

import yaml


TEST_SITE = File(__file__).parent.parent.child_folder('_test')


class TestMeta(object):

def setUp(self):
TEST_SITE.make()
TEST_SITE.parent.child_folder('sites/test_jinja').copy_contents_to(TEST_SITE)

def tearDown(self):
TEST_SITE.delete()


def test_can_load_front_matter(self):
front_matter = """
---
title: A nice title
author: Lakshmi Vyas
twitter: lakshmivyas
---
"""
about1 = File(TEST_SITE.child('content/about.html'))
about2 = File(TEST_SITE.child('content/about2.html'))

text = front_matter
text += "\n"
text += about1.read_all()

about2.write(text)
s = Site(TEST_SITE)
s.config.plugins = ['hyde.ext.plugins.meta.MetaPlugin']
gen = Generator(s)
gen.generate_all()
res = s.content.resource_from_path(about2.path)

assert hasattr(res, 'meta')
assert hasattr(res.meta, 'title')
assert hasattr(res.meta, 'author')
assert hasattr(res.meta, 'twitter')
assert res.meta.title == "A nice title"
assert res.meta.author == "Lakshmi Vyas"
assert res.meta.twitter == "lakshmivyas"

Loading…
Cancel
Save