Skip to content

Commit

Permalink
Fix capitalisation in header links (copy verbatim from MW), fix hyphens
Browse files Browse the repository at this point in the history
in header links

Closes #17
  • Loading branch information
projectgus committed Mar 20, 2015
1 parent 5791c47 commit 8158a51
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
16 changes: 16 additions & 0 deletions dokuwiki.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,22 @@ def make_dokuwiki_pagename(mediawiki_name):
result = mediawiki_name.replace(" ","_")
return names.clean_id(camel_to_underscore(result)).replace("/",":")

def make_dokuwiki_heading_id(mw_heading_name):
"""
Convert a Mediawiki internal anchor heading link to the Dokuwiki anchor heading link id
Equivalent function in dokuwiki is _headerToLink in inc/parser/xhtml.php
which calls sectionID in inc/pageutils.php
"""
result = names.clean_id(mw_heading_name, True)
result = re.sub(r'[:.]', '', result)

nums_stripped = result.lstrip("0123456789_-")
if len(nums_stripped):
return nums_stripped
else:
return "section"+re.sub(r"[^0-9]+", "", result)

def camel_to_underscore(camelcase):
"""
Convert a camelcased string to underscore_delimited (tweaked from this StackOverflow answer)
Expand Down
2 changes: 1 addition & 1 deletion names.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"""
import re, os.path, unicodedata

def clean_id(name):
def clean_id(name, preserve_case=False):
"""
Return a 'clean' dokuwiki-compliant name. Based on the cleanID() PHP function in inc/pageutils.php
Expand Down
13 changes: 10 additions & 3 deletions wikicontent.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,12 +259,19 @@ def convert(node, trailing_newline):
print("WARNING: Unsupported node type: %s" % (node.__class__))
return convert_children(node)


def convert_internal_link(mw_target):
"""
Convert an internal Mediawiki link, with or without an anchor # in the middle.
Same as converting a plain pagename, only we want to preserve any #s in the target text.
"""
parts = mw_target.split("#")
return "#".join([dokuwiki.make_dokuwiki_pagename(part) for part in parts])
if "#" in mw_target:
page,anchor = mw_target.split("#",1)
else:
page = mw_target
anchor = None
if len(page):
page = dokuwiki.make_dokuwiki_pagename(page)
if anchor is not None:
page = page + "#" + dokuwiki.make_dokuwiki_heading_id(anchor)
return page

0 comments on commit 8158a51

Please sign in to comment.