Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed a bug where the you could get encode/decode exceptions if JavaScript had non-ascii characters. #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions sphinxcontrib/autojs.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import os.path
import re
import io
from docutils import nodes
from docutils.statemachine import ViewList
from pygments.lexers import JavascriptLexer, LEXERS
Expand Down Expand Up @@ -84,9 +85,9 @@ def handle_signature(self, sig, signode):
signode += addnodes.desc_annotation(sig_prefix, sig_prefix)
sig = sig.split(".")[-1]
names = super(JSClassmember, self).handle_signature(sig, signode)
name_prefix = self.env.temp_data.get("js:class")
name_prefix = self.env.temp_data.get("object")
if name_prefix:
return self.make_prefix(name_prefix) + names[0], names[1]
return self.make_prefix(name_prefix[0]) + names[0], names[1]
else:
return names

Expand Down Expand Up @@ -231,9 +232,9 @@ class JavaScriptDocument(object):
(?P<docsuffix> \*/)
""", re.VERBOSE | re.MULTILINE | re.DOTALL)

def __init__(self, path):
with open(path) as f:
self.source = "".join(f.readlines())
def __init__(self, path, encoding="utf-8-sig"):
with io.open(path, encoding=encoding) as f:
self.source = f.read()

def get_description(self):
match = self._MODULE_DOCSTRING_RE.match(self.source)
Expand All @@ -244,7 +245,7 @@ def get_description(self):
def get_docstrings(self):
matches = self._DOCSTRING_RE.finditer(self.source)
if not matches:
raise ValueError("There is no any named docstring.")
raise ValueError("There is not any named docstring.")
for match in matches:
yield JavaScriptDocstring.from_match(match)

Expand All @@ -257,18 +258,15 @@ def auto_include_desc(self, rst, options):

def auto_include_members(self, rst, options):
members = options.get("members")
compare = self._make_comparer(options.get("member-order"))
docstrings = list(self.get_docstrings())
docstrings.sort(cmp=compare)
if members is not None:
exclude_members = options.get("exclude-members", [])
is_member = self._make_member_checker(members, exclude_members)
compare = self._make_comparer(options.get("member-order"))
docstrings = list(self.get_docstrings())
docstrings.sort(cmp=compare)
for doc in docstrings:
if is_member(doc):
rst.append(doc.to_rst(docstrings, is_member=is_member))
else:
for doc in docstrings:
rst.append(doc.to_rst(docstrings))

def _make_member_checker(self, members, exclude_members):
__members__ = members
Expand Down Expand Up @@ -338,12 +336,16 @@ def add_lines(self, lines):
self.add_line(line)

def run(self):
config = self.state.document.settings.env.config
source_encoding = config['source_encoding']
self.result = ViewList()
path = self.arguments[0]
filename = os.path.basename(path)
node = nodes.section()
node.document = self.state.document
self.add_lines(JavaScriptDocument(path).to_rst(self.options))
self.add_lines(
JavaScriptDocument(
path, encoding=source_encoding).to_rst(self.options))
nested_parse_with_titles(self.state, self.result, node)
return node.children

Expand Down