Skip to content

Wrong default encoding #532

Closed
Closed

Description

@maemual

I love this project. But i find that, if i use chinese character as the name of markdown file and directory, the encoding will go wrong.
PS: I use python 2.7 on Mac OS X 10.10.3 and Ubuntu 14.04

Traceback (most recent call last):
  File "/Users/maemual/Workspace/mkdocs/env/bin/mkdocs", line 9, in <module>
    load_entry_point('mkdocs==0.13.0.dev0', 'console_scripts', 'mkdocs')()
  File "/Users/maemual/Workspace/mkdocs/env/lib/python2.7/site-packages/click/core.py", line 664, in __call__
    return self.main(*args, **kwargs)
  File "/Users/maemual/Workspace/mkdocs/env/lib/python2.7/site-packages/click/core.py", line 644, in main
    rv = self.invoke(ctx)
  File "/Users/maemual/Workspace/mkdocs/env/lib/python2.7/site-packages/click/core.py", line 991, in invoke
    return _process_result(sub_ctx.command.invoke(sub_ctx))
  File "/Users/maemual/Workspace/mkdocs/env/lib/python2.7/site-packages/click/core.py", line 837, in invoke
    return ctx.invoke(self.callback, **ctx.params)
  File "/Users/maemual/Workspace/mkdocs/env/lib/python2.7/site-packages/click/core.py", line 464, in invoke
    return callback(*args, **kwargs)
  File "/Users/maemual/Workspace/mkdocs/mkdocs/cli.py", line 83, in serve_command
    livereload=livereload,
  File "/Users/maemual/Workspace/mkdocs/mkdocs/serve.py", line 77, in serve
    config = builder()
  File "/Users/maemual/Workspace/mkdocs/mkdocs/serve.py", line 73, in builder
    build(config, live_server=True)
  File "/Users/maemual/Workspace/mkdocs/mkdocs/build.py", line 306, in build
    build_pages(config)
  File "/Users/maemual/Workspace/mkdocs/mkdocs/build.py", line 252, in build_pages
    if not build_template('search.html', env, config, site_navigation):
  File "/Users/maemual/Workspace/mkdocs/mkdocs/build.py", line 166, in build_template
    output_content = template.render(context)
  File "/Users/maemual/Workspace/mkdocs/env/lib/python2.7/site-packages/jinja2/environment.py", line 969, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/maemual/Workspace/mkdocs/env/lib/python2.7/site-packages/jinja2/environment.py", line 742, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/Users/maemual/Workspace/mkdocs/mkdocs/themes/readthedocs/search.html", line 1, in top-level template code
    {% extends "base.html" %}
  File "/Users/maemual/Workspace/mkdocs/mkdocs/themes/readthedocs/base.html", line 62, in top-level template code
    <li>{% include "toc.html" %}<li>
  File "/Users/maemual/Workspace/mkdocs/mkdocs/themes/readthedocs/toc.html", line 3, in top-level template code
    <li><span>{{ nav_item.title }}</span></li>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 0: ordinal not in range(128)

I use a magic method to fix it which just add next 3 lines of code at the beginning of program.

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

I know the use of sys.setdefaultencoding() has always been discouraged, and it has become a no-op in py3k. But i really don't know how to fix it correctly.

So, can you help me to fix it?

Activity

  1. d0ugal commented on May 16, 2015

    @d0ugal
    Contributor

    Ah, interesting. I hadn't considered unicode filenames, and I've certainly not tested that or seen anyone else try. I'll need to do some investigation on how to handle this, it's a new one to me 😄

  2. added this to the 0.13.0 milestone on May 16, 2015
  3. d0ugal commented on May 16, 2015

    @d0ugal
    Contributor

    I've marked this as the next release, but as we are close to the end of that development cycle I may have to push it to then next one. It depends how easily resolvable this is.

  4. d0ugal commented on May 18, 2015

    @d0ugal
    Contributor

    Does it work if you use Python 3? I've done some initial testing and found that I have the same error on Python 2 but everything works on Python 3

  5. waylan commented on May 18, 2015

    @waylan
    Contributor

    The update to this comment might be of interest regarding non-ascii file names:

    Note, os.walk returns Unicode strings when the given path is a Unicode string. A normal string path will return binary strings. This is the same behaviour as os.listdir (http://docs.python.org/2/library/os.html#os.listdir).

    I can't say that I ever knew that. I suppose as long as you pass unicode strings to os.walk (here?) then all the returned file names will already be unicode.?? Which explains why this works fine on Python 3 but fails on Python 2. In Python 3 the strings would all be unicode by default, but not necessarily in Python 2.

    Btw, my above comments are based on the assumption that the page title is being generated from the filename. What happens if the page title is explicitly defined in the config? I agree it should work either way, but at least that might be a workaround till this gets fixed.

  6. waylan commented on May 18, 2015

    @waylan
    Contributor

    It occurs to me that the easy fix may be to force all strings loaded by the YAML config file to be unicode. See https://stackoverflow.com/questions/2890146/how-to-force-pyyaml-to-load-strings-as-unicode-objects. That is, after all, what is already happening in Python 3.

  7. d0ugal commented on May 18, 2015

    @d0ugal
    Contributor

    Interesting, I thought pyYAML was already loading everything as Unicode.

  8. facelessuser commented on May 18, 2015

    @facelessuser
    Contributor

    Interesting, I thought pyYAML was already loading everything as Unicode.

    Nope, at least not for PY2. It only returns as unicode for strings that have non-ascii strings as I recall. You can override it very similar to OrderedDict was done until we removed it.

  9. d0ugal commented on May 18, 2015

    @d0ugal
    Contributor

    Sounds like that would be a good idea, to make things consistent.

  10. added 2 commits that reference this issue on May 21, 2015
    ba381d6
    6679927
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions