Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Changes to section attributes.

- Removed hijacking
  - Section ids are section_ prefixed header ids
  - headerids are not removed
- Classes in headers are now used in sections as well
- sections now have hasn classes where n is the number of child sections(immediate)
  • Loading branch information...
commit f0e7c071d24534ab59f18820e8bfff77e12d0848 1 parent 10a65a3
Lakshmi Vyas authored March 21, 2012

Showing 1 changed file with 44 additions and 23 deletions. Show diff stats Hide diff stats

  1. 67  markdown/extensions/sections.py
67  markdown/extensions/sections.py
@@ -9,18 +9,27 @@
9 9
 
10 10
     >>> import markdown
11 11
     >>> text = "# Some Header #"
12  
-    >>> md = markdown.markdown(text, ['headerid', 'sections'])
  12
+    >>> md = markdown.markdown(text, ['attr_list', 'headerid', 'sections'])
13 13
     >>> print md
14  
-    <section class="level1" id="some-header"><h1>Some Header</h1>
  14
+    <section class="level1" id="section_some-header"><h1 id="some-header">Some Header</h1>
15 15
     </section>
16 16
 
17 17
 Single section with id attribute:
18 18
 
19 19
     >>> import markdown
20 20
     >>> text = "# Some Header{@id=the_header}"
21  
-    >>> md = markdown.markdown(text, ['headerid', 'sections'])
  21
+    >>> md = markdown.markdown(text, ['attr_list', 'headerid', 'sections'])
22 22
     >>> print md
23  
-    <section class="level1" id="the_header"><h1>Some Header</h1>
  23
+    <section class="level1" id="section_the_header"><h1 id="the_header">Some Header</h1>
  24
+    </section>
  25
+
  26
+Single section with class attribute:
  27
+
  28
+    >>> import markdown
  29
+    >>> text = "# Some Header{: .title #the_header}"
  30
+    >>> md = markdown.markdown(text, ['attr_list', 'headerid', 'sections'])
  31
+    >>> print md
  32
+    <section class="level1 title" id="section_the_header"><h1 class="title" id="the_header">Some Header</h1>
24 33
     </section>
25 34
 
26 35
 Two sections:
@@ -32,11 +41,11 @@
32 41
     ... ## Some second level header
33 42
     ... Some more text
34 43
     ... '''
35  
-    >>> md = markdown.markdown(text, ['headerid', 'sections'])
  44
+    >>> md = markdown.markdown(text, ['attr_list', 'headerid', 'sections'])
36 45
     >>> print md
37  
-    <section class="level1" id="some-header"><h1>Some Header</h1>
  46
+    <section class="level1 has1" id="section_some-header"><h1 id="some-header">Some Header</h1>
38 47
     <p>Some text</p>
39  
-    <section class="level2" id="some-second-level-header"><h2>Some second level header</h2>
  48
+    <section class="level2" id="section_some-second-level-header"><h2 id="some-second-level-header">Some second level header</h2>
40 49
     <p>Some more text</p>
41 50
     </section></section>
42 51
 
@@ -51,13 +60,13 @@
51 60
     ... ## Another second level header
52 61
     ... Even more text
53 62
     ... '''
54  
-    >>> md = markdown.markdown(text, ['headerid', 'sections'])
  63
+    >>> md = markdown.markdown(text, ['attr_list', 'headerid', 'sections'])
55 64
     >>> print md
56  
-    <section class="level1" id="some-header"><h1>Some Header</h1>
  65
+    <section class="level1 has2" id="section_some-header"><h1 id="some-header">Some Header</h1>
57 66
     <p>Some text</p>
58  
-    <section class="level2" id="some-second-level-header"><h2>Some second level header</h2>
  67
+    <section class="level2" id="section_some-second-level-header"><h2 id="some-second-level-header">Some second level header</h2>
59 68
     <p>Some more text</p>
60  
-    </section><section class="level2" id="another-second-level-header"><h2>Another second level header</h2>
  69
+    </section><section class="level2" id="section_another-second-level-header"><h2 id="another-second-level-header">Another second level header</h2>
61 70
     <p>Even more text</p>
62 71
     </section></section>
63 72
 
@@ -86,7 +95,6 @@ def is_true(s, default=False):
86 95
         return True
87 96
     return default
88 97
 
89  
-
90 98
 class SectionsAssember(object):
91 99
 
92 100
     def __init__(self, md, config):
@@ -95,10 +103,10 @@ def __init__(self, md, config):
95 103
         self.section_stack = []
96 104
         self.current_section = None
97 105
         self.current_level = 0
98  
-        self.hijack_header_id = is_true(self._get_config_value('hijack_header_id'))
99 106
         self.class_prefix = self._get_config_value('class_prefix')
100 107
         self.max_level = int(self._get_config_value('max_level'))
101  
-        self.headers = ['h%d' % level for level in range(self.max_level)]
  108
+        self.allheaders = ['h%d' % level for level in range(1, 7)]
  109
+        self.headers = ['h%d' % level for level in range(1, self.max_level+1)]
102 110
 
103 111
     def _get_config_value(self, key):
104 112
 
@@ -113,15 +121,16 @@ def get_level(self, header):
113 121
 
114 122
     def make_section(self, header, parent):
115 123
         atts = {}
116  
-        if self.hijack_header_id:
117  
-            header_id = header.get('id', None)
118  
-            if header_id:
119  
-                atts['id'] = header_id
120  
-                header.set('id', '')
121  
-                del header.attrib['id']
  124
+        header_id = header.get('id', None)
  125
+        if header_id:
  126
+            atts['id'] = 'section_' + header_id
  127
+        css_class = ''
122 128
         if self.class_prefix:
123 129
             css_class = '%s%d' % (self.class_prefix, self.get_level(header))
124  
-            atts['class'] = css_class
  130
+        header_class = header.get('class', None)
  131
+        if header_class:
  132
+            css_class = css_class + ' ' + header_class
  133
+        atts['class'] = css_class.strip()
125 134
         return etree.SubElement(parent, 'section', atts)
126 135
 
127 136
     def begin_section(self, header, parent):
@@ -132,6 +141,18 @@ def begin_section(self, header, parent):
132 141
         if not self.current_section is None:
133 142
             self.section_stack.append((self.current_section, self.current_level))
134 143
             parent = self.current_section
  144
+            css_classes = self.current_section.get('class', 'has0').split(' ')
  145
+            count = 0
  146
+            for css_class in css_classes:
  147
+                if css_class.startswith('has'):
  148
+                    count = int(css_class.replace('has', ''))
  149
+            has_class = 'has{count}'.format(count=count)
  150
+            if has_class in css_classes:
  151
+                css_classes.remove(has_class)
  152
+            count = count + 1
  153
+            css_classes.append('has' + str(count))
  154
+            self.current_section.set('class', ' '.join(css_classes))
  155
+
135 156
         self.current_section = self.make_section(header, parent)
136 157
         self.current_level = level
137 158
 
@@ -150,6 +171,7 @@ def add_element(self, element, parent):
150 171
 
151 172
     def assemble(self, elem):
152 173
         section = None
  174
+        children = elem.getchildren
153 175
         for child in elem.getchildren():
154 176
             if child.tag in self.headers:
155 177
                 self.begin_section(child, elem)
@@ -179,8 +201,7 @@ def __init__(self, configs):
179 201
         # set defaults
180 202
         self.config = {
181 203
                 'max_level' : ['3', 'Maximum header level for adding sections.'],
182  
-                'class_prefix' : ['level', 'Prefix for section\'s class attribute.'],
183  
-                'hijack_header_id' : ['True', 'Uses the header\'s id for the section.']
  204
+                'class_prefix' : ['level', 'Prefix for section\'s class attribute.']
184 205
             }
185 206
 
186 207
         for key, value in configs:

0 notes on commit f0e7c07

Please sign in to comment.
Something went wrong with that request. Please try again.