Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic support for LaTeX builder #12

Merged
merged 2 commits into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,13 @@ Sphinx Extension for LilyPond

The extension is originated from `sphinx-contrib/lilypond`_ , allows `LilyPond`_
music notes to be included in Sphinx-generated documents inline and outline.
Compared to its predecessor, the extension has many new features such as
scale transposing, audio output, layout controlling, and so on.
Compared to its predecessor, the extension has many new features such as:

- `Scale transposing <Transposing>`_
- `Audio preview <Audio preview>`_
- Layout controlling
- LaTeX support (Since `2022-03-XX 1.5`_)
- And so on…

.. _sphinx-contrib/lilypond: https://github.com/sphinx-contrib/lilypond
.. _LilyPond: https://lilypond.org/
Expand Down Expand Up @@ -331,6 +336,11 @@ Control Bar at the Top
Chang Log
=========

2022-03-XX 1.5
--------------

- Add LaTeX builder suppport

2021-12-19 1.4
--------------

Expand Down
66 changes: 62 additions & 4 deletions sphinxnotes/lilypond/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@ def move_to_builddir(builder, node:lily_base_node, out:lilypond.Output):
return out


# TODO: two type
def html_visit_lily_node(self, node:lily_base_node):
def get_lilypond_output(self, node:lily_base_node) -> lilypond.Output:
out = pick_from_builddir(self.builder, node)

cached = not (out is None)
Expand Down Expand Up @@ -218,6 +217,10 @@ def html_visit_lily_node(self, node:lily_base_node):
else:
# Get relative path
move_to_builddir(self.builder, node, out)
return out

def html_visit_lily_node(self, node:lily_base_node):
out = get_lilypond_output(self, node)

# Create div for block level element
if isinstance(node, lily_outline_node):
Expand Down Expand Up @@ -262,6 +265,57 @@ def html_visit_lily_node(self, node:lily_base_node):

raise nodes.SkipNode

def latex_visit_lily_node(self, node:lily_base_node):
"""
See sphinx/sphinx/writers/latex.py::visit_image().
"""

out = get_lilypond_output(self, node)

CR = '\n'
pre: List[str] = [] # in reverse order
post: List[str] = []


options = ''

if node.get('preview') and out.preview:
base, ext = path.splitext(out.preview)
self.body.append(CR)
self.body.append(r'\sphinxincludegraphics%s{{%s}%s}' %
(options, base, ext))
self.body.append(CR)
elif out.score:
base, ext = path.splitext(out.score)
self.body.append(CR + r'\noindent')
self.body.append(r'\sphinxincludegraphics%s{{%s}%s}' %
(options, base, ext))
self.body.append(CR)
elif out.paged_scores:
for p in out.paged_scores:
self.body.append(CR + r'\noindent')
base, ext = path.splitext(p)
self.body.append(r'\sphinxincludegraphics%s{{%s}%s}' %
(options, base, ext))
self.body.append(CR)
else:
msg = 'no score generated from lilypond document'
logger.warning(msg, location=node)
sm = nodes.system_message(msg, type='WARNING', level=2, backrefs=[],
source=node['lilysrc'])
sm.walkabout(self)
raise nodes.SkipNode

if node.get('audio'):
msg = 'audio option is supported for latex builder'
logger.warning(msg, location=node)
sm = nodes.system_message(msg, type='WARNING', level=2, backrefs=[],
source=node['lilysrc'])
sm.walkabout(self)

raise nodes.SkipNode


def _config_inited(app, config:Config) -> None:
lilypond.Config.lilypond_args = config.lilypond_lilypond_args
lilypond.Config.timidity_args = config.lilypond_timidity_args
Expand All @@ -276,8 +330,12 @@ def _config_inited(app, config:Config) -> None:


def setup(app):
app.add_node(lily_inline_node, html=(html_visit_lily_node, None))
app.add_node(lily_outline_node, html=(html_visit_lily_node, None))
app.add_node(lily_inline_node,
html=(html_visit_lily_node, None),
latex=(latex_visit_lily_node, None))
app.add_node(lily_outline_node,
html=(html_visit_lily_node, None),
latex=(latex_visit_lily_node, None))
app.add_role('lily', lily_role)
app.add_directive('lily', LilyDirective)
app.add_directive('lilyinclude', LilyIncludeDirective)
Expand Down