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

Adding Import CSS + Include in views #9

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 56 additions & 1 deletion src/main/java/lajavel/View.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ public static String make(String viewName, Map.Entry<String, Object>... entries)
return viewContent;
}

viewContent = importCss(viewContent);

Matcher m = Pattern.compile("\\{\\{([^{{}}]*)\\}\\}").matcher(viewContent);

// Creating the target string buffer
Expand Down Expand Up @@ -81,10 +83,63 @@ public static String getValueOf(String propertyName, Object object) {
}
}

public static String importCss(String viewContent) {
// get CSS by import
final String cssRegex = "\\{% import (\\S*) %\\}"; // Dans le html on a {% import nomdufichiercss %}

// Ici on effectue le include avant de s'attaquer au CSS (returnIncluded)
final Matcher cssMatcher = Pattern.compile(cssRegex, Pattern.DOTALL).matcher(returnIncluded(viewContent));
StringBuffer sbImport = new StringBuffer();

while (cssMatcher.find()) {
String css_url = "<link rel='stylesheet' type='text/css' href='/css/" + cssMatcher.group(1) + ".css'>";
cssMatcher.appendReplacement(sbImport, css_url);
}
cssMatcher.appendTail(sbImport);

viewContent = sbImport.toString();

return viewContent;
}

public static String returnIncluded(String viewContent) {
StringBuffer sbImport = new StringBuffer();

final String includeRegex = "\\{% include (\\S*) %\\}";

final Matcher includeMatcher = Pattern.compile(includeRegex, Pattern.DOTALL).matcher(viewContent);

while (includeMatcher.find()) {
final String blockRegex = "\\{% block (\\S*) %\\}(.*)\\{% endblock %\\}";
final Matcher blockMatcher = Pattern.compile(blockRegex, Pattern.DOTALL).matcher(View.getViewContent(includeMatcher.group(1)));
while (blockMatcher.find()){
includeMatcher.appendReplacement(sbImport, blockMatcher.group(2));
}
}
includeMatcher.appendTail(sbImport);

viewContent = sbImport.toString();

return viewContent;
}

public static String getViewContent(String viewName) {
// Dans le HTML on a {% include Component:nomdufichierhtml %}
String[] viewNameCategorized = viewName.split("\\:");

String viewNameCategory = viewNameCategorized[0];

if (viewNameCategorized.length > 1) {
viewName = viewNameCategorized[1];
}

if (viewNameCategory.equals("Component")) {
viewNameCategory = "components/";
} else {
viewNameCategory = "views/";
}
// Get the file url, not working in JAR file.
URL resource = View.class.getClassLoader().getResource("views/" + viewName + ".view");
URL resource = View.class.getClassLoader().getResource(viewNameCategory + viewName + ".view");

if (resource == null) {
throw new IllegalArgumentException("file not found!");
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/components/header.view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% block header %}
{% import header %}
<header>
<h1>Welcome In Lajavel</h1>
</header>
{% endblock %}
6 changes: 6 additions & 0 deletions src/main/resources/public/css/header.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
header h1{
text-align: center;
font-size: 1.5rem;
color: red;
text-decoration: underline;
}
2 changes: 2 additions & 0 deletions src/main/resources/views/index.view
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<link href="/css/index.css" rel="stylesheet">
</head>

{% include Component:header %}

<center><br />

Bonjour,
Expand Down