Skip to content

Conversation

@mpfaff
Copy link

@mpfaff mpfaff commented Jun 15, 2019

This pull request proposes adding support for converting FrontMatterDocument to a raw String.

See #1 for the initial discussion.

Added
* Add conversion of FrontMatterDocument to front-matter markdown files

Modified
* Add .idea/ to .gitignore
* Reimplement parseContent and parseFile functions as constructors/functions on FrontMatterDocument
* Change FrontMatterDocument.value to a getter transforming content and data to a front-matter markdown files
* Update test to match changes
* Update example to match changes
@mpfaff
Copy link
Author

mpfaff commented Jun 15, 2019

Unfortunately, the YAML spec forbids parsers from keeping key/value definition order, so, to keep orders consistent, the raw generation currently sorts entries by comparing the keys.

@izolate
Copy link
Owner

izolate commented Jun 15, 2019

Thanks for your PR @MaterialTime. Your formatter is adding spaces to function parentheses. While I take a deeper look at your PR, would you mind pushing a commit to revert those changes to reduce the diff a bit? I'm using the default dartfmt here.

@mpfaff
Copy link
Author

mpfaff commented Jun 15, 2019

@izolate Yeah, sorry about that. I'd be happy to push a commit without those changes.

@izolate izolate changed the base branch from master to develop June 16, 2019 10:21
Copy link
Owner

@izolate izolate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate the effort, but the API changes needed a discussion beforehand. A method already exists to return the raw string, so I don't believe any changes are needed here.

var fileContents = await file.readAsString();

var doc = front_matter.parse(fileContents);
var doc = FrontMatterDocument.fromText(fileContents);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this change really necessary? It introduces a breaking API change. Happy to discuss this in a separate issue (or PR) but I think this is the wrong PR for it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you feel this PR isn't the place for it, I can revert it back to the way it is done in your version of the package.

Copy link
Author

@mpfaff mpfaff Jun 16, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't mind me asking, as I'm working on reverting it now, is there a reason you have the parse method pointing directly to the parseContent method, except for the defaultDelimiter bit?

Would you mind if I just put the contents of parseContent directly into the parse method?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a reason you have the parse method pointing directly to the parseContent method?

Just a functional programming style.

Would you mind if I just put the contents of parseContent directly into the parse method?

I would, unfortunately. I've just pushed a new release 1.1.0 with some small changes. One of the changes is to rename the parseContent method to parser. I'd appreciate if you kept the current directory structure, with the two methods (parse and parseFile) exposed from front_matter.dart delegating out to the parser() from parser.dart.

I recommend rebasing with the latest commits from master and starting from there.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I'm considering how to implement my changes in line with the things you've mentioned, is the FrontMatterDocument constructor supposed to be public? Is it supposed to be used outside of the package itself?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it supposed to be used outside of the package itself?

At the moment, no. But this is subject to change.

Copy link
Owner

@izolate izolate Jun 17, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MaterialTime the more I think about it, the less I'm convinced that this FrontMatterDocument to String method is possible. The method to generate the YAML string from a Map is outside the scope of this package, and frankly better suited to the yaml package.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've looked around, and the yamlicious package might be a suitable alternative for generating the YAML portion of value. I hope that this can be used to resolve the issue of generating YAML strings being outside the scope of this package.

// The content begins after the closing delimiter index.
content = value.substring(closeIndex + (delimiter.length + 1)).trim();

print('content: $content');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why return the content: prefix here? Just return content directly.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, what's your reasoning for using print() here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that. I believe that was there for testing and I forgot to remove it when I pushed the commit.

YamlMap data;

FrontMatterDocument(this.value);
String get value {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Presuming this method was the reason for the PR, the value getter already exists in the latest version of this package. You can access the original raw value like so:

var str = "---\nfoo: bar\n---\nHello, world!";
var doc = fm.parse(str);

assert(doc.value, equals(str)); // true

Returning the original value directly is preferable over generating the raw string from the parsed document, to avoid any discrepancies.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason for the getter is so that the document can be modified and converted back to a raw String to be stored. Eg. I'm planning to use it with Git storage.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've just released 1.1.0 with a toString() method on FrontMatterDocument that returns the raw, original value. Is this what you need?

Perhaps we should pause this PR and open the discussion in the issue #1 ?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. Maybe I'm communicating it wrong, but the point of the getter is so that one can modify the data or content variables on a FrontMatterDocument can be modified and easily converted back to a raw front-matter document.

@izolate izolate changed the base branch from develop to master June 16, 2019 11:01
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.

2 participants