From 63aa3b316514062a53dec185f06c1c4201eb16cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoine=20Beaupr=C3=A9?= Date: Sat, 15 Jul 2017 00:50:52 -0400 Subject: [PATCH] write to separate rst files this makes the ePUB easier to parse by e-readers, because they do not need to load one giant HTML file, but one per author. it also makes sphinx rendering more efficient and interactive --- beetsplug/lyrics.py | 58 +++++++++++++++++++++++++++++------------ docs/plugins/lyrics.rst | 5 ++-- 2 files changed, 44 insertions(+), 19 deletions(-) diff --git a/beetsplug/lyrics.py b/beetsplug/lyrics.py index 3c0afab46..42ddef025 100644 --- a/beetsplug/lyrics.py +++ b/beetsplug/lyrics.py @@ -22,12 +22,15 @@ import difflib import itertools import json import struct +import os.path import re import requests import unicodedata +from unidecode import unidecode import warnings import six from six.moves import urllib +import sys try: from bs4 import SoupStrainer, BeautifulSoup @@ -660,9 +663,9 @@ class LyricsPlugin(plugins.BeetsPlugin): help=u'print lyrics to console', ) cmd.parser.add_option( - u'-r', u'--print-rst', dest='printrst', - action='store_true', default=False, - help=u'print lyrics to console as RST text', + u'-r', u'--write-rst', dest='writerst', + action='store', default='.', + help=u'write lyrics to given directory as RST files', ) cmd.parser.add_option( u'-f', u'--force', dest='force_refetch', @@ -679,8 +682,9 @@ class LyricsPlugin(plugins.BeetsPlugin): # The "write to files" option corresponds to the # import_write config value. write = ui.should_write() - artist = '' - album = '' + artist = u'Unknown artist' + album = False + output = sys.stdout for item in lib.items(ui.decargs(args)): if not opts.skip_fetched and not self.config['skip']: self.fetch_item_lyrics( @@ -690,24 +694,44 @@ class LyricsPlugin(plugins.BeetsPlugin): if item.lyrics: if opts.printlyr: ui.print_(item.lyrics) - if opts.printrst: + if opts.writerst: if artist != item.artist: artist = item.artist - ui.print_(artist) - ui.print_(u'=' * len(artist)) - ui.print_() + if output != sys.stdout: + output.close() + slug = re.sub(r'\W+', '-', + unidecode(artist).lower()) + path = os.path.join(opts.writerst, slug + u'.rst') + output = open(path, 'w') + output.write(artist.encode('utf-8')) + output.write(u'\n') + output.write(u'=' * len(artist)) + output.write(u'\n') + output.write(u''' +.. contents:: + :local: +''') + output.write(u'\n') if album != item.album: album = item.album - ui.print_(album) - ui.print_(u'-' * len(album)) - ui.print_() + output.write(album.encode('utf-8')) + output.write(u'\n') + output.write(u'-' * len(album)) + output.write(u'\n') + output.write(u'\n') title_str = u':index:`' + item.title + u'`' - ui.print_(title_str) - ui.print_(u'~' * len(title_str)) - ui.print_() + output.write(title_str.encode('utf-8')) + output.write(u'\n') + output.write(u'~' * len(title_str)) + output.write(u'\n') + output.write(u'\n') # turn lyrics into a line block - ui.print_(u'| ' + item.lyrics.replace(u'\n', u'\n| ')) - ui.print_() + block = u'| ' + item.lyrics.replace(u'\n', u'\n| ') + output.write(block.encode('utf-8')) + output.write(u'\n') + output.write(u'\n') + if opts.writerst: + output.close() cmd.func = func return [cmd] diff --git a/docs/plugins/lyrics.rst b/docs/plugins/lyrics.rst index 454f16d57..2fe7422f9 100644 --- a/docs/plugins/lyrics.rst +++ b/docs/plugins/lyrics.rst @@ -85,8 +85,9 @@ embedded into files' metadata. The ``-p`` option to the ``lyrics`` command makes it print lyrics out to the console so you can view the fetched (or previously-stored) lyrics. The -``-r`` option similarly shows all lyrics as an RST (ReStructuredText) -document. That document, in turn, can be parsed by tools like Sphinx +``-r directory`` option similarly shows all lyrics as an RST (ReStructuredText) +document structure located in ``directory`` (which defaults to the +current directory). That document, in turn, can be parsed by tools like Sphinx to generate HTML, ePUB or PDF formatted documents. Use, for example, the following ``conf.py``::