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

Implement --prettify parameter (only supports HTML for now). #7825

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

fvbommel
Copy link

@fvbommel fvbommel commented Oct 13, 2020

This is similar to --minify, but has essentially the opposite effect: instead of making the output less readable it makes it more readable.

This uses github.com/yosssi/gohtml to perform the actual transformation.

Fixes #7190


The default configuration currently looks like this:

prettify:
  prettifyOutput: false
  disableHTML: false
  html:
    condense: false
    inlineTags:
      - a
      - code
      - em
      - span
      - strong
    inlineTagsMaxLength: 40

which is based on the defaults of gohtml.

I'm especially looking for feedback on whether the interface (parameter name and config syntax) are reasonable.

TODOs:

  • For configuration stability the defaults should probably be hard-coded instead of taken from gohtml, right?
    • Currently the default configuration just imports the gohtml globals in the initializer.
  • I haven't added the line wrapping configuration yet.
    • gohtml supports LineWrapColumn and LineWrapMaxSpillover.
  • Documentation.

Stuff that may happen later:

  • Support other media types (I'd prefer these to be separate PRs though)
    • At least JSON should be doable using encoding/json, I haven't looked into others.
  • Add a Resources.Prettify pipeline function so it can be used from there too.
    • Less useful if only HTML is supported?

This is similar to --minify, but has essentially the opposite effect:
instead of making the output less readable it makes it *more* readable.

This uses github.com/yosssi/gohtml to perform the actual transformation.

Fixes gohugoio#7190
@CLAassistant
Copy link

CLAassistant commented Oct 13, 2020

CLA assistant check
All committers have signed the CLA.

@fvbommel
Copy link
Author

Oh, sorry @nu8, I forgot to add that to the configuration syntax above. (I'll edit it in)

You can enable like this:

prettify:
  prettifyOutput: true

This is basically the same as how --minify can be enabled via the configuration file, but with "minify" replaced with "prettify".

@fvbommel
Copy link
Author

I think it depends on how you structure your pages, but yes I've noticed that too if there are no tags on either end of the white space.

Like the first message says, this implementation uses github.com/yosssi/gohtml (as suggested in #7190), and it appears to mostly leave the "middle" of pure-text nodes alone. Mostly though, it seems to do a pretty good job.

If you know of a better HTML formatting lib written in Go though, feel free to suggest it. (I did look around a bit and this is definitely the most popular one I found at least)

Alternatively, you could raise this issue in https://github.com/yosssi/gohtml and/or submit a PR there.

@fvbommel
Copy link
Author

Ah yes, that will also "break" it. 🤣

@bep
Copy link
Member

bep commented Dec 2, 2020

Hey, thanks for this and sorry for the delay.

I need to wait with this. I looked into the gohtml package in relation with something else, and there are some bugs in there that means we cannot use this (my memory is a little hazy, but it's related to whitespace handling with text and span elements). I looked into fixing this upstream, but that looked hard ... I have been tinkering with my own html formatter Go package, which I think will get ready soonish ... will let you know.

@stale
Copy link

stale bot commented Jun 26, 2021

This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
If this is a bug and you can still reproduce this error on the master branch, please reply with all of the information you have about it in order to keep the issue open.
If this is a feature request, and you feel that it is still relevant and valuable, please tell us why.
This issue will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@jwflory
Copy link

jwflory commented Oct 31, 2021

Is there a chance of this work making it into a future release of Hugo?

@github-actions
Copy link

This PR has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help.
Please check https://github.com/gohugoio/hugo/blob/master/CONTRIBUTING.md#code-contribution and verify that this code contribution fits with the description. If yes, tell is in a comment.
This PR will automatically be closed in the near future if no further activity occurs. Thank you for all your contributions.

@github-actions github-actions bot added the Stale label May 31, 2023
@BBaoVanC
Copy link

Please don't close this, I would love this feature!

@github-actions github-actions bot removed the Stale label Jul 22, 2023
@hiAndrewQuinn
Copy link

I'd be interested in this too. Commenting to keep it alive.

@mjp4
Copy link

mjp4 commented Mar 12, 2024

I too would love to see this get in.

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.

Add HTML tidying as an option
7 participants