Use more proper slugging on filenames for object

This resolves some issues with special characters in paths on Windows based
systems. It replaces special characters and unicode characters to ensure paths
are predictable.
pull/23/head
Anthony Johnson 9 years ago
parent f8e1d7435c
commit 87a16ea421

@ -1,7 +1,9 @@
import re
import os
import fnmatch
from collections import OrderedDict
import unidecode
from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from sphinx.util.console import darkgreen, bold
from sphinx.util.osutil import ensuredir
@ -95,6 +97,27 @@ class PythonMapperBase(object):
'''Shorten name property'''
return self.name.split('.')[-1]
@property
def pathname(self):
'''Sluggified path for filenames
Slugs to a filename using the follow steps
* Decode unicode to approximate ascii
* Remove existing hypens
* Substitute hyphens for non-word characters
* Break up the string as paths
'''
slug = self.name
try:
slug = self.name.split('(')[0]
except IndexError:
pass
slug = unidecode.unidecode(slug)
slug = slug.replace('-', '')
slug = re.sub(r'[^\w\.]+', '-', slug).strip('-')
return os.path.join(*slug.split('.'))
@property
def ref_type(self):
return self.type

@ -179,12 +179,7 @@ class DotNetSphinxMapper(SphinxMapperBase):
if not rst:
continue
try:
filename = obj.name.split('(')[0]
except IndexError:
filename = id
filename = filename.replace('#', '-')
detail_dir = os.path.join(root, *filename.split('.'))
detail_dir = os.path.join(root, obj.pathname)
ensuredir(detail_dir)
path = os.path.join(detail_dir, '%s%s' % ('index', source_suffix))
with open(path, 'wb') as detail_file:

@ -15,7 +15,7 @@
{% for item in obj.children|sort %}
{% if item.type != 'namespace' %}
/autoapi/{{ item.name.split('.')|join('/') }}/index
/autoapi/{{ item.pathname }}/index
{% endif %}
{% endfor %}
@ -30,7 +30,7 @@
{% for item in obj.references|sort %}
{% if item.type != 'namespace' %}
/autoapi/{{ item.name.split('.')|join('/') }}/index
/autoapi/{{ item.pathname }}/index
{% endif %}
{% endfor %}

@ -9,7 +9,7 @@
:maxdepth: 4
{% for item in obj.children|sort %}
/autoapi/{{ item.id.split('.')|join('/') }}/index
/autoapi/{{ item.pathname }}/index
{%- endfor %}
{% endif %}

@ -9,6 +9,7 @@ try:
'sphinx',
'sphinxcontrib-golangdomain',
'sphinxcontrib-dotnetdomain',
'unidecode',
],
test_suite='nose.collector',
tests_require=['nose', 'mock'],
@ -23,6 +24,7 @@ except ImportError:
'sphinx'
'sphinxcontrib-golangdomain',
'sphinxcontrib-dotnetdomain',
'unidecode',
],
)

@ -1,3 +1,5 @@
# coding=utf8
'''Test .NET autoapi objects'''
import unittest
@ -94,3 +96,18 @@ class NamespaceTests(unittest.TestCase):
cls = dotnet.DotNetClass(dict(id='Foo',
type='class'))
self.assertIsNone(cls.namespace)
def test_filename(self):
'''Object file name'''
cls = dotnet.DotNetClass({'id': 'Foo.Bar.Widget'})
self.assertEqual(cls.pathname, 'Foo/Bar/Widget')
cls = dotnet.DotNetClass({'id': 'Foo.Bar.Widget<T>'})
self.assertEqual(cls.pathname, 'Foo/Bar/Widget-T')
cls = dotnet.DotNetClass({'id': 'Foo.Bar.Widget<T>(TFoo)'})
self.assertEqual(cls.pathname, 'Foo/Bar/Widget-T')
cls = dotnet.DotNetClass({'id': 'Foo.Foo-Bar.Widget<T>(TFoo)'})
self.assertEqual(cls.pathname, 'Foo/FooBar/Widget-T')
cls = dotnet.DotNetClass({'id': u'Foo.Bär'})
self.assertEqual(cls.pathname, u'Foo/Bar')
cls = dotnet.DotNetClass({'id': u'Ащщ.юИфк'})
self.assertEqual(cls.pathname, u'Ashchshch/iuIfk')

Loading…
Cancel
Save