コード例 #1
0
ファイル: documentation.py プロジェクト: prah23/zulip
    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
        article = kwargs["article"]
        context: Dict[str, Any] = super().get_context_data()
        (context["article"], http_status_ignored) = self.get_path(article)

        # For disabling the "Back to home" on the homepage
        context["not_index_page"] = not context["article"].endswith(
            "/index.md")
        if self.path_template == "/zerver/help/%s.md":
            context["page_is_help_center"] = True
            context["doc_root"] = "/help/"
            (sidebar_index,
             http_status_ignored) = self.get_path("include/sidebar_index")
            title_base = "Zulip Help Center"
        else:
            context["page_is_api_center"] = True
            context["doc_root"] = "/api/"
            (sidebar_index,
             http_status_ignored) = self.get_path("sidebar_index")
            title_base = "Zulip API documentation"

        # The following is a somewhat hacky approach to extract titles from articles.
        # Hack: `context["article"] has a leading `/`, so we use + to add directories.
        article_path = os.path.join(settings.DEPLOY_ROOT,
                                    "templates") + context["article"]
        if os.path.exists(article_path):
            with open(article_path) as article_file:
                first_line = article_file.readlines()[0]
            # Strip the header and then use the first line to get the article title
            if self.path_template == "/zerver/api/%s.md" and first_line[
                    0] != "#":
                api_operation = context["OPEN_GRAPH_URL"].split(
                    "/api/")[1].replace("-", "_")
                endpoint_path, endpoint_method = get_endpoint_from_operationid(
                    api_operation)
                article_title = get_openapi_summary(endpoint_path,
                                                    endpoint_method)
            else:
                article_title = first_line.lstrip("#").strip()
            if context["not_index_page"]:
                context["OPEN_GRAPH_TITLE"] = f"{article_title} ({title_base})"
            else:
                context["OPEN_GRAPH_TITLE"] = title_base
            self.request.placeholder_open_graph_description = (
                f"REPLACMENT_OPEN_GRAPH_DESCRIPTION_{int(2**24 * random.random())}"
            )
            context[
                "OPEN_GRAPH_DESCRIPTION"] = self.request.placeholder_open_graph_description

        context["sidebar_index"] = sidebar_index
        # An "article" might require the api_uri_context to be rendered
        api_uri_context: Dict[str, Any] = {}
        add_api_uri_context(api_uri_context, self.request)
        api_uri_context["run_content_validators"] = True
        context["api_uri_context"] = api_uri_context
        add_google_analytics_context(context)
        return context
コード例 #2
0
    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
        article = kwargs["article"]
        context: Dict[str, Any] = super().get_context_data()
        (context["article"], http_status_ignored) = self.get_path(article)

        # For disabling the "Back to home" on the homepage
        context["not_index_page"] = not context["article"].endswith(
            "/index.md")
        if self.path_template == '/zerver/help/%s.md':
            context["page_is_help_center"] = True
            context["doc_root"] = "/help/"
            (sidebar_index,
             http_status_ignored) = self.get_path("include/sidebar_index")
            # We want the sliding/collapsing behavior for /help pages only
            sidebar_class = "sidebar slide"
            title_base = "Zulip Help Center"
        else:
            context["page_is_api_center"] = True
            context["doc_root"] = "/api/"
            (sidebar_index,
             http_status_ignored) = self.get_path("sidebar_index")
            sidebar_class = "sidebar"
            title_base = "Zulip API Documentation"

        # The following is a somewhat hacky approach to extract titles from articles.
        # Hack: `context["article"] has a leading `/`, so we use + to add directories.
        article_path = os.path.join(settings.DEPLOY_ROOT,
                                    'templates') + context["article"]
        if os.path.exists(article_path):
            with open(article_path) as article_file:
                first_line = article_file.readlines()[0]
            # Strip the header and then use the first line to get the article title
            article_title = first_line.lstrip("#").strip()
            if context["not_index_page"]:
                context["OPEN_GRAPH_TITLE"] = "%s (%s)" % (article_title,
                                                           title_base)
            else:
                context["OPEN_GRAPH_TITLE"] = title_base
            self.request.placeholder_open_graph_description = (
                "REPLACMENT_OPEN_GRAPH_DESCRIPTION_%s" %
                (int(2**24 * random.random()), ))
            context[
                "OPEN_GRAPH_DESCRIPTION"] = self.request.placeholder_open_graph_description

        context["sidebar_index"] = sidebar_index
        context["sidebar_class"] = sidebar_class
        # An "article" might require the api_uri_context to be rendered
        api_uri_context: Dict[str, Any] = {}
        add_api_uri_context(api_uri_context, self.request)
        api_uri_context["run_content_validators"] = True
        context["api_uri_context"] = api_uri_context
        add_google_analytics_context(context)
        return context
コード例 #3
0
 def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
     context: Dict[str, Any] = super().get_context_data(**kwargs)
     add_integrations_context(context)
     add_integrations_open_graph_context(context, self.request)
     add_google_analytics_context(context)
     return context
コード例 #4
0
    def get_context_data(self, **kwargs: Any) -> Dict[str, Any]:
        article = kwargs["article"]
        context: Dict[str, Any] = super().get_context_data()

        documentation_article = self.get_path(article)
        context["article"] = documentation_article.article_path
        if documentation_article.article_path.startswith("/") and os.path.exists(
            documentation_article.article_path
        ):
            # Absolute path case
            article_path = documentation_article.article_path
        elif documentation_article.article_path.startswith("/"):
            # Hack: `context["article"] has a leading `/`, so we use + to add directories.
            article_path = (
                os.path.join(settings.DEPLOY_ROOT, "templates") + documentation_article.article_path
            )
        else:
            article_path = os.path.join(
                settings.DEPLOY_ROOT, "templates", documentation_article.article_path
            )

        # For disabling the "Back to home" on the homepage
        context["not_index_page"] = not context["article"].endswith("/index.md")
        if self.path_template == "/zerver/help/%s.md":
            context["page_is_help_center"] = True
            context["doc_root"] = "/help/"
            context["doc_root_title"] = "Help center"
            sidebar_article = self.get_path("include/sidebar_index")
            sidebar_index = sidebar_article.article_path
            title_base = "Zulip Help Center"
        elif self.path_template == f"{settings.POLICIES_DIRECTORY}/%s.md":
            context["page_is_policy_center"] = True
            context["doc_root"] = "/policies/"
            context["doc_root_title"] = "Terms and policies"
            sidebar_article = self.get_path("sidebar_index")
            sidebar_index = sidebar_article.article_path
            title_base = "Zulip terms and policies"
        else:
            context["page_is_api_center"] = True
            context["doc_root"] = "/api/"
            context["doc_root_title"] = "API documentation"
            sidebar_article = self.get_path("sidebar_index")
            sidebar_index = sidebar_article.article_path
            title_base = "Zulip API documentation"

        # The following is a somewhat hacky approach to extract titles from articles.
        endpoint_name = None
        endpoint_method = None
        if os.path.exists(article_path):
            with open(article_path) as article_file:
                first_line = article_file.readlines()[0]
            # Strip the header and then use the first line to get the article title
            if context["article"] == "/zerver/api/api-doc-template.md":
                endpoint_name, endpoint_method = (
                    documentation_article.endpoint_path,
                    documentation_article.endpoint_method,
                )
                assert endpoint_name is not None
                assert endpoint_method is not None
                article_title = get_openapi_summary(endpoint_name, endpoint_method)
            elif self.path_template == "/zerver/api/%s.md" and "{generate_api_title(" in first_line:
                api_operation = context["OPEN_GRAPH_URL"].split("/api/")[1]
                endpoint_name, endpoint_method = get_endpoint_from_operationid(api_operation)
                article_title = get_openapi_summary(endpoint_name, endpoint_method)
            else:
                article_title = first_line.lstrip("#").strip()
                endpoint_name = endpoint_method = None
            if context["not_index_page"]:
                context["OPEN_GRAPH_TITLE"] = f"{article_title} ({title_base})"
            else:
                context["OPEN_GRAPH_TITLE"] = title_base
            request_notes = RequestNotes.get_notes(self.request)
            request_notes.placeholder_open_graph_description = (
                f"REPLACMENT_OPEN_GRAPH_DESCRIPTION_{int(2**24 * random.random())}"
            )
            context["OPEN_GRAPH_DESCRIPTION"] = request_notes.placeholder_open_graph_description

        context["sidebar_index"] = sidebar_index
        # An "article" might require the api_uri_context to be rendered
        api_uri_context: Dict[str, Any] = {}
        add_api_uri_context(api_uri_context, self.request)
        api_uri_context["run_content_validators"] = True
        context["api_uri_context"] = api_uri_context
        if endpoint_name and endpoint_method:
            context["api_uri_context"]["API_ENDPOINT_NAME"] = endpoint_name + ":" + endpoint_method
        add_google_analytics_context(context)
        return context