Error while parsing YAML block of text - ruby

I am new to YAML, and have tried to do a YAML block but I am getting the following error:
while scanning a block mapping: expected , but found: #< ScalarToken Value="content: | This is a test of a test test test test A very very good test yeah yeah A test of test test" Style="None"> (line 8, column 1)
While it works fine without the |, I want whitespace preservation.
YAML file (Home.yml):
---
section:
title: About this site
content: |
This is a test of a test test test test
A very very good test
A test of test test
section:
title: Source code
content: |
Licens:: BSD
Link:: Here
Foo
...
Ruby code:
home = YAML.load_file('data/Home.yml')
home.inspect

Which YAML parser are you using? Both the Ruby 1.8.7 parser and the parser in 1.9 parse the YAML in your question.
There's still trouble, through. The syntax you've given is for a hash like this:
{
'section' => {
'title' => "About this site",
'content => ...
}
'section' => {
'title' => 'Source code',
'content' => ...
}
}
However, you can't have two hash keys be the same. What happens is that the last one wins. You may be looking for an array of hashes. To do that, use this YAML syntax:
---
-
section:
title: About this site
content: |
This is a test of a test test test test
A very very good test
A test of test test
-
section:
title: Source code
content: |
Licens:: BSD
Link:: Here
Foo

Related

asciidoctor not run (or not running properly) in gradle

I am having trouble getting the asciidoctor to run in gradle. The script is below.
asciidoctor {
logger.lifecycle("Processing from $sourceDir to $asciiDocOutputDir")
sources {
include 'index.adoc'
}
backends = ['html5']
attributes = [
doctype: 'book',
toc: 'left',
toclevels: '3',
numbered: '',
sectlinks: '',
sectanchors: '',
hardbreaks: '',
generated: asciiDocOutputDir
]
}
Here is the printout from the debug line to show the directories:
Processing from C:\Users\xxx\eclipse-workspace\springfox\src\docs\asciidoc to C:\Users\xxx\eclipse-workspace\springfox\build\asciidoc\generated
With the above logging line printed, does it mean the asciidoctor task was run? I don't see error and the index.html was not created (or at least I could not find it in the folder I expect to see it).
I have an index.adoc file under:
C:\Users\xxx\eclipse-workspace\springfox\src\docs\asciidoc\index.adoc with this content:
include::{generated}/overview.adoc[]
include::{generated}/paths.adoc[]
include::{generated}/security.adoc[]
include::{generated}/definitions.adoc[]
The 4 .adoc files are available in the following folder with contents:
C:\Users\xxx\eclipse-workspace\springfox\build\asciidoc\generated
But I could not get the index.html created :-(. There is got to be a silly mistake somewhere but I just could not figure out. What should I look for, or how can I debug this?
Thanks!

Why does puppet think my custom fact is a string?

I am trying to create a custom fact I can use as the value for a class parameter in a hiera yaml file.
I am using the openstack/puppet-keystone module and I want to use fernet-keys.
According to the comments in the module I can use this parameter.
# [*fernet_keys*]
# (Optional) Hash of Keystone fernet keys
# If you enable this parameter, make sure enable_fernet_setup is set to True.
# Example of valid value:
# fernet_keys:
# /etc/keystone/fernet-keys/0:
# content: c_aJfy6At9y-toNS9SF1NQMTSkSzQ-OBYeYulTqKsWU=
# /etc/keystone/fernet-keys/1:
# content: zx0hNG7CStxFz5KXZRsf7sE4lju0dLYvXdGDIKGcd7k=
# Puppet will create a file per key in $fernet_key_repository.
# Note: defaults to false so keystone-manage fernet_setup will be executed.
# Otherwise Puppet will manage keys with File resource.
# Defaults to false
So wrote this custom fact ...
[root#puppetmaster modules]# cat keystone_fernet/lib/facter/fernet_keys.rb
Facter.add(:fernet_keys) do
setcode do
fernet_keys = {}
puts ( 'Debug keyrepo is /etc/keystone/fernet-keys' )
Dir.glob('/etc/keystone/fernet-keys/*').each do |fernet_file|
data = File.read(fernet_file)
if data
content = {}
puts ( "Debug Key file #{fernet_file} contains #{data}" )
fernet_keys[fernet_file] = { 'content' => data }
end
end
fernet_keys
end
end
Then in my keystone.yaml file I have this line:
keystone::fernet_keys: '%{::fernet_keys}'
But when I run puppet agent -t on my node I get this error:
Error: Could not retrieve catalog from remote server: Error 500 on SERVER: Server Error: Evaluation Error: Error while evaluating a Function Call, "{\"/etc/keystone/fernet-keys/1\"=>{\"content\"=>\"xxxxxxxxxxxxxxxxxxxx=\"}, \"/etc/keystone/fernet-keys/0\"=>{\"content\"=>\"xxxxxxxxxxxxxxxxxxxx=\"}}" is not a Hash. It looks to be a String at /etc/puppetlabs/code/environments/production/modules/keystone/manifests/init.pp:1144:7 on node mgmt-01
I had assumed that I had formatted the hash correctly because facter -p fernet_keys output this on the agent:
{
/etc/keystone/fernet-keys/1 => {
content => "xxxxxxxxxxxxxxxxxxxx="
},
/etc/keystone/fernet-keys/0 => {
content => "xxxxxxxxxxxxxxxxxxxx="
}
}
The code in the keystone module looks like this (with line numbers)
1142
1143 if $fernet_keys {
1144 validate_hash($fernet_keys)
1145 create_resources('file', $fernet_keys, {
1146 'owner' => $keystone_user,
1147 'group' => $keystone_group,
1148 'subscribe' => 'Anchor[keystone::install::end]',
1149 }
1150 )
1151 } else {
Puppet does not necessarily think your fact value is a string -- it might do, if the client is set to stringify facts, but that's actually beside the point. The bottom line is that Hiera interpolation tokens don't work the way you think. Specifically:
Hiera can interpolate values of any of Puppet’s data types, but the
value will be converted to a string.
(Emphasis added.)

YML syntax error

I want to have a multi-line bit of markdown java in a yam file. I tried many things but I guess I don't quite get the quoting rules of Yaml.
{
title: Museum,
body: |
"```java
code code code
java2",
answers: [
"`museum`",
"`museum.getFloor(3)`",
"`museum.getFloor(3).getExhibit(5)`",
"`museum.getFloor(3).getExhibit(5).getCurator()`",
"`museum.getFloor(3).getExhibit(5).getCurator().name`",
"`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"
]
}
Produces:
/Users/pitosalas/.rbenv/versions/2.3.1/lib/ruby/2.3.0/psych.rb:377:in `parse': (generator/test.yml): found character that cannot start any token while scanning for the next token at line 3 column 9 (Psych::SyntaxError)
YAML has two styles: the JSON like flow style and the much better human readable block style.
Roughly speaking you can have nested structures each style nested within itself and can have flow style nested within block style, but block style nested within flow style is not allowed.
Your to level { and } are flow style but you try to introduce, with |, a literal block style scalar within that flow style. Replace the flow style with block style upwards from that scalar:
title: Museum
body: |
"```java
code code code
java2"
answers: [
"`museum`",
"`museum.getFloor(3)`",
"`museum.getFloor(3).getExhibit(5)`",
"`museum.getFloor(3).getExhibit(5).getCurator()`",
"`museum.getFloor(3).getExhibit(5).getCurator().name`",
"`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"
]
and your YAML is fine. Note that the double quotes "around" the value for the key body are not going to be stripped when loading, maybe that is not what you intended.
You should IMO not leave out the trailing , after the last value in the (flow style) sequence that is the value for answers. This will certainly lead to errors when you extend the list and forget to put in the trailing comma on the line above.
I would personally go for block style all the way:
title: Museum
body: |
"```java
code code code
java2"
answers:
- "`museum`"
- "`museum.getFloor(3)`"
- "`museum.getFloor(3).getExhibit(5)`"
- "`museum.getFloor(3).getExhibit(5).getCurator()`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"
When dealing with YAML file generation that is convoluted or complex, or when it's not working as I expect, I revert to letting Ruby show me the way:
require 'yaml'
body = <<EOT
"```java
code code code
java2
"
EOT
answers = %w(
`museum`
`museum.getFloor(3)`
`museum.getFloor(3).getExhibit(5)`
`museum.getFloor(3).getExhibit(5).getCurator()`
`museum.getFloor(3).getExhibit(5).getCurator().name`
`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`
)
obj = {
"title" => "Museum",
"body" => body,
"answers" => answers
}
puts obj.to_yaml
Which, in this case, outputs:
---
title: Museum
body: |
"```java
code code code
java2
"
answers:
- "`museum`"
- "`museum.getFloor(3)`"
- "`museum.getFloor(3).getExhibit(5)`"
- "`museum.getFloor(3).getExhibit(5).getCurator()`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name`"
- "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"
If you then pass that YAML back into the parser, you should get the original data structure back:
YAML.load(obj.to_yaml)
# => {"title"=>"Museum",
# "body"=>"\"```java\n" +
# "code code code\n" +
# "java2\n" +
# "\"\n",
# "answers"=>
# ["`museum`",
# "`museum.getFloor(3)`",
# "`museum.getFloor(3).getExhibit(5)`",
# "`museum.getFloor(3).getExhibit(5).getCurator()`",
# "`museum.getFloor(3).getExhibit(5).getCurator().name`",
# "`museum.getFloor(3).getExhibit(5).getCurator().name.toUpper()`"]}

expect(Class).to receive(:x).with(hash_including(y: :z)) doesn't work

I want to check that Pandoc.convert is called with to: :docx option like this:
options = {to: :docx}
PandocRuby.convert("some string", options)
I have the following expectation in a spec:
expect(PandocRuby).to receive(:convert).with(hash_including(to: :docx))
The spec fails like this:
Failure/Error: expect(PandocRuby).to receive(:convert).with(hash_including(to: :docx))
(PandocRuby (class)).convert(hash_including(:to=>:docx))
expected: 1 time with arguments: (hash_including(:to=>:docx))
received: 0 times
But when debugging, options is like this:
[2] pry(#<ReportDocumentsController>)> options
=> {
:to => :docx,
:reference_docx => "/Users/josh/Documents/Work/Access4All/Projects/a4aa2/src/public/uploads/report_template/reference_docx/1/reference.docx"
}
I think I'm using the wrong RSpec matcher (or the right one in the wrong way), but I can't get it working.
You just need to expect all of the method arguments:
expect(PandocRuby).to receive(:convert).with("some string", hash_including(to: :docx))
Or you could use a matcher to be less specific about the first argument, e.g.
expect(PandocRuby).to receive(:convert).with(an_instance_of(String), hash_including(to: :docx))

How can I apply basic html formatting to a sublime text syntax definition only within

Ok so by the following yaml definition, shouldn't only what's in between {blah} and {!blah} get text.html.basic syntax highlighting? And the {blah} tags themselves take the highlighting of a comment? Unfortunately it's not happening that way. HTML highlighting is anywhere in the document, and {blah} doesn't get comment highlighting.
# [PackageDev] target_format: plist, ext: tmLanguage
---
name: Test
scopeName: source.test
fileTypes: [test]
uuid: 3631aac6-ee25-4ec1-ab08-39f156235363
patterns:
- name: comment.number-sign
begin: \{blah\}
end: \{\!blah\}
patterns:
- include: text.html.basic
match: .
...
Sample Code:
{blah}
<input type="text"/>
{!blah} <----- these are not styled according to comment.number-sign
<input type="text"/> <----- this also has HTML highlighting and I don't want it to
This should get you started:
# [PackageDev] target_format: plist, ext: tmLanguage
name: Test
scopeName: source.test
fileTypes: [test]
uuid: 3631aac6-ee25-4ec1-ab08-39f156235363
patterns:
- name: html.test
begin: (\{blah\})
beginCaptures:
'1': {name: comment.blah.test}
end: (\{!blah\})
endCaptures:
'1': {name: comment.blah.test}
patterns:
- include: text.html.basic
The name is the base scope for the entire selector. beginCaptures and endCaptures assign the comment.blah.test scope to the {blah} tags, while the include rule assigns HTML highlighting to whatever is between the begin and end tags.
Using the Neon Color Scheme (full disclosure: I'm its maintainer), a sample file looks like this:
As you can see, {blah} and {!blah} are in gray italics, the comment scope. Markup outside the {blah} tags is not highlighted at all, while that enclosed in the {blah} tags is highlighted as HTML.
For more on syntax definitions, check the tutorial and the reference at the unofficial docs.

Resources