Browse Source

Merge branch 'master' into content_cd

pull/207/head
Oliverpool 9 years ago
parent
commit
af8bd36073
  1. 2
      patacrep/authors.py
  2. 48
      patacrep/content/setcounter.py
  3. 17
      patacrep/data/latex/patacrep.sty
  4. 2
      patacrep/templates.py
  5. 4
      test/test_content/setcounter.control
  6. 7
      test/test_content/setcounter.source
  7. 17
      test/test_content/test_content.py
  8. 3
      test/test_songbook/content.tex.control
  9. 1
      test/test_songbook/content.yaml

2
patacrep/authors.py

@ -17,7 +17,7 @@ def compile_authwords(authwords):
'ignore': authwords.get('ignore', []), 'ignore': authwords.get('ignore', []),
'after': [ 'after': [
re.compile(RE_AFTER.format(word), re.LOCALE) re.compile(RE_AFTER.format(word), re.LOCALE)
for word in authwords['after'] for word in authwords.get('after')
], ],
'separators': [ 'separators': [
re.compile(RE_SEPARATOR.format(word), re.LOCALE) re.compile(RE_SEPARATOR.format(word), re.LOCALE)

48
patacrep/content/setcounter.py

@ -0,0 +1,48 @@
"""Allows to set an arbitrary value to any LaTeX counter (like `songnum`)."""
from patacrep.content import ContentItem, ContentList, validate_parser_argument
class CounterSetter(ContentItem):
"""Set a counter."""
# pylint: disable=too-few-public-methods
def __init__(self, name, value):
self.name = name
self.value = value
def render(self, __context):
"""Set the value of the counter."""
return r'\setcounter{{{}}}{{{}}}'.format(self.name, self.value)
#pylint: disable=unused-argument
@validate_parser_argument("""
type: //any
of:
- //nil
- //int
- type: //rec
optional:
name: //str
value: //int
""")
def parse(keyword, argument, config):
"""Parse the counter setter.
Arguments:
- nothing
reset the "songnum" counter to 1
- an int
reset the "songnum" counter to this value
- a dict:
- name ("songnum"): the counter to set;
- value: value to set the counter to;
"""
if argument is None:
argument = {}
if isinstance(argument, int):
argument = {'value': argument}
name = argument.get('name', 'songnum')
value = argument.get('value', 1)
return ContentList([CounterSetter(name, value)])
CONTENT_PLUGINS = {'setcounter': parse}

17
patacrep/data/latex/patacrep.sty

@ -443,4 +443,21 @@
\def\@void[#1]{} \def\@void[#1]{}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Prevent numbering reset on new 'songs' environment
\newcounter{tempsongnum}
\let\oldsongs\songs
\let\endoldsongs\endsongs
\renewenvironment{songs}[1]{%
\setcounter{tempsongnum}{\thesongnum}%
\oldsongs{#1}%
\setcounter{songnum}{\thetempsongnum}%
}{%
\endoldsongs\ignorespacesafterend%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\endinput \endinput

2
patacrep/templates.py

@ -275,7 +275,7 @@ def iter_bookoptions(config):
if config['chords']['show']: if config['chords']['show']:
yield 'chorded' yield 'chorded'
else: else:
yield 'lyrics' yield 'lyric'
book_equivalents = { book_equivalents = {
'pictures': 'pictures', 'pictures': 'pictures',

4
test/test_content/setcounter.control

@ -0,0 +1,4 @@
- setcounter{songnum}{101}
- setcounter{songnum}{1}
- setcounter{songnum}{5}
- setcounter{counter_name}{-1}

7
test/test_content/setcounter.source

@ -0,0 +1,7 @@
- setcounter:
value: 101
- setcounter:
- setcounter: 5
- setcounter:
name: counter_name
value: -1

17
test/test_content/test_content.py

@ -10,7 +10,7 @@ import yaml
from pkg_resources import resource_filename from pkg_resources import resource_filename
from patacrep import content, files from patacrep import content, files
from patacrep.content import song, section, songsection, tex from patacrep.content import song, section, setcounter, songsection, tex
from patacrep.songbook import prepare_songbook from patacrep.songbook import prepare_songbook
from .. import logging_reduced from .. import logging_reduced
@ -76,15 +76,18 @@ class FileTest(unittest.TestCase, metaclass=dynamic.DynamicTest):
@classmethod @classmethod
def _clean_path(cls, elem): def _clean_path(cls, elem):
"""Shorten the path relative to the `songs` directory""" """Shorten the path relative to the `songs` directory"""
if isinstance(elem, song.SongRenderer):
songpath = os.path.join(os.path.dirname(__file__), 'datadir', 'songs')
return files.path2posix(files.relpath(elem.song.fullpath, songpath))
elif isinstance(elem, section.Section): latex_command_classes = (
section.Section,
songsection.SongSection,
setcounter.CounterSetter,
)
if isinstance(elem, latex_command_classes):
return elem.render(None)[1:] return elem.render(None)[1:]
elif isinstance(elem, songsection.SongSection): elif isinstance(elem, song.SongRenderer):
return elem.render(None)[1:] songpath = os.path.join(os.path.dirname(__file__), 'datadir', 'songs')
return files.path2posix(files.relpath(elem.song.fullpath, songpath))
elif isinstance(elem, tex.LaTeX): elif isinstance(elem, tex.LaTeX):
return files.path2posix(elem.filename) return files.path2posix(elem.filename)

3
test/test_songbook/content.tex.control

@ -122,6 +122,9 @@ guitar,
\songsection{Test of song section} \songsection{Test of song section}
\setcounter{songnum}{101}
\begin{songs}{titleidx,authidx} \begin{songs}{titleidx,authidx}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%% content_datadir/content/song.csg %% content_datadir/content/song.csg

1
test/test_songbook/content.yaml

@ -11,6 +11,7 @@ content:
- section: Test of section - section: Test of section
- sort: - sort:
- songsection: Test of song section - songsection: Test of song section
- setcounter: 101
- addsongdir: - addsongdir:
# relative to yaml songfile # relative to yaml songfile
path: content_datadir/content path: content_datadir/content

Loading…
Cancel
Save