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 os
import fnmatch import fnmatch
from collections import OrderedDict from collections import OrderedDict
import unidecode
from jinja2 import Environment, FileSystemLoader, TemplateNotFound from jinja2 import Environment, FileSystemLoader, TemplateNotFound
from sphinx.util.console import darkgreen, bold from sphinx.util.console import darkgreen, bold
from sphinx.util.osutil import ensuredir from sphinx.util.osutil import ensuredir
@ -95,6 +97,27 @@ class PythonMapperBase(object):
'''Shorten name property''' '''Shorten name property'''
return self.name.split('.')[-1] 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 @property
def ref_type(self): def ref_type(self):
return self.type return self.type

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

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

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

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

@ -1,5 +1,8 @@
# coding=utf8
'''Test .NET autoapi objects''' '''Test .NET autoapi objects'''
import os
import unittest import unittest
from autoapi.mappers import dotnet from autoapi.mappers import dotnet
@ -94,3 +97,18 @@ class NamespaceTests(unittest.TestCase):
cls = dotnet.DotNetClass(dict(id='Foo', cls = dotnet.DotNetClass(dict(id='Foo',
type='class')) type='class'))
self.assertIsNone(cls.namespace) 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