Skip to content

Commit

Permalink
Merge pull request #12 from lancebeet/patch-2
Browse files Browse the repository at this point in the history
base show for MIME"text/html"
  • Loading branch information
rahulkp220 authored Mar 16, 2018
2 parents c13a51e + ee7e6f3 commit 0e31e0c
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/PeriodicTable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,46 @@ function Base.show(io::IO, ::MIME"text/plain", el::Element)
printpresent(io, "spectral image", el.spectral_img)
end

function printpresenthtml(io::IO, name, val, suffix="")
if ispresent(val)
println(io, "<tr><th>", name, "</th><td>", val, suffix, "</td></tr>")
end
end

function Base.show(io::IO, ::MIME"text/html", el::Element)
println(io, "<style>")
println(io, "th{text-align:right; padding:5px;}td{text-align:left; padding:5px}")
println(io, "</style>")
println(io, el.name, " (", el.symbol, "), number ", el.number, ':')
println(io, "<table>")
printpresenthtml(io, "category", el.category)
printpresenthtml(io, "atomic mass", el.atomic_mass, " u")
printpresenthtml(io, "density", el.density, " g/cm³")
printpresenthtml(io, "molar heat", el.molar_heat, " J/mol⋅K")
printpresenthtml(io, "melting point", el.melt, " K")
printpresenthtml(io, "boiling point", el.boil, " K")
printpresenthtml(io, "phase", el.phase)
printpresenthtml(io, "shells", el.shells)
printpresenthtml(io, "appearance", el.appearance)
printpresenthtml(io, "color", el.color)
printpresenthtml(io, "summary", el.summary)
printpresenthtml(io, "discovered by", el.discovered_by)
printpresenthtml(io, "named by", el.named_by)

link = string("<a href=\"", el.source, "\">", el.source, "</a>")
printpresenthtml(io, "source", link)
println(io, "</table>")

if ispresent(el.spectral_img)
width = 500
file = replace(el.spectral_img, "https://en.wikipedia.org/wiki/File:", "")
#Wikimedia hotlinking api
wm = "https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/"
imgdomain = string(wm, file, "&width=$width")
println(io, "<img src=\"", imgdomain, "\" alt=\"", file, "\">")
end
end

"""
Elements composite type
"""
Expand Down
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ F = elements[9]

@test stringmime("text/plain", elements) == "Elements(…119 elements…):\nH He \nLi Be B C N O F Ne \nNa Mg Al Si P S Cl Ar \nK Ca Sc Ti V Cr Mn Fe Co Ni Cu Zn Ga Ge As Se Br Kr \nRb Sr Y Zr Nb Mo Tc Ru Rh Pd Ag Cd In Sn Sb Te I Xe \nCs Ba Hf Ta W Re Os Ir Pt Au Hg Tl Pb Bi Po At Rn \nFr Ra Rf Db Sg Bh Hs Mt Ds Rg Cn Nh Fl Mc Lv Ts Og \nUue \n La Ce Pr Nd Pm Sm Eu Gd Tb Dy Ho Er Tm Yb Lu \n Ac Th Pa U Np Pu Am Cm Bk Cf Es Fm Md No Lr \n"
@test stringmime("text/plain", O) == "Oxygen (O), number 8:\n category: diatomic nonmetal\n atomic mass: 15.999 u\n density: 1.429 g/cm³\n melting point: 54.36 K\n boiling point: 90.188 K\n phase: Gas\n shells: [2, 6]\n summary: Oxygen is a chemical element with symbol O and atomic number 8. It is a member of the chalcogen group on the periodic table and is a highly reactive nonmetal and oxidizing agent that readily forms compounds (notably oxides) with most elements. By mass, oxygen is the third-most abundant element in the universe, after hydrogen and helium.\n discovered by: Carl Wilhelm Scheele\n named by: Antoine Lavoisier\n source: https://en.wikipedia.org/wiki/Oxygen\n spectral image: https://en.wikipedia.org/wiki/File:Oxygen_spectre.jpg\n"
@test stringmime("text/html", O) == "<style>\nth{text-align:right; padding:5px;}td{text-align:left; padding:5px}\n</style>\nOxygen (O), number 8:\n<table>\n<tr><th>category</th><td>diatomic nonmetal</td></tr>\n<tr><th>atomic mass</th><td>15.999 u</td></tr>\n<tr><th>density</th><td>1.429 g/cm³</td></tr>\n<tr><th>melting point</th><td>54.36 K</td></tr>\n<tr><th>boiling point</th><td>90.188 K</td></tr>\n<tr><th>phase</th><td>Gas</td></tr>\n<tr><th>shells</th><td>[2, 6]</td></tr>\n<tr><th>summary</th><td>Oxygen is a chemical element with symbol O and atomic number 8. It is a member of the chalcogen group on the periodic table and is a highly reactive nonmetal and oxidizing agent that readily forms compounds (notably oxides) with most elements. By mass, oxygen is the third-most abundant element in the universe, after hydrogen and helium.</td></tr>\n<tr><th>discovered by</th><td>Carl Wilhelm Scheele</td></tr>\n<tr><th>named by</th><td>Antoine Lavoisier</td></tr>\n<tr><th>source</th><td><a href=\"https://en.wikipedia.org/wiki/Oxygen\">https://en.wikipedia.org/wiki/Oxygen</a></td></tr>\n</table>\n<img src=\"https://commons.wikimedia.org/w/index.php?title=Special:Redirect/file/Oxygen_spectre.jpg&width=500\" alt=\"Oxygen_spectre.jpg\">\n"

0 comments on commit 0e31e0c

Please sign in to comment.