"Fossies" - the Fresh Open Source Software Archive

Member "julia-1.1.1/share/julia/stdlib/v1.1/Markdown/src/render/rst.jl" (16 May 2019, 3829 Bytes) of package /linux/misc/julia-1.1.1-linux-i686.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Julia source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 # This file is a part of Julia. License is MIT: https://julialang.org/license
    2 
    3 rst(x) = sprint(rst, x)
    4 
    5 function rst(io::IO, content::Vector)
    6     isempty(content) && return
    7     for md in content[1:end-1]
    8         rst(io, md)
    9         println(io)
   10     end
   11     rst(io, content[end])
   12 end
   13 
   14 rst(io::IO, md::MD) = rst(io, md.content)
   15 
   16 function rst(io::IO, header::Header{l}) where l
   17     s = rstinline(header.text)
   18     println(io, s)
   19     println(io, string("*=-~:.^"[l])^length(s))
   20     println(io)
   21 end
   22 
   23 function rst(io::IO, code::Code)
   24     if code.language == "jldoctest"
   25         println(io, ".. doctest::\n")
   26     elseif code.language != "rst"
   27         println(io, ".. code-block:: julia\n")
   28     end
   29     for l in lines(code.code)
   30         println(io, "    ", l)
   31     end
   32 end
   33 
   34 function rst(io::IO, p::Paragraph)
   35     rstinline(io, p.content)
   36     println(io)
   37 end
   38 
   39 function rst(io::IO, list::List)
   40     for (i, item) in enumerate(list.items)
   41         bullet = isordered(list) ? "$(i + list.ordered - 1). " : "* "
   42         print(io, bullet)
   43         lines = split(rstrip(sprint(rst, item)), '\n')
   44         for (n, line) in enumerate(lines)
   45             print(io, (n == 1 || isempty(line)) ? "" : " "^length(bullet), line)
   46             n < length(lines) && println(io)
   47         end
   48         println(io)
   49     end
   50 end
   51 
   52 function rst(io::IO, q::BlockQuote)
   53     s = sprint(rst, q.content)
   54     for line in split(rstrip(s), "\n")
   55         println(io, "    ", line)
   56     end
   57     println(io)
   58 end
   59 
   60 function rst(io::IO, f::Footnote)
   61     print(io, ".. [", f.id, "]")
   62     s = sprint(rst, f.text)
   63     lines = split(rstrip(s), "\n")
   64     # Single line footnotes are printed on the same line as their label
   65     # rather than taking up an additional line.
   66     if length(lines) == 1
   67         println(io, " ", lines[1])
   68     else
   69         println(io)
   70         for line in lines
   71             println(io, isempty(line) ? "" : "   ", rstrip(line))
   72         end
   73         println(io)
   74     end
   75 end
   76 
   77 function rst(io::IO, md::Admonition)
   78     s = sprint(rst, md.content)
   79     title = md.title == uppercasefirst(md.category) ? "" : md.title
   80     println(io, ".. ", md.category, "::", isempty(title) ? "" : " $title")
   81     for line in split(rstrip(s), "\n")
   82         println(io, isempty(line) ? "" : "   ", line)
   83     end
   84     println(io)
   85 end
   86 
   87 function rst(io::IO, md::HorizontalRule)
   88     println(io, "–" ^ 5)
   89 end
   90 
   91 function rst(io::IO, l::LaTeX)
   92     println(io, ".. math::\n")
   93     for line in lines(l.formula)
   94         println(io, "    ", line)
   95     end
   96 end
   97 
   98 rst(io::IO, md) = show(io, "text/rst", md)
   99 
  100 # Inline elements
  101 
  102 rstinline(x) = sprint(rstinline, x)
  103 
  104 function rstinline(io::IO, md...)
  105     wasCode = false
  106     for el in md
  107         wasCode && isa(el, AbstractString) && !Base.startswith(el, " ") && print(io, "\\ ")
  108         wasCode = (isa(el, Code) || isa(el, LaTeX) || isa(el, Link)) && (wasCode = true)
  109         rstinline(io, el)
  110     end
  111 end
  112 
  113 rstinline(io::IO, md::Vector) = !isempty(md) && rstinline(io, md...)
  114 
  115 # rstinline(io::IO, md::Image) = rstinline(io, ".. image:: ", md.url)
  116 
  117 function rstinline(io::IO, md::Link)
  118     if occursin(r":(func|obj|ref|exc|class|const|data):`\.*", md.url)
  119         rstinline(io, md.url)
  120     else
  121         rstinline(io, "`", md.text, " <", md.url, ">`_")
  122     end
  123 end
  124 
  125 rstinline(io::IO, f::Footnote) = print(io, "[", f.id, "]_")
  126 
  127 rstescape(s) = replace(s, "\\" => "\\\\")
  128 
  129 rstinline(io::IO, s::AbstractString) = print(io, rstescape(s))
  130 
  131 rstinline(io::IO, md::Bold) = rstinline(io, "**", md.text, "**")
  132 
  133 rstinline(io::IO, md::Italic) = rstinline(io, "*", md.text, "*")
  134 
  135 rstinline(io::IO, md::Code) = print(io, "``", md.code, "``")
  136 
  137 rstinline(io::IO, br::LineBreak) = println(io)
  138 
  139 rstinline(io::IO, l::LaTeX) = print(io, ":math:`", l.formula, "`")
  140 
  141 rstinline(io::IO, x) = show(io, MIME"text/rst"(), x)
  142 
  143 # show
  144 
  145 Base.show(io::IO, ::MIME"text/rst", md::MD) = rst(io, md)