Posts: fix HTML body parsing #39

Open
Iam_Tj wants to merge 1 commit from Iam_Tj/BSSG:fix_html_body_parsing into main
Contributor

Existing sed expression assumes elements are alone on a line. It
fails when there are multiple elements for two reasons:

  1. The 2-address 'end' match will never happen
  2. The greedy * wildcard will match everything up to the end of the
    last element

The replacement works as follows:

  1. Any line other than the last is copied into the hold space
  2. The last line is added then hold swapped to pattern and a replace is
    done on the complete text of the input

Sed expression tested using:

$ cat <(echo "=== test1.html") /tmp/test1.html <( echo "=== test2.html") /tmp/test2.html
=== test1.html
<!DOCTYPE html>
<html>
        <head>
                <title>Test</title>
        </head>
        <body>
                <h1>Test</h1>
                <p>This is a test</p>
        </body>
</html>

=== test2.html
<!DOCTYPE html>
<html>
        <head>
                <title>Test</title>
        </head>
        <body><h1>Test</h1><p>This is a test</p></body>
</html>

$ sed  -n -e '$! { H; d; }' -e '$ { H; x; s#.*<body[^>]*>\(.*\)</body[^>]*>.*#\1#; p; }' /tmp/test1.html

                <h1>Test</h1>
                <p>This is a test</p>

$ sed  -n -e '$! { H; d; }' -e '$ { H; x; s#.*<body[^>]*>\(.*\)</body[^>]*>.*#\1#; p; }' /tmp/test2.html
<h1>Test</h1><p>This is a test</p>
Existing sed expression assumes <body> elements are alone on a line. It fails when there are multiple elements for two reasons: 1. The 2-address 'end' match will never happen 2. The greedy * wildcard will match everything up to the end of the last element The replacement works as follows: 1. Any line other than the last is copied into the hold space 2. The last line is added then hold swapped to pattern and a replace is done on the complete text of the input Sed expression tested using: ``` $ cat <(echo "=== test1.html") /tmp/test1.html <( echo "=== test2.html") /tmp/test2.html === test1.html <!DOCTYPE html> <html> <head> <title>Test</title> </head> <body> <h1>Test</h1> <p>This is a test</p> </body> </html> === test2.html <!DOCTYPE html> <html> <head> <title>Test</title> </head> <body><h1>Test</h1><p>This is a test</p></body> </html> $ sed -n -e '$! { H; d; }' -e '$ { H; x; s#.*<body[^>]*>\(.*\)</body[^>]*>.*#\1#; p; }' /tmp/test1.html <h1>Test</h1> <p>This is a test</p> $ sed -n -e '$! { H; d; }' -e '$ { H; x; s#.*<body[^>]*>\(.*\)</body[^>]*>.*#\1#; p; }' /tmp/test2.html <h1>Test</h1><p>This is a test</p> ```
Existing sed expression assumes <body> elements are alone on a line. It
 fails when there are multiple elements for two reasons:

 1. The 2-address 'end' match will never happen
 2. The greedy * wildcard will match everything up to the end of the
    last element

The replacement works as follows:

 1. Any line other than the last is copied into the hold space
 2. The last line is added then hold swapped to pattern and a replace is
    done on the complete text of the input

Signed-off-by: Tj <tj.iam.tj@proton.me>
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u fix_html_body_parsing:Iam_Tj-fix_html_body_parsing
git switch Iam_Tj-fix_html_body_parsing

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch main
git merge --no-ff Iam_Tj-fix_html_body_parsing
git switch Iam_Tj-fix_html_body_parsing
git rebase main
git switch main
git merge --ff-only Iam_Tj-fix_html_body_parsing
git switch Iam_Tj-fix_html_body_parsing
git rebase main
git switch main
git merge --no-ff Iam_Tj-fix_html_body_parsing
git switch main
git merge --squash Iam_Tj-fix_html_body_parsing
git switch main
git merge --ff-only Iam_Tj-fix_html_body_parsing
git switch main
git merge Iam_Tj-fix_html_body_parsing
git push origin main
Sign in to join this conversation.
No reviewers
No labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
stefano/BSSG!39
No description provided.