#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from Xac.defaults import *
from Xac.plugins.news_feed import gen_news_feed
import Xac
import os
import sys






# Your name.
NAME = 'Foo Bar'

# The site URL.
SRV_URL = 'http://example.com'

# The directory in which to store automatically generated content. It is a good
# idea to keep this in a separate directory that it can be easily cleaned up.
#
# This should always map to the root of the server.
AUTOGEN_DIR = 'files/autogen'

# The server path of dynamically generated plugin content.
DYN_PATH = 'etc/dyn'

# The local path to the dynamic plugin content, usually a subpath of AUTOGEN_DIR.
DYN_REAL_PATH = os.path.join(AUTOGEN_DIR, DYN_PATH)

# The server path of the dynamic plugin content.
DYN_SRV_PATH = os.path.join('/', DYN_PATH)

# The temporary working directory for plugins that generate intermediate content.
DYN_TMP_DIR = 'tmp'

# The prepublication directory in which to stage files for publication.
PREPUB_DIR = os.getenv('XAC_PREPUB_DIR', default='prepub')

# The publication directory that will be uploaded to the server.
PUB_DIR = os.getenv('XAC_PUB_DIR', default='pub')

# The templates directory in which to look for the default templates.
TEMPLATES_DIR = os.getenv('XAC_TEMPLATES_DIR', default="files/templates")

# The templates CSS directory in which to look for the template CSS files.
TEMPLATE_CSS_DIR = os.getenv('XAC_TEMPLATE_CSS_DIR', default="files/template_css")

# The theme color of the site.
THEME_COLOR = '#76249C'






# The list of file directories to publish. The first component of the tuple is
# the local path. The second component is the relative path from the server
# root. None indicates that the files will be published in the server root.
#
# The hierarchy of each directory is recreated at the relative server path.
# For example, given the local files
#
#     foo/bar/a.txt
#     foo/bar/b.txt
#
# ('foo', 'baz') would create the following files on the server:
#
#     baz/bar/a.txt
#     baz/bar/b.txt
#
# Files with the '.markdown' extension will be passed to Pandoc along with
# template files returned by the get_pandoc_template function (see the Xac class
# documentation).
#
# Non-markdown files can be interpolated using a custom get_var_delimiters
# function. This function accepts a path and returns either None or a tuple
# of variable delimiters. If it returns None for a given path, that file is
# just symlinked into the server. If it returns a tuple of delimiters, then
# the file is parsed and all configuration variables in config_vars will be
# interpolated if they are flanked by the delimiters.
#
# This will hopefully be made clear with an example. The default
# get_var_delimiters function returns ('@@@','@@@') for all files with a ".css"
# extension. When Xac publishes a CSS file, it therefore checks the file for
# "@@@foo@@@", where foo is any variable in the config_vars dictionary. All
# instances of "@@@foo@@@" are replaced by the value for foo in the dictionary.
# This enables CSS files to be easily customized using variables, but it could
# be used for any file.


# See how the news directories are used below.
NEWS_DIRS = ('files/news', 'news')
TEMPLATE_CSS_DIRS = (TEMPLATE_CSS_DIR, 'etc/css')
FILE_DIRS = (
  ('files/main', None),
  ('files/forum', 'forum'),
  (AUTOGEN_DIR, None),
  TEMPLATE_CSS_DIRS,
)
# This is an *iterable* of patterns that will be matched against server paths.
# Anything that matches is passed through without further action. Use this to
# include a forum or .markdown files on the server. Ignored paths will still
# be included in the nav bar, but not their contents.

# Note the trailing slash for directories. Also be careful not to remove the
# trailing comma for 1-tuples, as they will cease to be tuples. The pattern
# would then be matched against the separate letters of the string.
IGNORED = (
  '/forum/',
)


# This is just an example of some of the things that can be done by creating
# custom functions for the Xac object. This will change the theme color and
# banner for different areas of the site to make them more distinct. See the
# comments in Xac.defaults for more details.

areas = (
  {
    'path' : '/',
    'name' : 'Main',
    'color' : THEME_COLOR,
    'banner' : '/etc/img/banner.png'
  },
  {
    'path' : '/news/',
    'name' : 'News',
    'color' : THEME_COLOR,
    'banner' : '/etc/img/banner.png'
  },
  {
    'path' : '/foo/',
    'name' : 'Foo',
    'color' : '#1793D1',
    'banner' : '/etc/img/banner.png'
  },
)



# The footer is inserted directly into the HTML template.
# Here is a simple example.
footer_fields = (
  ('Contact', 'foo@example.com'),
)

footer = r'<dl>'
for t, d in footer_fields:
  footer += r'<dt>%s</dt><dd>%s</dd>' % (t,d)
footer += r'</dl>'





# Create the default Xac object.
xac = Xac.Xac(
  file_dirs            = FILE_DIRS,
  get_pandoc_template  = get_default_get_pandoc_template(TEMPLATES_DIR),
  get_pandoc_args      = get_default_get_pandoc_args(areas, AUTOGEN_DIR, TEMPLATE_CSS_DIRS, footer),
  config_vars          = get_default_config_vars(THEME_COLOR),
  get_var_delimiters   = get_default_var_delimiters,
  post_pandoc_handlers = get_default_post_pandoc_handlers(DYN_REAL_PATH, DYN_SRV_PATH, DYN_TMP_DIR),
  final_pandoc_handler = default_final_pandoc_handler,
  prepub_dir           = PREPUB_DIR,
  pub_dir              = PUB_DIR,
  ignored              = IGNORED
)






# Generate the news index and atom feed, and sort old new articles into
# subdirectories of the news directory.
def manage_news():
  gen_news_feed(
    NEWS_DIRS[0], #src
    os.path.join(AUTOGEN_DIR, NEWS_DIRS[1]), #dest
    NEWS_DIRS[1], #srv
    SRV_URL, #url
    author=NAME,
    title=NEWS_DIRS[1].title(),
    last_update=xac.watchlist_mtime,
    xac=xac
  )










def main():
  manage_news()
  xac.publish_html()
  #insert_maps()

if __name__ == '__main__':
  if sys.argv[1:]:
    for path in sys.argv[1:]:
      os.chdir(path)
      main()
  else:
    main()

