markdown/internal/CodeSpans.protos

Repository
guillermomolina/protos
Commit
2723a55555e7beecee0816bf99d16e3c8c1ae635
Tree
c36054e4e5abd7fc29a039b4e096cac2e8706492
Blob
d1c09694a9477e1b8ebf732d402bd3036d49e5c7
Source
View exact source on GitHub
// THE LICENSED WORK IS PROVIDED UNDER THE TERMS OF THE ADAPTIVE PUBLIC LICENSE
// ("LICENSE") AS FIRST COMPLETED BY: Guillermo Adrián Molina. ANY USE, PUBLIC
// DISPLAY, PUBLIC PERFORMANCE, REPRODUCTION OR DISTRIBUTION OF, OR PREPARATION OF
// DERIVATIVE WORKS BASED ON, THE LICENSED WORK CONSTITUTES RECIPIENT'S ACCEPTANCE
// OF THIS LICENSE AND ITS TERMS, WHETHER OR NOT SUCH RECIPIENT READS THE TERMS OF
// THE LICENSE. "LICENSED WORK" AND "RECIPIENT" ARE DEFINED IN THE LICENSE. A COPY
// OF THE LICENSE IS LOCATED IN THE TEXT FILE ENTITLED "LICENSE.TXT" ACCOMPANYING
// THE CONTENTS OF THIS FILE. IF A COPY OF THE LICENSE DOES NOT ACCOMPANY THIS
// FILE, A COPY OF THE LICENSE MAY ALSO BE OBTAINED AT THE FOLLOWING WEB SITE:
// https://github.com/guillermomolina/protos
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
// the specific language governing rights and limitations under the License.

//! Physical maximal backtick runs indexed by length and source position.
Source: import("./Source.protos")
Builders: import("./Builders.protos")

index: (s) => {
    grouped: Map()
    length: Source.size(s)
    i: Source.find(s, 0, length, 96)
    while(i >= 0) {
        start: i
        while(Source.at(s, i) == 96) { i = i + 1 }
        width: i - start
        if(grouped.containsKey(width) === false) { grouped[width] = Builders.array() }
        Builders.push(grouped[width], start)
        i = Source.find(s, i, length, 96)
    }
    positions: Map()
    grouped.each((width, builder) => { positions[width] = Builders.finish(builder) })
    { positions: positions }
}

space: (c) => { (c == 32) || (c == 10) || (c == 13) }

// The opening run extends from start to the end of its physical run; start
// may follow an escaped backtick, so the effective width can be a suffix.
// Only a whole physical run of exactly that width closes. Without a closer
// the value is null and end is the end of the opening run.
recognize: (s, start, runs) => {
    begin: start
    while(Source.at(s, begin) == 96) { begin = begin + 1 }
    width: begin - start
    none: { value: null; end: begin }
    if(runs.positions.containsKey(width) === false) { ^none }
    candidates: runs.positions[width]
    low: 0
    high: candidates.size()
    while(low < high) {
        mid: (low + high).div(2)
        if(candidates[mid] < begin) { low = mid + 1 }
        (candidates[mid] < begin).ifFalse(() => { high = mid })
    }
    if(low == candidates.size()) { ^none }
    end: candidates[low]
    finish: end
    if(space(Source.at(s, begin)) && space(Source.at(s, end - 1))) {
        allSpaces: true
        i: begin
        while((i < end) && allSpaces) {
            allSpaces = space(Source.at(s, i))
            i = i + 1
        }
        if(allSpaces === false) { begin = begin + 1; finish = finish - 1 }
    }
    output: Builders.text()
    copied: begin
    i: begin
    while(i < finish) {
        c: Source.at(s, i)
        if((c == 10) || (c == 13)) {
            Source.appendRange(output, s, copied, i)
            Builders.scalar(output, 32)
            copied = i + 1
        }
        i = i + 1
    }
    Source.appendRange(output, s, copied, finish)
    { value: Builders.string(output); end: end + width }
}