kythe.io@v0.0.68-0.20240422202219-7225dbc01741/kythe/cxx/doc/html_renderer.h (about)

     1  /*
     2   * Copyright 2016 The Kythe Authors. All rights reserved.
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *   http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   */
    16  
    17  #ifndef KYTHE_CXX_DOC_HTML_RENDERER_H_
    18  #define KYTHE_CXX_DOC_HTML_RENDERER_H_
    19  
    20  #include <functional>
    21  #include <string>
    22  
    23  #include "kythe/cxx/common/kythe_uri.h"
    24  #include "kythe/cxx/doc/markup_handler.h"
    25  #include "kythe/proto/common.pb.h"
    26  #include "kythe/proto/xref.pb.h"
    27  
    28  namespace kythe {
    29  
    30  struct HtmlRendererOptions {
    31    virtual ~HtmlRendererOptions() {}
    32    /// Used to determine the href attribute value for a link pointing to an
    33    /// `Anchor`. HtmlRenderer will HTML-escape the link (e.g., ampersands
    34    /// will be replaced by &amp;).
    35    std::function<std::string(const proto::Anchor&)> make_link_uri =
    36        [](const proto::Anchor&) { return ""; };
    37    /// Used to format a location from an `Anchor`. HtmlRenderer will HTML-escape
    38    /// the resulting text.
    39    std::function<std::string(const proto::Anchor&)> format_location =
    40        [](const proto::Anchor& anchor) {
    41          auto uri = URI::FromString(anchor.ticket());
    42          return uri.first ? std::string(uri.second.v_name().path()) : "";
    43        };
    44    /// Used to determine the href attribute value for a link pointing to an
    45    /// `Anchor`. HtmlRenderer will HTML-escape the link (e.g., ampersands
    46    /// will be replaced by &amp;). semantic_ticket is the ticket associated with
    47    /// the `Anchor`. If this returns the empty string, the renderer will try
    48    /// `make_link_uri`.
    49    std::function<std::string(const proto::Anchor&, const std::string&)>
    50        make_semantic_link_uri =
    51            [](const proto::Anchor&, const std::string& semantic_ticket) {
    52              return "";
    53            };
    54    /// Used to provide additional markup after the signature for the provided
    55    /// ticket with the given `MarkedSource`. The result is included directly in
    56    /// the output without escaping.
    57    std::function<std::string(const std::string&,
    58                              const proto::common::MarkedSource&)>
    59        make_post_signature_markup =
    60            [](const std::string& semantic_ticket,
    61               const proto::common::MarkedSource& marked_soure) { return ""; };
    62    /// Used to retrieve `NodeInfo` for the given semantic ticket.
    63    virtual const proto::common::NodeInfo* node_info(const std::string&) const {
    64      return nullptr;
    65    }
    66    /// Used to retrieve a string representing the kind of the given semantic
    67    /// ticket.
    68    std::function<std::string(const std::string&)> kind_name =
    69        [](const std::string&) { return ""; };
    70    /// Used to map from an anchor's ticket to that `Anchor`.
    71    virtual const proto::Anchor* anchor_for_ticket(const std::string&) const {
    72      return nullptr;
    73    }
    74    /// Configures the CSS class to apply to the outermost div of a document.
    75    std::string doc_div = "kythe-doc";
    76    /// Configures the CSS class to apply to a tag's label (e.g, "Authors:")
    77    std::string tag_section_title_div = "kythe-doc-tag-section-title";
    78    /// Configures the CSS class to apply to a tag's content.
    79    std::string tag_section_content_div = "kythe-doc-tag-section-content";
    80    /// Configures the CSS class to apply to signature divs.
    81    std::string signature_div = "kythe-doc-element-signature";
    82    /// Configures the CSS class to apply to content divs.
    83    std::string content_div = "kythe-doc-content";
    84    /// Configures the CSS class to apply to type name divs.
    85    std::string type_name_div = "kythe-doc-type-name";
    86    /// Configures the CSS class to apply to name spans.
    87    std::string name_span = "kythe-doc-name-span";
    88    /// Configures the CSS class to apply to signature detail divs.
    89    std::string sig_detail_div = "kythe-doc-qualified-name";
    90    /// Configures the CSS class to apply to initializer section divs.
    91    std::string initializer_div =
    92        "kythe-doc-initializer-section kythe-doc-qualified-name";
    93    /// Configures the CSS class to apply to multiline initializer pres.
    94    std::string initializer_multiline_pre =
    95        "kythe-doc-initializer kythe-doc-pre-code "
    96        "kythe-doc-initializer-multiline";
    97    /// Configures the CSS class to apply to initializer pres.
    98    std::string initializer_pre = "kythe-doc-initializer kythe-doc-pre-code";
    99  };
   100  
   101  class DocumentHtmlRendererOptions : public HtmlRendererOptions {
   102   public:
   103    explicit DocumentHtmlRendererOptions(
   104        const proto::DocumentationReply& document)
   105        : document_(document) {}
   106    const proto::common::NodeInfo* node_info(const std::string&) const override;
   107    const proto::Anchor* anchor_for_ticket(const std::string&) const override;
   108  
   109   private:
   110    const proto::DocumentationReply& document_;
   111  };
   112  
   113  /// \brief Render `printable` as HTML according to `options`.
   114  std::string RenderHtml(const HtmlRendererOptions& options,
   115                         const Printable& printable);
   116  
   117  /// \brief Render `document` as HTML according to `options`, using `handlers` to
   118  /// process markup.
   119  std::string RenderDocument(const HtmlRendererOptions& options,
   120                             const std::vector<MarkupHandler>& handlers,
   121                             const proto::DocumentationReply::Document& document);
   122  
   123  /// \brief Extract and render the simple identifiers for parameters in `sig`.
   124  std::vector<std::string> RenderSimpleParams(
   125      const proto::common::MarkedSource& sig);
   126  
   127  /// \brief Extract and render the simple identifier for `sig`.
   128  /// \return The empty string if there is no such identifier.
   129  std::string RenderSimpleIdentifier(const proto::common::MarkedSource& sig);
   130  
   131  /// \brief Extract and render the simple qualified name for `sig`.
   132  /// \param include_identifier if set, include the identifier on the qualified
   133  /// name.
   134  /// \return The empty string if there is no such identifier.
   135  std::string RenderSimpleQualifiedName(const proto::common::MarkedSource& sig,
   136                                        bool include_identifier);
   137  
   138  /// \brief Extract and render a plaintext initializer for `sig`.
   139  /// \return The empty string if there is no such initializer.
   140  std::string RenderInitializer(const proto::common::MarkedSource& sig);
   141  
   142  /// \brief Render `sig` as a full signature.
   143  /// \param base_ticket if set and `linkify` is true, use this ticket to try
   144  /// and generate links over the bare `IDENTIFIER` nodes in `sig`.
   145  std::string RenderSignature(const HtmlRendererOptions& options,
   146                              const proto::common::MarkedSource& sig,
   147                              bool linkify, const std::string& base_ticket);
   148  
   149  }  // namespace kythe
   150  
   151  #endif