# Permet de construire des durées avec une syntaxe comme :
#   7Days
using Dates
const Years = Year(1)
const Days  = Day(1)



# Affichage des DataFrames dans la sortie HTML
using DataFrames
using Printf
function info(df::DataFrame)
    try
        WEAVE_ARGS
    catch
        display(df)
        return
    end

    pretty(x) = x
    pretty(x::Float64) = @sprintf "%.6f" x
    pretty(::Type{Union{T,Missing}}) where {T} = "$T?"

    function printrow(i)
        print("<tr>")
        print("<th>$i</th>")
        for j in 1:ncol(df)
            print("<td>$(pretty(df[i,j]))</td>")
        end
        print("</tr>")
    end

    function printrows()
        i = 1
        while i<=nrow(df) && i<=3
            printrow(i)
            i += 1
        end

        i>nrow(df) && return

        if i < nrow(df)-2
            print("<tr><td>...</td></tr>")
        end

        i = max(i, nrow(df)-2)
        while i<=nrow(df)
            printrow(i)
            i += 1
        end
    end

    println("$(nrow(df)) rows × $(ncol(df)) columns")
    print("<table>")
    print("<thead>")
    print("<tr><th></th>")
    for col in names(df)
        print("<th>$col</th>")
    end
    print("</tr>")
    print("<tr><th></th>")
    for col in names(df)
        print("<th>$(pretty(eltype(df[!,col])))</th>")
    end
    print("</tr>")
    print("</thead>")
    print("<tbody>")
    printrows()
    print("</tbody>")
    print("</table>\n")
end



# Meilleure intégration entre Plotly et Weave
function disp()
    try
        WEAVE_ARGS
    catch
        display(plot!())
        return
    end

    buf = IOBuffer()
    show(buf, MIME("text/html"), plot!())
    seekstart(buf)
    str = String(read(buf))
    m = match(r"<body>(.*)</body>"s, str)

    return if m === nothing
        str
    else
        m.captures[1]
    end |> print
end
