-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor HTML generation into shared base handler. #182
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
Handlers/RealEstatesWatcher.AdPostsHandlers.Base.Html/CommonHtmlTemplateElements.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| namespace RealEstatesWatcher.AdPostsHandlers.Base.Html; | ||
|
|
||
| public static class CommonHtmlTemplateElements | ||
| { | ||
| public const string FullPage = """ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8"> | ||
| <title>Real Estate Advertisements</title> | ||
| </head> | ||
| <body style="max-width: 800px; margin:10px auto;"> | ||
| <maintitle/> | ||
| <posts/> | ||
| </body> | ||
| </html> | ||
| """; | ||
|
|
||
| public const string TitleNewPosts = """ | ||
| <h1>🏦 <span style="color: #4f4f4f; font-style: italic;">NEW Real estate offer</span></h1> | ||
| """; | ||
|
|
||
| public const string TitleInitialPosts = """ | ||
| <h1>🏦 <span style="color: #4f4f4f; font-style: italic;"> Current Real estate offer</span></h1> | ||
| """; | ||
|
|
||
| public const string Post = """ | ||
| <div style="padding: 10px; background: #ededed; min-height: 200px;"> | ||
| <div style="float: left; margin-right: 1em; width: 30%; height: 180px; display: {$img-display};"> | ||
| <img src="{$img-link}" style="height: 100%; width: 100%; object-fit: cover;" alt="Ad main visual presentation"/> | ||
| </div> | ||
| <a href="{$post-link}"> | ||
| <h3 style="margin: 0.2em; margin-top: 0;">{$title}</h3> | ||
| </a> | ||
| <span style="font-size: medium; color: #4f4f4f; display: {$price-display};"> | ||
| <strong>{$price}</strong> {$currency} | ||
| <span style="display: {$additional-fees-display};"> + {$additional-fees} {$currency}</span><br/> | ||
| </span> | ||
| <span style="font-size: medium; color: #4f4f4f; display: {$price-comment-display};"> | ||
| <strong>{$price-comment}</strong><br/> | ||
| </span> | ||
| <span> | ||
| <strong>Server:</strong> {$portal-name}<br/> | ||
| <strong>Adresa:</strong> {$address} | ||
| <span style="display: {$address-links-display};"> | ||
| <a target="_blank" rel="noopener noreferrer" href="https://www.google.com/maps/search/?api=1&query={$address-encoded}"><img alt="Google's map logo" title="Otvoriť v Google Mapách" src="https://upload.wikimedia.org/wikipedia/commons/thumb/a/aa/Google_Maps_icon_%282020%29.svg/32px-Google_Maps_icon_%282020%29.svg.png?20200218211225" style="margin-left: 6px; max-height: 14px; width: auto" /></a> | ||
| <a target="_blank" rel="noopener noreferrer" href="https://mapy.com/fnc/v1/search?query={$address-encoded}"><img alt="Icon of Mapy.com" title="Otvoriť na Mapy.com" src="https://mapy.com/img/favicon/common/plain/favicon-32x32.png" style="max-height: 14px; width: auto"></a> | ||
| <a target="_blank" rel="noopener noreferrer" href="https://maps.apple.com/?q={$address-encoded}"><img alt="Icon of Apple Maps" title="Otvoriť v Apple Mapách" src="https://maps.apple.com/static/maps-app-web-client/images/maps-app-icon-120x120.png" style="max-height: 14px; width: auto"></a> | ||
| </span><br/> | ||
| <strong>Výmera:</strong> {$floor-area}<br/> | ||
| <strong>Dispozícia:</strong> {$layout}<br/> | ||
| </span> | ||
| <p style="margin: 0.2em; font-size: small; text-align: justify; display: {$text-display};">{$text}</p> | ||
| </div> | ||
| """; | ||
| } |
89 changes: 89 additions & 0 deletions
89
Handlers/RealEstatesWatcher.AdPostsHandlers.Base.Html/HtmlBasedAdPostsHandlerBase.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| using RealEstatesWatcher.Models; | ||
|
|
||
| using System.Globalization; | ||
| using System.Text; | ||
| using System.Web; | ||
|
|
||
| namespace RealEstatesWatcher.AdPostsHandlers.Base.Html; | ||
|
|
||
| public abstract class HtmlBasedAdPostsHandlerBase(NumberFormatInfo numberFormat) | ||
| { | ||
| protected virtual string CreateHtmlPostElement(RealEstateAdPost post) | ||
| { | ||
| var postHtmlBuilder = new StringBuilder(CommonHtmlTemplateElements.Post) | ||
| .Replace("{$title}", post.Title) | ||
| .Replace("{$portal-name}", post.AdsPortalName) | ||
| .Replace("{$post-link}", post.WebUrl.AbsoluteUri) | ||
| .Replace("{$address}", post.Address); | ||
|
|
||
| // address links | ||
| if (!string.IsNullOrEmpty(post.Address)) | ||
| { | ||
| postHtmlBuilder.Replace("{$address-links-display}", "inline-block") | ||
| .Replace("{$address-encoded}", HttpUtility.UrlEncode(post.Address)); | ||
| } | ||
| else | ||
| { | ||
| postHtmlBuilder.Replace("{$address-links-display}", "none"); | ||
| } | ||
|
|
||
| // layout | ||
| postHtmlBuilder.Replace("{$layout}", post.Layout is not Layout.NotSpecified ? post.Layout.ToDisplayString() : "-"); | ||
|
|
||
| // floor area | ||
| postHtmlBuilder.Replace("{$floor-area}", post.FloorArea is not null and not decimal.Zero ? post.FloorArea + " m²" : "-"); | ||
|
|
||
| // image | ||
| if (post.ImageUrl is not null) | ||
| { | ||
| postHtmlBuilder.Replace("{$img-link}", post.ImageUrl.AbsoluteUri) | ||
| .Replace("{$img-display}", "block"); | ||
| } | ||
| else | ||
| { | ||
| postHtmlBuilder.Replace("{$img-link}", string.Empty) | ||
| .Replace("{$img-display}", "none"); | ||
| } | ||
|
|
||
| // price | ||
| if (post.Price is not decimal.Zero) | ||
| { | ||
| postHtmlBuilder.Replace("{$price}", post.Price.ToString("N", numberFormat)) | ||
| .Replace("{$currency}", post.Currency.ToString()) | ||
| .Replace("{$price-display}", "block") | ||
| .Replace("{$price-comment-display}", "none") | ||
| .Replace("{$price-comment}", string.Empty); | ||
| } | ||
| else | ||
| { | ||
| postHtmlBuilder.Replace("{$price-comment}", post.PriceComment ?? "-") | ||
| .Replace("{$price-comment-display}", "block") | ||
| .Replace("{$price-display}", "none") | ||
| .Replace("{$price}", string.Empty) | ||
| .Replace("{$currency}", string.Empty); | ||
| } | ||
|
|
||
| // additional fees | ||
| if (post.AdditionalFees is not null and not decimal.Zero) | ||
| { | ||
| postHtmlBuilder.Replace("{$additional-fees}", post.AdditionalFees.Value.ToString("N", numberFormat)) | ||
| .Replace("{$additional-fees-display}", "inline-block"); | ||
| } | ||
| else | ||
| { | ||
| postHtmlBuilder.Replace("{$additional-fees}", decimal.Zero.ToString("N")) | ||
| .Replace("{$additional-fees-display}", "none"); | ||
| } | ||
|
|
||
| // text | ||
| postHtmlBuilder.Replace("{$text}", post.Text) | ||
| .Replace("{$text-display}", !string.IsNullOrEmpty(post.Text) ? "table" : "none"); | ||
supermartzin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| return postHtmlBuilder.ToString(); | ||
| } | ||
|
|
||
| protected virtual string CreateFullHtmlPage(IEnumerable<RealEstateAdPost> posts, string titleHtmlElement) => | ||
| CommonHtmlTemplateElements.FullPage | ||
| .Replace("<maintitle/>", titleHtmlElement) | ||
| .Replace("<posts/>", string.Join(Environment.NewLine, posts.Select(CreateHtmlPostElement))); | ||
| } | ||
13 changes: 13 additions & 0 deletions
13
...atesWatcher.AdPostsHandlers.Base.Html/RealEstatesWatcher.AdPostsHandlers.Base.Html.csproj
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net10.0</TargetFramework> | ||
supermartzin marked this conversation as resolved.
Show resolved
Hide resolved
supermartzin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <ProjectReference Include="..\..\RealEstatesWatcher.Models\RealEstatesWatcher.Models.csproj" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.