Skip to content

Commit

Permalink
add the custom tab label option. fixes #1
Browse files Browse the repository at this point in the history
  • Loading branch information
yacir committed Jun 16, 2017
1 parent 99d6eda commit 96d5f3f
Showing 1 changed file with 17 additions and 5 deletions.
22 changes: 17 additions & 5 deletions markdown_fenced_code_tabs.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
# Ordered so that 'data-' attrs of html are in deterministic order.
PARAM_REGEXES = odict((
('hl_lines', re.compile(r'''hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot)''')),
('fct_label', re.compile(r'''fct_label=(?P<quot>"|')(?P<fct_label>.*?)(?P=quot)''')),
))


Expand All @@ -45,6 +46,8 @@ class FencedCodeTabsPreprocessor(Preprocessor):
(\{?\.?(?P<lang>[\w#.+-]*))?[ ]* # Optional {, and lang
# Optional highlight lines, single- or double-quote-delimited
(hl_lines=(?P<quot>"|')(?P<hl_lines>.*?)(?P=quot))?[ ]*
# Optional tab label, single- or double-quote-delimited
(fct_label=(?P<fct_quot>"|')(?P<fct_label>.*?)(?P=fct_quot))?[ ]*
}?[ ]*\n # Optional closing }
(?P<code>.*?)(?<=\n)
(?P=fence)[ ]*$''', re.MULTILINE | re.DOTALL | re.VERBOSE)
Expand Down Expand Up @@ -126,9 +129,14 @@ def _identify_code_tabs(self, block_str):
raise Exception("{} needs an argument within \n{}".format(param, first_line))

lang = ''
label = ''

if m.group('lang') and m.group('lang') not in PARAM_REGEXES:
lang = m.group('lang')
label = lang[0:1].capitalize() + lang[1:]

if kwargs.get('fct_label'):
label = kwargs.get('fct_label')

if self.codehilite_conf:
highliter = CodeHilite(
Expand All @@ -147,7 +155,7 @@ def _identify_code_tabs(self, block_str):
code = self.TAB_ITEM_BODY_WRAP_ESCAPE % (lang,
self._escape(m.group('code')))

self.tab_items.append(FencedCodeTabs(lang, lang, code))
self.tab_items.append(FencedCodeTabs(label, lang, code))

placeholder = self.tab_item_placeholder.format(len(self.tab_items) - 1)

Expand Down Expand Up @@ -247,7 +255,7 @@ class FencedCodeTabsSet(object):
"""
TAB_SET_HANDLE_TEMPLATE = """
<li class="nav-item">
<a href="#{id}" aria-controls="{id}" role="tab" data-toggle="tab" data-lang="{lang}">{ulang}</a>
<a href="#{id}" aria-controls="{id}" role="tab" data-toggle="tab" data-lang="{lang}">{label}</a>
</li>
"""
TAB_SET_TAB_CONTAINER_TEMPLATE = """
Expand Down Expand Up @@ -297,11 +305,12 @@ def __str__(self):

tab_set_id = self._get_tab_id(tab)
lang = tab.get_lang()
label = tab.get_label()

tab_handles += self.TAB_SET_HANDLE_TEMPLATE.format(id=tab_set_id,
isTabActiveClass=tab_active_class,
lang=lang,
ulang=lang[0:1].capitalize() + lang[1:])
label=label)

tabs += self.TAB_BODY_CONTAINER_TEMPLATE.format(id=tab_set_id,
isTabActiveClass=tab_active_class,
Expand All @@ -325,8 +334,8 @@ def __repr__(self):
# Fenced Code Nav Item
class FencedCodeTabs(object):

def __init__(self, name, lang, body):
self.name = name
def __init__(self, label, lang, body):
self.label = label
self.lang = lang
self.tabBody = body

Expand All @@ -336,6 +345,9 @@ def __str__(self):
def get_lang(self):
return self.lang

def get_label(self):
return self.label

def __repr__(self):
return self.__str__()

Expand Down

0 comments on commit 96d5f3f

Please sign in to comment.