# -*- coding: utf-8 -*-
from logya.content import read_all
from logya.template import init_env
from logya.util import load_yaml, paths


class Logya:
    """Object to store data such as site index and settings."""

    def __init__(self, options):
        """Set required logya object properties."""

        self.verbose = getattr(options, 'verbose', False)
        self.paths = paths(dir_site=getattr(options, 'dir_site', None))
        self.settings = load_yaml(self.paths.root.joinpath('site.yaml').read_text())

        # Initialize index so scripts can generate indexed content before build.
        self.index = {}

    def build(self):
        """Build index of documents and collections."""

        # Previously indexed content can exist. If a document exists in the read directory with the same URL as an
        # already indexed content object, the existing object will be overridden.
        self.index.update(read_all(self.paths, self.settings))

        # Initialize template env.
        init_env(self.settings, self.index)

    def info(self, msg):
        """Print message if in verbose mode."""

        if self.verbose:
            print(msg)

@@ -17,16 +17,62 @@
 f-8 -*-%0A
+from os import walk%0Afrom pathlib import Path%0A%0A
 from log
@@ -93,20 +93,16 @@
 ort read
-_all
 %0Afrom lo
@@ -169,16 +169,45 @@
 l, paths
+, process_extensions, slugify
 %0A%0A%0Aclass
@@ -608,16 +608,32 @@
 e index 
+and collections 
 so scrip
@@ -706,70 +706,103 @@
  %7B%7D%0A
-%0A
+  
     
-def build(self):%0A        %22%22%22Build index of documents and 
+  self.collections = self.settings.get('collections', %7B%7D)%0A        for name, coll in self.
 coll
@@ -809,20 +809,24 @@
 ections.
-%22%22%22%0A
+items():
 %0A       
@@ -830,258 +830,144 @@
     
-# Previously indexed content can exist. If a document exists in the read directory with the same URL as an%0A        # already indexed content object, the existing object will be overridden.%0A        self.index.update(read_all(self.paths, self.settings)
+    coll%5B'index'%5D = %7B%7D%0A%0A    def build(self):%0A        %22%22%22Read content and initialize template environment.%22%22%22%0A%0A        self.read_content(
 )%0A%0A 
@@ -1067,16 +1067,21 @@
 elf, msg
+: str
 ):%0A     
@@ -1172,8 +1172,1860 @@
 nt(msg)%0A
+%0A    def read_content(self):%0A        %22%22%22Read content and update index and collections.%0A%0A        Previously indexed content can exist. If a file inside the content directory has the same URL as already indexed%0A        content, the existing content will be replaced.%0A        %22%22%22%0A%0A        for root, _, files in walk(self.paths.content):%0A            for f in files:%0A                path = Path(root, f)%0A                if path not in process_extensions:%0A                    continue%0A                doc = read(path, path.relative_to(self.paths.content))%0A                if doc:%0A                    if self.collections:%0A                        self.update_collections(doc)%0A                    self.index%5Bdoc%5B'url'%5D%5D = %7B'doc': doc, 'path': path%7D%0A%0A    def update_collections(self, doc: dict):%0A        %22%22%22Update collections index for given doc.%22%22%22%0A%0A        for attr, values in doc.items():%0A            if attr not in self.collections:%0A                continue%0A            coll = self.collections%5Battr%5D%0A%0A            # Process unique values.%0A            for value in set(values):%0A                url = f'/%7Bcoll%5B%22path%22%5D%7D/%7Bslugify(value).lower()%7D/'%0A%0A                if url in self.index:%0A                    print(f'Collection not created because content exists at %7Burl%7D.')%0A                    continue%0A%0A                # Add attribute for creating collection links in templates.%0A                links = attr + '_links'%0A                doc%5Blinks%5D = doc.get(links, %5B%5D) + %5B(url, value)%5D%0A%0A                # Update or create collection index value.%0A                if url in coll%5B'index'%5D:%0A                    coll%5B'index'%5D%5Burl%5D%5B'docs'%5D.append(doc)%0A                else:%0A                    coll%5B'index'%5D%5Burl%5D = %7B%0A                        'docs': %5Bdoc%5D,%0A                        'title': value,%0A                        'template': coll%5B'template'%5D%0A                    %7D%0A

Add read_content and update_collections methods to Logya class.