Debian Codegraph @

docs/plugins/advancedrewrite.rst

raw
1 Advanced Rewrite Plugin
2 =======================
3
4 The ``advancedrewrite`` plugin lets you easily substitute values in your
5 templates and path formats, similarly to the :doc:`/plugins/rewrite`. It's
6 recommended to read the documentation of that plugin first.
7
8 The *advanced* rewrite plugin does not only support the simple rule format of
9 the ``rewrite`` plugin, but also an advanced format: there, the plugin doesn't
10 consider the value of the rewritten field, but instead checks if the given item
11 matches a :doc:`query </reference/query>`. Only then, the field is replaced with
12 the given value. It's also possible to replace multiple fields at once, and even
13 supports multi-valued fields.
14
15 To use advanced field rewriting, first enable the ``advancedrewrite`` plugin
16 (see :ref:`using-plugins`). Then, make a ``advancedrewrite:`` section in your
17 config file to contain your rewrite rules.
18
19 In contrast to the normal ``rewrite`` plugin, you need to provide a list of
20 replacement rule objects, which can have a different syntax depending on the
21 rule complexity.
22
23 The simple syntax is the same as the one of the rewrite plugin and allows to
24 replace a single field:
25
26 ::
27
28 advancedrewrite:
29 - artist ODD EYE CIRCLE: 이달의 소녀 오드아이써클
30
31 As with :doc:`/plugins/rewrite`, simple rules applied to a multi-valued field
32 rewrite only the matching list entries.
33
34 The advanced syntax consists of a query to match against, as well as a map of
35 replacements to apply. For example, to credit all songs of ODD EYE CIRCLE before
36 2023 to their original group name, you can use the following rule:
37
38 ::
39
40 advancedrewrite:
41 - match: "mb_artistid:dec0f331-cb08-4c8e-9c9f-aeb1f0f6d88c year:..2022"
42 replacements:
43 artist: 이달의 소녀 오드아이써클
44 artist_sort: LOONA / ODD EYE CIRCLE
45
46 Note how the sort name is also rewritten within the same rule. You can specify
47 as many fields as you'd like in the replacements map.
48
49 If you need to work with multi-valued fields, you can use the following syntax:
50
51 ::
52
53 advancedrewrite:
54 - match: "artist:배유빈 feat. 김미현"
55 replacements:
56 artists:
57 - 유빈
58 - 미미
59
60 As a convenience, the plugin applies patterns for the ``artist`` field to the
61 ``albumartist`` field as well. (Otherwise, you would probably want to duplicate
62 every rule for ``artist`` and ``albumartist``.)
63
64 Make sure to properly quote your query strings if they contain spaces, otherwise
65 they might not do what you expect, or even cause beets to crash.
66
67 Take the following example:
68
69 ::
70
71 advancedrewrite:
72 # BAD, DON'T DO THIS!
73 - match: album:THE ALBUM
74 replacements:
75 artist: New artist
76
77 On the first sight, this might look sane, and replace the artist of the album
78 *THE ALBUM* with *New artist*. However, due to the space and missing quotes,
79 this query will evaluate to ``album:THE`` and match ``ALBUM`` on any field,
80 including ``artist``. As ``artist`` is the field being replaced, this query will
81 result in infinite recursion and ultimately crash beets.
82
83 Instead, you should use the following rule:
84
85 ::
86
87 advancedrewrite:
88 # Note the quotes around the query string!
89 - match: album:"THE ALBUM"
90 replacements:
91 artist: New artist
92
93 A word of warning: This plugin theoretically only applies to templates and path
94 formats; it initially does not modify files' metadata tags or the values tracked
95 by beets' library database, but since it *rewrites all field lookups*, it
96 modifies the file's metadata anyway. See comments in issue :bug:`2786`.
97
98 As an alternative to this plugin the simpler but less powerful
99 :doc:`/plugins/rewrite` can be used. If you don't want to modify the item's
100 metadata and only replace values in file paths, you can check out the
101 :doc:`/plugins/substitute`.