HiveBrain v1.2.0
Get Started
← Back to all entries
patternhtmlMinor

Slurp JSON trading cards to valid HTML

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
slurpcardstradingvalidjsonhtml

Problem

For Cardshifter TCG we pretty frequently update our list of available cards, so I tried to think of a way to make it easy to transform an exported JSON file containing all the cards into a decent HTML file that I can update on our website.

Each card's format looks like this:

{
"MAX_HEALTH" : 10,
"SICKNESS" : 1,
"MANA_COST" : 30,
"ATTACK" : 3,
"HEALTH" : 10,
"ATTACK_AVAILABLE" : 1,
"flavor" : "The Great Grandfather, Emperor of all Deities, Vanquisher of Evil.",
"name" : "JADE EMPEROR",
"description" : "Give creatures of type Chinese owned by you on Battlefield 3 HEALTH\nGive creatures on Battlefield -1 ATTACK\n",
"type" : "Chinese God"
}

Notice the \n character in the description field which separates multiple statements which are part of the same description.

I wrote a Groovy script to take this data and format it into HTML. I'm heavily using Groovy's GString interpolated expressions to assemble the data and HTML together. I'm using a little bit of RegEx & partial string matching so that card types like "Chinese God", "Chinese Hero" and "Chinese" all get included in one set, for example.

Bear in mind, we have not yet decided on and made styles for the tags and such, so you'll see some inline CSS; that is temporary.

```
import groovy.json.JsonSlurper

def filePath = "/Users/francisveilleux-gaboury/Downloads/"
def inputFileName = "game-1.json"
def outputFileName = "mythos-cards.html"

def inputFile = new File("$filePath$inputFileName")
def outputFile = new File("$filePath$outputFileName")

if (!inputFile.exists()) {
throw new RuntimeException("File $inputFile does not exist.")
}

if (outputFile.exists()) {
println "Overwriting existing file ${filePath}${outputFileName}"
} else {
println "Creating file: ${filePath}${outputFileName}"
}

def nl = System.getProperty("line.separator")

outputFile.write """

Mythos card list


Mythos card list
"""

// HTML sections formatting
def cardTitle = ''
def endCardTitle = ""
def car

Solution

To avoid the repetition in generating the html for each mythology, create a function that takes a title and a pattern and move inside the code that is currently duplicated for Chinese and Greek, something like this:

def appendMythos(title, pattern) {
    // the currently duplicated code
}


That way, you can generate the texts as simply as:

appendMythos("Greek mythology", /(?i)^Greek/)
appendMythos("Chinese mythology", /(?i)^Chinese/)


You don't need to declare variables line this:

def pattern
def matcher


It's better to declare them where you assign to them.

The way you match patterns is a bit tedious. You can simplify like this:

def pattern = /(?i)^Greek/
if (it."type" =~ pattern) {


Because in this expression the =~ operator will behave as a predicate, call find on the returned Matcher and coerce into a boolean.

Code Snippets

def appendMythos(title, pattern) {
    // the currently duplicated code
}
appendMythos("Greek mythology", /(?i)^Greek/)
appendMythos("Chinese mythology", /(?i)^Chinese/)
def pattern
def matcher
def pattern = /(?i)^Greek/
if (it."type" =~ pattern) {

Context

StackExchange Code Review Q#96740, answer score: 4

Revisions (0)

No revisions yet.