示例#1
0
class HTMLFile(ImportedFile):

    """
    Imported HTML file Proxy model.

    This tracks only the HTML files for indexing to search.
    """

    class Meta(object):
        proxy = True

    objects = HTMLFileManager()

    def get_processed_json(self):
        """
        Get the parsed JSON for search indexing.

        Check for two paths for each index file
        This is because HTMLDir can generate a file from two different places:

        * foo.rst
        * foo/index.rst

        Both lead to `foo/index.html`
        https://github.com/rtfd/readthedocs.org/issues/5368
        """
        paths = []
        basename = os.path.splitext(self.path)[0]
        paths.append(basename + '.fjson')
        if basename.endswith('/index'):
            new_basename = re.sub(r'\/index$', '', basename)
            paths.append(new_basename + '.fjson')

        full_json_path = self.project.get_production_media_path(
            type_='json', version_slug=self.version.slug, include_file=False
        )
        try:
            for path in paths:
                file_path = os.path.join(full_json_path, path)
                if os.path.exists(file_path):
                    return process_file(file_path)
        except Exception:
            log.warning(
                'Unhandled exception during search processing file: %s',
                file_path,
            )
        return {
            'headers': [],
            'content': '',
            'path': file_path,
            'title': '',
            'sections': [],
        }

    @cached_property
    def processed_json(self):
        return self.get_processed_json()
示例#2
0
class HTMLFile(ImportedFile):
    """
    Imported HTML file Proxy model.

    This tracks only the HTML files for indexing to search.
    """
    class Meta:
        proxy = True

    objects = HTMLFileManager.from_queryset(HTMLFileQuerySet)()

    def get_processed_json(self):
        """
        Get the parsed JSON for search indexing.

        Check for two paths for each index file
        This is because HTMLDir can generate a file from two different places:

        * foo.rst
        * foo/index.rst

        Both lead to `foo/index.html`
        https://github.com/rtfd/readthedocs.org/issues/5368
        """
        file_path = None
        storage = get_storage_class(settings.RTD_BUILD_MEDIA_STORAGE)()

        fjson_paths = []
        basename = os.path.splitext(self.path)[0]
        fjson_paths.append(basename + '.fjson')
        if basename.endswith('/index'):
            new_basename = re.sub(r'\/index$', '', basename)
            fjson_paths.append(new_basename + '.fjson')

        storage_path = self.project.get_storage_path(
            type_='json', version_slug=self.version.slug, include_file=False)
        try:
            for fjson_path in fjson_paths:
                file_path = storage.join(storage_path, fjson_path)
                if storage.exists(file_path):
                    return process_file(file_path)
        except Exception:
            log.warning(
                'Unhandled exception during search processing file: %s',
                file_path,
            )

        return {
            'path': file_path,
            'title': '',
            'sections': [],
            'domain_data': {},
        }

    @cached_property
    def processed_json(self):
        return self.get_processed_json()
示例#3
0
class HTMLFile(ImportedFile):
    """
    Imported HTML file Proxy model.

    This tracks only the HTML files for indexing to search.
    """
    class Meta(object):
        proxy = True

    objects = HTMLFileManager()

    @cached_property
    def json_file_path(self):
        basename = os.path.splitext(self.path)[0]
        if self.project.documentation_type == 'sphinx_htmldir' and basename.endswith(
                '/index'):
            new_basename = re.sub(r'\/index$', '', basename)
            log.info(
                'Adjusted json file path: %s -> %s',
                basename,
                new_basename,
            )
            basename = new_basename

        file_path = basename + '.fjson'

        full_json_path = self.project.get_production_media_path(
            type_='json', version_slug=self.version.slug, include_file=False)

        file_path = os.path.join(full_json_path, file_path)
        return file_path

    def get_processed_json(self):
        file_path = self.json_file_path
        try:
            return process_file(file_path)
        except Exception:
            log.warning(
                'Unhandled exception during search processing file: %s',
                file_path,
            )
        return {
            'headers': [],
            'content': '',
            'path': file_path,
            'title': '',
            'sections': [],
        }

    @cached_property
    def processed_json(self):
        return self.get_processed_json()
示例#4
0
class HTMLFile(ImportedFile):

    """
    Imported HTML file Proxy model.

    This tracks only the HTML files for indexing to search.
    """

    class Meta(object):
        proxy = True

    objects = HTMLFileManager()

    @cached_property
    def json_file_path(self):
        basename = os.path.splitext(self.path)[0]
        file_path = basename + '.fjson'

        full_json_path = self.project.get_production_media_path(type_='json',
                                                                version_slug=self.version.slug,
                                                                include_file=False)

        file_path = os.path.join(full_json_path, file_path)
        return file_path

    def get_processed_json(self):
        file_path = self.json_file_path
        try:
            return process_file(file_path)
        except Exception:
            log.warning('Unhandled exception during search processing file: %s' % file_path)
        return {
            'headers': [],
            'content': '',
            'path': file_path,
            'title': '',
            'sections': []
        }

    @cached_property
    def processed_json(self):
        return self.get_processed_json()