Sha256: 47d89ed14fcd23b3c136e9285d481ef6296720ecad9a418b17fb5b239328d9af

Contents?: true

Size: 1.9 KB

Versions: 5

Compression:

Stored size: 1.9 KB

Contents

#!/usr/bin/env -S awk -f
# SPDX-License-Identifier: Apache-2.0 OR MIT
# Based on https://codereview.stackexchange.com/questions/194986/print-code-fenced-sections-of-a-markdown-document/195030#195030
BEGIN {
    in_code_block = 0;
    tag = "";
    operation_id = "";
}

function slug(value) {
    head = "";
    tail = value;
    while ( match(tail,/[[:upper:]][[:lower:]]/) ) {
        tgt = substr(tail,RSTART,1);
        if ( substr(tail,RSTART-1,1) ~ /[[:lower:]]/ || RSTART > 1 ) {
            tgt = "-" tolower(tgt);
        }
        head = head substr(tail,1,RSTART-1) tgt;
        tail = substr(tail,RSTART+1);
    }
    return tolower(head) tail;
}

function camel(value) {
    gsub("_ip_", "_iP_", value);
    gsub("_id_", "_iD_", value);

    head = toupper(substr(value,0,1)) substr(value,2);
    while ( match(head, /_([a-z])/) ) {
        head = substr(head,0,RSTART-1) toupper(substr(head,RSTART+1, 1)) substr(head,RSTART+2)
    }
    # NOTE special cases for all caps groups which we can't handle otherwise
    gsub("Aws", "AWS", head);
    gsub("Gcp", "GCP", head);
    return head;
}

/^# DatadogAPIClient::V[0-9]*::(.+)API/ {
    tag = slug(substr($2, 23, length($2)-25));
}
/^## (.+)/ {
    operation_id = camel($2);
}
/^```ruby/ {
    if (operation_id == "") {
        # Filter out with_http_info examples
        next;
    }
    if (in_code_block == 0) {
        in_code_block = 1;
        if (out_file) {
            close(out_file);
        }
        system("mkdir -p " output "/" tag);
        out_file=output "/" tag "/" operation_id ".rbbeta";
        print out_file;
    } else {
        print "Can't parse " FILENAME > "/dev/stderr"
        exit 1
    }
    next;
}
/^```/ {
    in_code_block = 0;
    operation_id = "";
}

in_code_block {
    # Make sure that the file is newly created
    if (in_code_block == 1) {
        in_code_block = 2;
        print > out_file;
    } else {
        print >> out_file;
    }
}

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
datadog_api_client-1.3.0 extract-code-blocks.awk
datadog_api_client-1.2.0 extract-code-blocks.awk
datadog_api_client-1.1.0 extract-code-blocks.awk
datadog_api_client-1.0.0 extract-code-blocks.awk
datadog_api_client-1.0.0.beta.3 extract-code-blocks.awk