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

Render newlines in Project page description #189

Merged
merged 1 commit into from
Feb 11, 2024
Merged
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
5 changes: 4 additions & 1 deletion application/controllers/catalog/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public function index($slug)
//$this->data['sections'] = $this->section_model->as_array()->get_many_by(array('project_id'=>$this->data['project']->id));
$this->data['sections'] = $this->section_model->get_full_sections_info($this->data['project']->id);

$this->load->helper('description_html_render');
$this->data['project']->description = _normalize_and_deduplicate_newlines_in_html($this->data['project']->description);

//var_dump($this->data['sections']);
if (!empty($this->data['sections']))
{
Expand Down Expand Up @@ -173,4 +176,4 @@ function _language($language_id)
$this->load->model('language_model');
return $this->language_model->get($language_id)->language;
}
}
}
35 changes: 35 additions & 0 deletions application/helpers/description_html_render_helper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/**
* For a while, newlines weren't being rendered properly in the catalog HTML,
* so people put manual <br /> tags into project descriptions. Now, we want to
* render the newlines properly, so we need to try and dedupe a bit so that we
* don't have crazy amounts of whitespace.
*
* Inspired by this post: https://www.darklaunch.com/php-normalize-newlines-line-endings-crlf-cr-lf-unix-windows-mac.html
*
* $description string The description to render as HTML
*/
function _normalize_and_deduplicate_newlines_in_html($description) {
// Normalise everything to '\n' characters
$description = str_replace(
array("<br>", "<br />", "\r\n"),
"\n",
$description
);

// Replace suspiciously-long strings of newlines
$description = preg_replace(
"/\n{3,}/",
"\n\n",
$description,
);

// Turn the newlines into '<br />' tags
// return nl2br($description);
return str_replace(
"\n",
"<br />",
$description,
);
}