From b0701e149bcb73f2a9768f970fe0f2ee5a85a27f Mon Sep 17 00:00:00 2001 From: Urs Liska Date: Tue, 23 Jun 2015 18:08:50 +0200 Subject: [PATCH 1/8] colorize: Add use_id_or_class option This adds control to how the
 tag and the line numbers
are exported. I have always found it inappropriate that
Frescobaldi has 
 hardcoded. python-ly allowed
to change the "document" part already, now also the id can be
changed for class.

I will add the interface to Frescobaldi too.
---
 ly/colorize.py | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/ly/colorize.py b/ly/colorize.py
index 96362966..6abcac74 100644
--- a/ly/colorize.py
+++ b/ly/colorize.py
@@ -517,7 +517,8 @@ class HtmlWriter(object):
     
     inline_style = False
     number_lines = False
-    
+
+    id_or_class = "id"
     document_id = "document"
     linenumbers_id = "linenumbers"
     
@@ -528,7 +529,15 @@ class HtmlWriter(object):
     
     stylesheet_ref = None
     full_html = True
-    
+
+    def use_id_or_class(self, selector):
+        """Export the 
 tag as class or id"""
+        if not selector in ["id", "class"]:
+            print "Slector has to be 'id' or 'class'."
+            print "Falling back to default 'id'."
+            self.id_or_class = 'id'
+        self.id_or_class = selector
+
     def html(self, cursor):
         """Return the output HTML."""
         doc_style = {}
@@ -543,8 +552,8 @@ def html(self, cursor):
         if self.linenumbers_bgcolor:
             num_style['background'] = self.linenumbers_bgcolor
         
-        num_attrs = {'id': self.linenumbers_id}
-        doc_attrs = {'id': self.document_id}
+        num_attrs = {self.id_or_class: self.linenumbers_id}
+        doc_attrs = {self.id_or_class: self.document_id}
         
         css = []
         if self.inline_style:

From 4be60e623de119158083c4e3a500edde66a02892 Mon Sep 17 00:00:00 2001
From: Urs Liska 
Date: Tue, 23 Jun 2015 18:09:29 +0200
Subject: [PATCH 2/8] colorize.py: Remove unneccessary whitespace

This was done by my editor automatically, so I commit it
as it seems useful.
---
 ly/colorize.py | 118 ++++++++++++++++++++++++-------------------------
 1 file changed, 58 insertions(+), 60 deletions(-)

diff --git a/ly/colorize.py b/ly/colorize.py
index 6abcac74..2b5a9a0f 100644
--- a/ly/colorize.py
+++ b/ly/colorize.py
@@ -25,7 +25,7 @@
 
 The Mapping object normally maps a token's class basically to a CSS class and
 possibly a base CSS class. This way you can define base styles (e.g. string,
-comment, etc) and have specific classes (e.g. LilyPond string, Scheme 
+comment, etc) and have specific classes (e.g. LilyPond string, Scheme
 comment) inherit from that base style. This CSS class is described by the
 css_class named tuple, with its three fields: mode, name, base. E.g.
 ('lilypond', 'articulation', 'keyword'). The base field may be None.
@@ -58,15 +58,15 @@
 
 class Mapper(dict):
     """Maps token classes to arbitrary values, which can be highlighting styles.
-    
+
     Mapper behaves like a dict, you set items with a token class as key to an
     arbitrary value.
-    
-    But getting items can be done using a token. The token class's method 
-    resolution order is walked up and the value for the first available 
-    class found in the keys is returned. The class is also cached to speed 
+
+    But getting items can be done using a token. The token class's method
+    resolution order is walked up and the value for the first available
+    class found in the keys is returned. The class is also cached to speed
     up requests for other tokens.
-    
+
     """
     def __getitem__(self, token):
         cls = type(token)
@@ -93,7 +93,7 @@ def default_mapping():
     from ly.lex import texinfo
     #from ly.lex import latex
     #from ly.lex import docbook
-    
+
     return (
         ('lilypond', (
             style('keyword', 'keyword', (lilypond.Keyword,)),
@@ -244,12 +244,12 @@ def default_mapping():
 
 def get_tokens(cursor):
     """Return the list of tokens for the cursor.
-    
+
     Tokens that are partially inside the cursor's selection are re-created
     so that they fall exactly within the selection.
-    
+
     This can be used to convert a highlighted part of a document to e.g. HTML.
-    
+
     """
     tokens = list(ly.document.Source(cursor, None, ly.document.PARTIAL, True))
     if tokens:
@@ -264,10 +264,10 @@ def get_tokens(cursor):
 
 def map_tokens(cursor, mapper):
     """Yield a two-tuple(token, style) for every token.
-    
+
     The style is what mapper[token] returns.
     Style may be None, which also happens with unparsed (not-tokenized) text.
-    
+
     """
     text = cursor.document.plaintext()
     start = cursor.start
@@ -300,9 +300,9 @@ def melt_mapped_tokens(mapped_tokens):
 
 def css_mapper(mapping=None):
     """Return a Mapper dict, mapping token classes to two CSS classes.
-    
+
     By default the mapping returned by default_mapping() is used.
-    
+
     """
     if mapping is None:
         mapping = default_mapping()
@@ -314,9 +314,9 @@ def css_mapper(mapping=None):
 
 def css_dict(css_style, scheme=default_scheme):
     """Return the css properties dict for the style, taken from the scheme.
-    
+
     This can be used for inline style attributes.
-    
+
     """
     d = {}
     try:
@@ -337,10 +337,10 @@ def css_item(i):
 
 def css_attr(d):
     """Return a dictionary with a 'style' key.
-    
-    The value is the style items in d formatted with css_item() joined with 
+
+    The value is the style items in d formatted with css_item() joined with
     spaces. If d is empty, an empty dictionary is returned.
-    
+
     """
     if d:
         return {'style': ' '.join(map(css_item, sorted(d.items())))}
@@ -365,7 +365,7 @@ class css_style_attribute_formatter(object):
     """Return the inline style attribute for a specified style."""
     def __init__(self, scheme=default_scheme):
         self.scheme = scheme
-    
+
     def __call__(self, css_style):
         d = css_dict(css_style, self.scheme)
         if d:
@@ -401,10 +401,10 @@ def html_escape_attr(text):
 
 def html_format_attrs(d):
     """Format the attributes dict as a string.
-    
-    The attributes are escaped correctly. A space is prepended for every 
+
+    The attributes are escaped correctly. A space is prepended for every
     assignment.
-    
+
     """
     return ''.join(' {0}="{1}"'.format(
             k, html_escape_attr(format(v))) for k, v in d.items())
@@ -412,12 +412,12 @@ def html_format_attrs(d):
 
 def html(cursor, mapper, span=format_css_span_class):
     """Return a HTML string with the tokens wrapped in  elements.
-    
-    The span argument is a function returning an attribute for the  
-    tag for the specified style. By default the format_css_span_class() 
-    function is used, that returns a 'class="group style base"' string. 
+
+    The span argument is a function returning an attribute for the 
+    tag for the specified style. By default the format_css_span_class()
+    function is used, that returns a 'class="group style base"' string.
     You'll want to wrap the HTML inside 
 tokens and add a CSS stylesheet.
-    
+
     """
     result = []
     for t, style in melt_mapped_tokens(map_tokens(cursor, mapper)):
@@ -433,14 +433,14 @@ def html(cursor, mapper, span=format_css_span_class):
 
 def add_line_numbers(cursor, html, linenum_attrs=None, document_attrs=None):
     """Combines the html (returned by html()) with the line numbers in a HTML table.
-    
-    The linenum_attrs are put in the  tag for the line numbers. The 
-    default value is: {"style": "background: #eeeeee;"}. The document_attrs 
+
+    The linenum_attrs are put in the  tag for the line numbers. The
+    default value is: {"style": "background: #eeeeee;"}. The document_attrs
     are put in the  tag for the document. The default is empty.
-    
-    By default, the id for the linenumbers  is set to "linenumbers", 
+
+    By default, the id for the linenumbers  is set to "linenumbers",
     and the id for the document  is set to "document".
-    
+
     """
     linenum_attrs = dict(linenum_attrs) if linenum_attrs else {"style": "background: #eeeeee;"}
     document_attrs = dict(document_attrs) if document_attrs else {}
@@ -451,7 +451,7 @@ def add_line_numbers(cursor, html, linenum_attrs=None, document_attrs=None):
     linenum_attrs['style'] = linenum_attrs.get('style', '') + 'vertical-align: top; text-align: right;'
     document_attrs['valign'] = 'top'
     document_attrs['style'] = document_attrs.get('style', '') + 'vertical-align: top;'
-    
+
     start_num = cursor.document.index(cursor.start_block()) + 1
     end_num = cursor.document.index(cursor.end_block()) + 1
     linenumbers = '
{0}
'.format('\n'.join(map(format, range(start_num, end_num)))) @@ -471,15 +471,15 @@ def add_line_numbers(cursor, html, linenum_attrs=None, document_attrs=None): def format_html_document(body, title="", stylesheet=None, stylesheet_ref=None, encoding='UTF-8'): """Return a complete HTML document. - - The body is put inside body tags unchanged. The title is html-escaped. - If stylesheet_ref is given, it is put as a reference in the HTML; - if stylesheet is given, it is put verbatim in a