Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CSS to Railroad output #480

Merged
merged 2 commits into from Apr 19, 2023
Merged

Add CSS to Railroad output #480

merged 2 commits into from Apr 19, 2023

Conversation

schnorea
Copy link
Contributor

@schnorea schnorea commented Apr 18, 2023

The head kwargs wasn't getting passed on the HTML file. Also the railroad diagram.diagram.writeStandalone function adds the default styling to each instance of the diagram this needed to be turned off so that any style sheet include via the head arg has an effect. Fund this while testing.

Sample to test PR

from pyparsing import Word, alphas, restOfLine
# Creates a simple diagram with a blue body and
# various other railroad features colored with
# a complete disregard for taste

# Very simple grammer for demo purposes
salutation = Word(alphas).setResultsName("salutation")
subject = restOfLine.setResultsName("subject")

parse_grammer = salutation + subject

# This is used to turn off the railroads
# definition of DEFAULT_STYLE.
# If this is set to 'None' the default style
# will be written as part of each diagram
# and you will not be able to set the 
# css style globally and the string 'expStyle'
# will have no effect.
# There is probably a PR to railroad_diagram to
# remove some cruft left in the SVG.
DEFAULT_STYLE = ""

# CSS Code to be placed into head of the html file
expStyle = """
<style type="text/css">

body {
    background-color: blue;
}

.railroad-heading {
    font-family: monospace;
    color: bisque;
}

svg.railroad-diagram {
    background-color: hsl(264,45%,85%);
}
svg.railroad-diagram path {
    stroke-width: 3;
    stroke: green;
    fill: rgba(0,0,0,0);
}
svg.railroad-diagram text {
    font: bold 14px monospace;
    text-anchor: middle;
    white-space: pre;
}
svg.railroad-diagram text.diagram-text {
    font-size: 12px;
}
svg.railroad-diagram text.diagram-arrow {
    font-size: 16px;
}
svg.railroad-diagram text.label {
    text-anchor: start;
}
svg.railroad-diagram text.comment {
    font: italic 12px monospace;
}
svg.railroad-diagram g.non-terminal text {
    /*font-style: italic;*/
}
svg.railroad-diagram rect {
    stroke-width: 3;
    stroke: black;
    fill: hsl(55, 72%, 69%);
}
svg.railroad-diagram rect.group-box {
    stroke: rgb(33, 8, 225);
    stroke-dasharray: 10 5;
    fill: none;
}
svg.railroad-diagram path.diagram-text {
    stroke-width: 3;
    stroke: black;
    fill: white;
    cursor: help;
}
svg.railroad-diagram g.diagram-text:hover path.diagram-text {
    fill: #eee;
}
</style>
"""

# the 'css=DEFAULT_STYLE' or 'css=""' is needed to turn off railroad_diagrams styling
parse_grammer.create_diagram("railroad_diagram_demo.html", vertical=6, show_results_names=True, css=DEFAULT_STYLE, head=expStyle)

@ptmcg ptmcg merged commit bf5ef48 into pyparsing:master Apr 19, 2023
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants