• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37604

26 Aug 2023 05:06AM UTC coverage: 86.533% (+0.1%) from 86.402%
#37604

push

local

web-flow
allow `@overlay` for methods with return type declaration (#51054)

12 of 12 new or added lines in 1 file covered. (100.0%)

73399 of 84822 relevant lines covered (86.53%)

12701637.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

53.04
/stdlib/Markdown/src/GitHub/table.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
mutable struct Table
4
    rows::Vector{Vector{Any}}
52✔
5
    align::Vector{Symbol}
6
end
7

8
function parserow(stream::IO)
×
9
    withstream(stream) do
16,228✔
10
        line = readline(stream)
16,228✔
11
        row = split(line, r"(?<!\\)\|")
16,228✔
12
        length(row) == 1 && return
16,228✔
13
        isempty(row[1]) && popfirst!(row)
395✔
14
        map!(x -> strip(replace(x, "\\|" => "|")), row, row)
2,570✔
15
        isempty(row[end]) && pop!(row)
395✔
16
        return row
395✔
17
    end
18
end
19

20
function rowlength!(row, len)
354✔
21
    while length(row) < len push!(row, "") end
354✔
22
    while length(row) > len pop!(row) end
354✔
23
    return row
354✔
24
end
25

26
const default_align = :r
27

28
function parsealign(row)
41✔
29
    align = Symbol[]
41✔
30
    for s in row
41✔
31
        (length(s) ≥ 3 && s ⊆ Set("-:")) || return
93✔
32
        push!(align,
93✔
33
              s[1] == ':' ? (s[end] == ':' ? :c : :l) :
34
              s[end] == ':' ? :r :
35
              default_align)
36
    end
134✔
37
    return align
41✔
38
end
39

40
function github_table(stream::IO, md::MD)
15,833✔
41
    withstream(stream) do
15,833✔
42
        skipblank(stream)
15,833✔
43
        rows = []
15,833✔
44
        cols = 0
×
45
        align = nothing
×
46
        while (row = parserow(stream)) !== nothing
16,228✔
47
            if length(rows) == 0
395✔
48
                cols = length(row)
87✔
49
            end
50
            if align === nothing && length(rows) == 1 # Must have a --- row
395✔
51
                align = parsealign(row)
41✔
52
                (align === nothing || length(align) != cols) && return false
41✔
53
            else
54
                push!(rows, map(x -> parseinline(x, md), rowlength!(row, cols)))
1,197✔
55
            end
56
        end
395✔
57
        length(rows) <= 1 && return false
15,833✔
58
        push!(md, Table(rows, align))
82✔
59
        return true
41✔
60
    end
61
end
62

63
function html(io::IO, md::Table)
×
64
    withtag(io, :table) do
×
65
        for (i, row) in enumerate(md.rows)
×
66
            withtag(io, :tr) do
×
67
                for (j, c) in enumerate(md.rows[i])
×
68
                    alignment = md.align[j]
×
69
                    alignment = alignment === :l ? "left" : alignment === :r ? "right" : "center"
×
70
                     withtag(io, i == 1 ? :th : :td, ("align", alignment)) do
×
71
                        htmlinline(io, c)
×
72
                    end
73
                end
×
74
            end
75
        end
×
76
    end
77
end
78

79
mapmap(f, xss) = map(xs->map(f, xs), xss)
130✔
80

81
colwidths(rows; len = length, min = 0) =
16✔
82
    reduce((x,y) -> max.(x,y), [min; convert(Vector{Vector{Int}}, mapmap(len, rows))])
57✔
83

84
padding(width, twidth, a) =
171✔
85
    a === :l ? (0, twidth - width) :
86
    a === :r ? (twidth - width, 0) :
87
    a === :c ? (floor(Int, (twidth-width)/2), ceil(Int, (twidth-width)/2)) :
88
    error("Invalid alignment $a")
89

90
function padcells!(rows, align; len = length, min = 0)
16✔
91
    widths = colwidths(rows, len = len, min = min)
8✔
92
    for i = 1:length(rows), j = axes(rows[1],1)
65✔
93
        cell = rows[i][j]
171✔
94
        lpad, rpad = padding(len(cell), widths[j], align[j])
171✔
95
        rows[i][j] = " "^lpad * cell * " "^rpad
171✔
96
    end
220✔
97
    return rows
8✔
98
end
99

100
_dash(width, align) =
24✔
101
    align === :l ? ":" * "-"^width * " " :
102
    align === :r ? " " * "-"^width * ":" :
103
    align === :c ? ":" * "-"^width * ":" :
104
    throw(ArgumentError("Invalid alignment $align"))
105

106
function plain(io::IO, md::Table)
8✔
107
    cells = mapmap(md.rows) do each
8✔
108
        replace(plaininline(each), "|" => "\\|")
171✔
109
    end
110
    padcells!(cells, md.align, len = length, min = 3)
8✔
111
    for i = axes(cells,1)
16✔
112
        print(io, "| ")
57✔
113
        join(io, cells[i], " | ")
57✔
114
        println(io, " |")
57✔
115
        if i == 1
57✔
116
            print(io, "|")
8✔
117
            join(io, [_dash(length(cells[i][j]), md.align[j]) for j = axes(cells[1],1)], "|")
8✔
118
            println(io, "|")
8✔
119
        end
120
    end
57✔
121
end
122

123
function rst(io::IO, md::Table)
×
124
    cells = mapmap(rstinline, md.rows)
×
125
    padcells!(cells, md.align, len = length, min = 3)
×
126
    single = ["-"^length(c) for c in cells[1]]
×
127
    double = ["="^length(c) for c in cells[1]]
×
128
    function print_row(row, row_sep, col_sep)
×
129
        print(io, col_sep, row_sep)
×
130
        join(io, row, string(row_sep, col_sep, row_sep))
×
131
        println(io, row_sep, col_sep)
×
132
    end
133
    print_row(single, '-', '+')
×
134
    for i = 1:length(cells)
×
135
        print_row(cells[i], ' ', '|')
×
136
        i ≡ 1 ? print_row(double, '=', '+') :
×
137
                print_row(single, '-', '+')
138
    end
×
139
end
140

141
function term(io::IO, md::Table, columns)
×
142
    margin_str = " "^margin
×
143
    cells = mapmap(x -> terminline_string(io, x), md.rows)
×
144
    padcells!(cells, md.align, len = ansi_length)
×
145
    for i = 1:length(cells)
×
146
        print(io, margin_str)
×
147
        join(io, cells[i], " ")
×
148
        if i == 1
×
149
            println(io)
×
150
            print(io, margin_str)
×
151
            join(io, ["–"^ansi_length(cells[i][j]) for j = 1:length(cells[1])], " ")
×
152
        end
153
        i < length(cells) && println(io)
×
154
    end
×
155
end
156

157
function latex(io::IO, md::Table)
×
158
    wrapblock(io, "tabular") do
×
159
        align = md.align
×
160
        println(io, "{$(join(align, " | "))}")
×
161
        for (i, row) in enumerate(md.rows)
×
162
            for (j, cell) in enumerate(row)
×
163
                j != 1 && print(io, " & ")
×
164
                latexinline(io, cell)
×
165
            end
×
166
            println(io, " \\\\")
×
167
            if i == 1
×
168
                println(io, "\\hline")
×
169
            end
170
        end
×
171
    end
172
end
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc