Skip to content

Commit

Permalink
Merge pull request getpelican/pelican-plugins#1347 from native2k/master
Browse files Browse the repository at this point in the history
i18n_subsites: Generate symbolic links for specified directories in language
subdirectories
  • Loading branch information
rschiang committed Aug 9, 2022
2 parents 5c3ff28 + d418e31 commit d6add21
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
25 changes: 25 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ dictionary must be given (but can be empty) in the ``I18N_SUBSITES`` dictionary
You must also have the following in your pelican configuration

.. code-block:: python
JINJA_ENVIRONMENT = {
'extensions': ['jinja2.ext.i18n'],
}
Expand Down Expand Up @@ -145,8 +146,32 @@ to link to the main site.
This short `howto <./implementing_language_buttons.rst>`_ shows two
example implementations of language buttons.

Additional config option
........................

If you use plugins like ``photos``, ``thumbnailer`` and want to prevent
the system from copying the files into each language directory, it is possible
to set a list of directories in the variable ``I18N_LINK_DIRS``.
For each path a symbolic link is created which links to the original directory.

.. code-block:: python
I18N_LINK_DIRS = ['images/thumbnails', 'photos']
.. code-block::
└── output/ # base output directory
├── images/
│ └── thumbnails/ # original directory
├── photos/ # original directory
└─── de/ # language subfolder
├── photos -> /output/photos # symbolic link to original directory
└── images/
└── thumbnails -> /output/images/thumbnails # symbolic link to original directory
Usage notes
===========

- It is **mandatory** to specify ``lang`` metadata for each article
and page as ``DEFAULT_LANG`` is later changed for each sub-site, so
content without ``lang`` metadata would be rendered in every
Expand Down
32 changes: 32 additions & 0 deletions i18n_subsites.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,36 @@ def get_pelican_cls(settings):
return cls


def create_dirs(settings):
dirs = settings.get('I18N_LINK_DIRS') or []
if not dirs:
return

toutput = settings['OUTPUT_PATH']
soutput = os.path.split(toutput)[0]

_LOGGER.debug("create dirs {} in '{}' ".format(dirs, toutput))

if not os.path.exists(toutput):
os.makedirs(toutput)

for dir in dirs:
src=os.path.join(soutput, dir)
if not os.path.exists(src):
os.makedirs(src)

destpath = os.path.split(dir)[0]
if destpath:
destpathloc = os.path.join(toutput, destpath)
if not os.path.exists(destpathloc):
os.makedirs(destpathloc)

dest=os.path.join(toutput, dir)
if not os.path.exists(dest):
_LOGGER.debug(" create link '{}' -> '{}'".format(dest, src))
os.symlink(src, dest)


def create_next_subsite(pelican_obj):
'''Create the next subsite using the lang-specific config
Expand All @@ -434,6 +464,8 @@ def create_next_subsite(pelican_obj):
new_pelican_obj = cls(settings)
_LOGGER.debug(("Generating i18n subsite for language '{}' "
"using class {}").format(lang, cls))

create_dirs(settings)
new_pelican_obj.run()


Expand Down

0 comments on commit d6add21

Please sign in to comment.