Skip to content

Commit

Permalink
Nouveau plugin : inclusion de fichiers LaTeX
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis committed Jun 15, 2014
1 parent 099f218 commit e6afeb9
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions songbook_core/content/tex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""Include LaTeX raw code in the songbook."""

import logging
import os

from songbook_core.content import Content, ContentError

LOGGER = logging.getLogger(__name__)

class LaTeX(Content):
"""Inclusion of LaTeX code"""

def __init__(self, filename):
self.filename = filename

def render(self, context):
outdir = os.path.dirname(context['filename'])
if os.path.abspath(self.filename).startswith(os.path.abspath(outdir)):

This comment has been minimized.

Copy link
@Luthaf

Luthaf Jun 16, 2014

Contributor

Pourquoi compliquer le traitement et ne pas renvoyer simplement le chemin absolu a chaque fois ?

This comment has been minimized.

Copy link
@paternal

paternal Jun 16, 2014

Contributor

Je suis d'accord que ça n'est pas forcément nécessaire. Ça permet que, si tous les fichiers sont contenus dans le répertoire courant (ou un de ses sous-répertoires), alors le fichier LaTeX est moins dépendant du répertoire dans lequel il est situé.

Si j'ai un dossier Chansons qui contient mon fichier principal .tex, et d'autres fichiers inclus avec des chemins relatifs, je peux déplacer mon dossier Chansons dans son ensemble sans casser mon fichier .tex.

filename = os.path.relpath(self.filename, outdir)
else:
filename = os.path.abspath(self.filename)
return r'\input{{{}}}'.format(filename)

#pylint: disable=unused-argument
def parse(keyword, argument, contentlist, config):
"""Parse the contentlist.
Arguments:
- keyword: unused;
- argument: unused;
- contentlist: a list of name of tex files;
- config: configuration dictionary of the current songbook.
"""
if not contentlist:
LOGGER.warning(
"Useless 'tex' content: list of files to include is empty."
)
filelist = []
for filename in contentlist:
checked_file = None
for path in config['_songdir']:
if os.path.exists(os.path.join(path, filename)):
checked_file = os.path.relpath(os.path.join(path, filename))
break
if not checked_file:
raise ContentError(

This comment has been minimized.

Copy link
@Luthaf

Luthaf Jun 16, 2014

Contributor

Il pourrait être intéressante de ne lancer qu'un warning ici, le fichier peut être dans les chemins traditionnels LaTeX et pdflatex les trouvera. Et si les fichiers ne sont pas trouves par LaTeX, la compilation plantera de toute manière.

This comment has been minimized.

Copy link
@paternal

paternal Jun 16, 2014

Contributor

Fait : f727f13

keyword,
"Cannot find file '{}' in '{}'.".format(
filename,
str(config['_songdir']),
)
)
filelist.append(LaTeX(checked_file))

return filelist


CONTENT_PLUGINS = {'tex': parse}

0 comments on commit e6afeb9

Please sign in to comment.