From: Alexandre Terrasa Date: Tue, 3 Feb 2015 21:38:27 +0000 (+0100) Subject: contrib/nitrpg: introduce templates to render the game as an HTML website X-Git-Tag: v0.7.2~15^2~14 X-Git-Url: http://nitlanguage.org contrib/nitrpg: introduce templates to render the game as an HTML website Signed-off-by: Alexandre Terrasa --- diff --git a/contrib/nitrpg/src/templates/panels.nit b/contrib/nitrpg/src/templates/panels.nit new file mode 100644 index 0000000..03f3ea2 --- /dev/null +++ b/contrib/nitrpg/src/templates/panels.nit @@ -0,0 +1,305 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2014-2015 Alexandre Terrasa +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Panels templates for `nitpg`. +module panels + +import templates_base + +# A panel can be displayed in a html page. +# +# This display a Bootstrap panel. +class Panel + super Template + + redef fun rendering do + add """
+
+

""" + render_title + add """

+
+
""" + render_body + add """
+
""" + end + + # Render the panel title. + # Betweem `

` tags. + fun render_title do end + + # Render the panel body. + fun render_body do end +end + +# A panel that contain only a table as body. +class TablePanel + super Panel + + redef fun rendering do + add """
+
+

""" + render_title + add """ +

+
""" + render_body + add """
""" + end +end + +# Display an error message within a panel. +class ErrorPanel + super Panel + + redef fun rendering do + add """ +
+
+

""" + render_title + add """ +

+
+
""" + render_body + add """ +
+
+""" + end + + # The error message to display as panel body. + var msg: String + + redef fun render_title do + add "  " + add "Error" + end + + redef fun render_body do + add msg.html_escape + end + +end + +# A panel that display repo statistics. +class GameStatusPanel + super Panel + + # Repo to display. + var game: Game + + redef fun render_title do + add "  " + add "{game.name}" + end + + redef fun render_body do + add "{game.load_players.length}" + add " players
" + add "{game.stats["pulls"]} pull requests" + add " ({game.stats["pulls_open"]} open)
" + add "{game.stats["issues"]} issues" + add " ({game.stats["issues_open"]} open)
" + end +end + +# Player status panel. +class PlayerStatusPanel + super Panel + + # Game instance. + var game: Game + + # Target player. + var player: Player + + redef fun render_title do + add "" + add " \"{player.name}\"" + add "  " + add "{player.name}" + end + + redef fun render_body do + var ranking = game.player_ranking + # TODO player.rank + add "

ranked " + add " # {ranking[player.name]}

" + add "{player.nitcoins} nitcoins
" + end +end + +# A panel that display a list of player in a repo. +class ShortListPlayersPanel + super Panel + + # Game instance. + var game: Game + + redef fun render_title do + add "  " + add "Players" + end + + redef fun render_body do + var players = game.load_players.values.to_a + if players.is_empty then + add "No player yet..." + return + end + (new PlayerCoinComparator).sort(players) + for player in players do + add "" + add player.name + add " ({player.nitcoins})
" + end + end +end + +# A panel that display a list of player in a repo. +class ListPlayersPanel + super TablePanel + + # Game instance. + var game: Game + + redef fun render_title do + add "  " + add "Players" + end + + redef fun render_body do + var players = game.load_players.values.to_a + (new PlayerCoinComparator).sort(players) + if players.is_empty then + add "
" + add "No player yet..." + add "
" + return + end + add """ + + + + + """ + var rank = 1 + for player in players do + add "" + add " " + add " " + add " " + add "" + rank += 1 + end + add "
#PlayerNitcoins
{rank}{player.name}{player.nitcoins}
" + end +end + +# A panel that display the podium. +class PodiumPanel + super Panel + + # Game instance. + var game: Game + + redef fun render_title do + add "  Hall of fame" + end + + redef fun render_body do + var players = game.load_players.values.to_a + (new PlayerCoinComparator).sort(players) + if players.is_empty then + add "No players yet..." + return + end + add """ +
+
""" + var max = players.first.nitcoins + var orders = [3, 1, 0, 2, 4] + for order in orders do + if order >= players.length then continue + var player = players[order] + var size = 0 + if max > 0 then size = player.nitcoins * 300 / max + add """ +
+

+ + {{{player.name}}} + +

+

{{{player.name}}}

+

{{{player.nitcoins}}}

+
+
""" + end + add """ +
+
""" + end +end + +# A `Panel` that displays the list of PR to review for a `Player`. +class PlayerReviewsPanel + super Panel + + # Repo to display. + var game: Game + + # Player to display customized list for. + var player: Player + + redef fun render_title do + add "  " + add "Review pull requests to gain nitcoins!" + end + + redef fun render_body do + var q = "is:open label:need_review sort:updated-asc " + + "-involves:{player.name}" + + var issues = game.repo.search_issues(q) + if issues.is_empty then + add "No pull request to review yet..." + return + end + for issue in issues do + add """""" + end + end +end diff --git a/contrib/nitrpg/src/templates/templates.nit b/contrib/nitrpg/src/templates/templates.nit new file mode 100644 index 0000000..f1affb9 --- /dev/null +++ b/contrib/nitrpg/src/templates/templates.nit @@ -0,0 +1,110 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2014-2015 Alexandre Terrasa +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Templates that compose the `nitrpg` site. +module templates + +import panels + +# A page in the nitrp site. +class NitRpgPage + super Template + + # URL used as prefix for all the links generated in this page. + var root_url: String + + # Breadcrumbs to this page if any. + var breadcrumbs: nullable Breadcrumbs = null is public writable + + # Panels to display in the sidebar. + var side_panels = new Array[Panel] + + # Panels to display in the page main container. + var flow_panels = new Array[Panel] + + redef fun rendering do + render_header + render_footer + end + + # Render the header shared by all pages. + fun render_header do + add """ + + + + + Github RPG + + + + + +
+
""" + if not side_panels.is_empty then + add """
""" + for panel in side_panels do add panel + add """
+
""" + else + add """
""" + end + for panel in flow_panels do add panel + add """
+
+
+""" + end + + # Render the footer shared by all pages. + fun render_footer do + add """ + + + + +""" + end +end + +# A Bootstrap breadcrumbs component. +class Breadcrumbs + super Template + + # Items to display in this breadcrumb. + var entries = new Array[String] + + redef fun rendering do + add "
    " + for entry in entries do + add "
  1. {entry}
  2. " + end + add "
" + end + + # Add a link to the breadcrumbs. + fun add_link(href, name: String) do + entries.add "{name}" + end +end diff --git a/contrib/nitrpg/src/templates/templates_base.nit b/contrib/nitrpg/src/templates/templates_base.nit new file mode 100644 index 0000000..adac329 --- /dev/null +++ b/contrib/nitrpg/src/templates/templates_base.nit @@ -0,0 +1,39 @@ +# This file is part of NIT ( http://www.nitlanguage.org ). +# +# Copyright 2014-2015 Alexandre Terrasa +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# Base HTML rendering templates for `nitpg`. +module templates_base + +import statistics + +redef class GameEntity + + # URL to this game entity page. + fun url: String do return game.url / key +end + +redef class Game + + # Root URL ise used as a prefix for `url`. + # + # This must be set before any access to `url`. + var root_url: String is noinit, writable + + redef fun url do return "{root_url}/games" / key + + # Displayed name. + fun name: String do return repo.full_name +end