From 21b1e37de1cbc90e7212d6fcbc537e4dd861126d Mon Sep 17 00:00:00 2001
From: Mats Bengtsson <mats.bengtsson@s3.kth.se>
Date: Wed, 4 May 2005 16:11:59 +0000
Subject: [PATCH] 	* scripts/convert-ly.py: Attempt to do a smarter
 update of 	text markups from versions < 1.9.0 with arbitrary nesting.

---
 ChangeLog             |  5 +++++
 scripts/convert-ly.py | 47 ++++++++++++++++++++++++++++++++++---------
 2 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index be33aba88e..050a2a872d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,8 @@
+2005-05-04  Mats Bengtsson  <mabe@drongo.s3.kth.se>
+
+	* scripts/convert-ly.py: Attempt to do a smarter update of 
+	text markups from versions < 1.9.0 with arbitrary nesting.
+
 2005-05-04  Heikki Junes  <hjunes@cc.hut.fi>
 
 	* po/fi.po: convert to utf-8, and update.
diff --git a/scripts/convert-ly.py b/scripts/convert-ly.py
index 06097eb07e..abc5e2d81d 100644
--- a/scripts/convert-ly.py
+++ b/scripts/convert-ly.py
@@ -1437,16 +1437,45 @@ if 1:
 		str = re.sub (r'@ACCENT@', '>', str)
 		return str
 
+	markup_start = re.compile(r"([-^_]|\\mark)\s*(#\s*'\s*)\(")
+	musicglyph = re.compile(r"\(\s*music\b")
+	submarkup_start = re.compile(r"\(\s*([a-zA-Z]+)")
+	leftpar = re.compile(r"\(")
+	rightpar = re.compile(r"\)")
+
 	def text_markup (str):
-		str = re.sub (r"""([-_^]) *# *' *\( *music *(\"[^"]*\") *\)""",
-				r"\1\\markup { \\musicglyph #\2 }", str)
-		str = re.sub (r"""([-_^]) *# *' *\( *([a-z]+) *([^()]*)\)""",
-				r"\1\\markup { \\\2 \3 }", str)
-		str = re.sub (r"""\\mark *# *' *\( *music *(\"[^"]*\") *\)""",
-				r"\\mark \\markup { \\musicglyph #\1 }", str)
-		str = re.sub (r"""\\mark *# *' *\( *([a-z]+) *([^()]*)\)""",
-				r"\\mark \\markup { \\\1 \2 }", str)
-		return str
+		result = ''
+		# Find the beginning of each markup:
+		match = markup_start.search (str)
+		while match:
+			result = result + str[:match.end (1)] + " \markup"
+			str = str[match.end( 2):]
+			# Count matching parentheses to find the end of the 
+			# current markup:
+			nesting_level = 0
+			pars = re.finditer(r"[()]",str)
+			for par in pars:
+				if par.group () == '(':
+					nesting_level = nesting_level + 1
+				else:
+					nesting_level = nesting_level - 1
+				if nesting_level == 0:
+					markup_end = par.end ()
+					break
+			# The full markup in old syntax:
+			markup = str[:markup_end]
+			# Modify to new syntax:
+			markup = musicglyph.sub (r"{\\musicglyph", markup)
+			markup = submarkup_start.sub (r"{\\\1", markup)
+			markup = leftpar.sub ("{", markup)
+			markup = rightpar.sub ("}", markup)
+	
+			result = result + markup
+			# Find next markup
+			str = str[markup_end:]
+			match = markup_start.search(str)
+		result = result + str
+		return result
 
 	def articulation_substitute (str):
 		str = re.sub (r"""([^-])\[ *([a-z]+[,']*[!?]?[0-9:]*\.*)""",
-- 
2.39.5