github.com/googleapis/api-linter@v1.65.2/docs/assets/js/proto-syntax.js (about)

     1  // Copyright 2019 Google LLC
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      https://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // Ex post facto improvements to protobuf syntax highlighting.
    16  // This applies special CSS classes to protobuf patterns that are overlooked
    17  // or insufficiently distinguished by the usual Pygments/Rouge syntax parsers.
    18  $.when($.ready).then(() => {
    19    // RPCs are always followed by three names (the RPC name, the request object,
    20    // and the response object) -- designate those appropriately.
    21    $('.language-proto .k:contains(rpc)').each((_, el) => {
    22      $(el)
    23        .nextAll('.n')
    24        .slice(1, 3)
    25        .addClass('nc');
    26    });
    27  
    28    // Designate message names as such when using them to delcare fields.
    29    $('.language-proto .n + .na + .o:contains(=)')
    30      .prev()
    31      .prev()
    32      .addClass('nc');
    33  
    34    // Colons in protocol buffers always come immediately after property keys.
    35    $('.language-proto .n + .o:contains(:)')
    36      .addClass('nk')
    37      .prev()
    38      .addClass('nk');
    39  
    40    // The option keyword is always followed by the annotation name.
    41    $('.language-proto .k:contains(option)').each((_, el) => {
    42      $(el)
    43        .nextAll('.n')
    44        .eq(0)
    45        .addClass('protobuf-annotation');
    46    });
    47  
    48    // Highlight correct and incorrect proto code blocks.
    49    for (let desc of ['Correct', 'Incorrect']) {
    50      let magic = `// ${desc}.\n`;
    51      $(`.language-proto .c1:first-child:contains('${magic}')`).each((_, el) => {
    52        let text = $(el).text();
    53  
    54        // Sanity check. This has to be the leading text.
    55        if (!text.startsWith(magic)) {
    56          return null;
    57        }
    58  
    59        // Hide the comment and add the CSS class to create the border.
    60        $(el)
    61          .addClass('hide-screen')
    62          .parents('.language-proto')
    63          .addClass(`api-linter-${desc.toLowerCase()}`);
    64  
    65        // If there is any other text, add it to a new comment <span> so it
    66        // is not hidden.
    67        if (text != magic) {
    68          $(el).after(
    69            $(`<span class="c1">${text.substring(magic.length)}</span>`)
    70          );
    71        }
    72      });
    73    }
    74  });