Merge pull request #23 from rtfd/doc-name-slugging

Use more proper slugging on filenames for object
pull/28/head
Eric Holscher 9 years ago
commit 3a22ae63e0

@ -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

@ -211,12 +211,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,5 +1,8 @@
# coding=utf8
'''Test .NET autoapi objects'''
import os
import unittest
from autoapi.mappers import dotnet
@ -94,3 +97,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, os.path.join('Foo', 'Bar', 'Widget'))
cls = dotnet.DotNetClass({'id': 'Foo.Bar.Widget<T>'})
self.assertEqual(cls.pathname, os.path.join('Foo', 'Bar', 'Widget-T'))
cls = dotnet.DotNetClass({'id': 'Foo.Bar.Widget<T>(TFoo)'})
self.assertEqual(cls.pathname, os.path.join('Foo', 'Bar', 'Widget-T'))
cls = dotnet.DotNetClass({'id': 'Foo.Foo-Bar.Widget<T>(TFoo)'})
self.assertEqual(cls.pathname, os.path.join('Foo', 'FooBar', 'Widget-T'))
cls = dotnet.DotNetClass({'id': u'Foo.Bär'})
self.assertEqual(cls.pathname, os.path.join('Foo', 'Bar'))
cls = dotnet.DotNetClass({'id': u'Ащщ.юИфк'})
self.assertEqual(cls.pathname, os.path.join('Ashchshch', 'iuIfk'))

Loading…
Cancel
Save