Skip to content

Commit

Permalink
help,markdown: Fix double line breaks when converting to text
Browse files Browse the repository at this point in the history
Also fix ugly last line break hack
  • Loading branch information
wader committed Mar 2, 2023
1 parent 461783e commit dec433f
Show file tree
Hide file tree
Showing 22 changed files with 70 additions and 75 deletions.
6 changes: 3 additions & 3 deletions format/apple/bookmark/testdata/help_applebookmark.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@ about these types of files, see Sarah Edwards' excellent research on the subject
fq's grep_by function can be used to recursively descend through the decoded tree, probing for and selecting any bookmark blobs, then
converting them to readable JSON with torepr:


fq 'grep_by(.type=="data" and .value[0:4] == "book") | .value | apple_bookmark |
torepr' <sfl2 file>

Authors
=======

- David McDonald @dgmcdona (https://github.com/dgmcdona) @river_rat_504 (https://twitter.com/river_rat_504)

References
==========

- https://developer.apple.com/documentation/foundation/url/2143023-bookmarkdata
- https://mac-alias.readthedocs.io/en/latest/bookmark_fmt.html
-
https://www.mac4n6.com/blog/2016/1/1/manual-analysis-of-nskeyedarchiver-formatted-plist-files-a-review-of-the-new-os-x-1011-recent-items
- https://michaellynn.github.io/2015/10/24/apples-bookmarkdata-exposed/

5 changes: 3 additions & 2 deletions format/apple/macho/testdata/help_macho.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ Select 64bit load segments
==========================

$ fq '.load_commands[] | select(.cmd=="segment_64")' file

References
==========

- https://github.com/aidansteele/osx-abi-macho-file-format-reference

Authors
=======
- Sıddık AÇIL acils@itu.edu.tr @Akaame (https://github.com/Akaame)

- Sıddık AÇIL acils@itu.edu.tr @Akaame (https://github.com/Akaame)
8 changes: 4 additions & 4 deletions format/asn1/testdata/help_asn1_ber.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,21 @@ Can be used to decode certificates etc
======================================

$ fq -d bytes 'from_pem | asn1_ber | d' cert.pem

Can decode nested values
========================

$ fq -d asn1_ber '.constructed[1].value | asn1_ber' file.ber

Manual schema
=============

$ fq -d asn1_ber 'torepr as $r | ["version", "modulus", "private_exponent", "private_exponen", "prime1", "prime2", "exponent1", "exponent2", "coefficient"] | with_entries({key: .value, value: $r[.key]})' pkcs1.der

References
==========

- https://www.itu.int/ITU-T/studygroups/com10/languages/X.690_1297.pdf
- https://en.wikipedia.org/wiki/X.690
- https://letsencrypt.org/docs/a-warm-welcome-to-asn1-and-der/
- https://lapo.it/asn1js/

3 changes: 2 additions & 1 deletion format/avro/testdata/help_avro_ocf.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ Limitations:

References
==========

- https://avro.apache.org/docs/current/spec.html#Object+Container+Files

Authors
=======
- Xentripetal xentripetal@fastmail.com @xentripetal (https://github.com/xentripetal)

- Xentripetal xentripetal@fastmail.com @xentripetal (https://github.com/xentripetal)
4 changes: 2 additions & 2 deletions format/bencode/testdata/help_bencode.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Convert represented value to JSON
=================================

$ fq -d bencode torepr file.torrent

References
==========
- https://wiki.theory.org/BitTorrentSpecification#Bencoding

- https://wiki.theory.org/BitTorrentSpecification#Bencoding
6 changes: 3 additions & 3 deletions format/bson/testdata/help_bson.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ Convert represented value to JSON
=================================

$ fq -d bson torepr file.bson

Filter represented value
========================

$ fq -d bson 'torepr | select(.name=="bob")' file.bson

References
==========
- https://bsonspec.org/spec.html

- https://bsonspec.org/spec.html
4 changes: 2 additions & 2 deletions format/cbor/testdata/help_cbor.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ Convert represented value to JSON
=================================

$ fq -d cbor torepr file.cbor

References
==========

- https://en.wikipedia.org/wiki/CBOR
- https://www.rfc-editor.org/rfc/rfc8949.html

3 changes: 1 addition & 2 deletions format/csv/testdata/help_csv.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ TSV to CSV
==========

$ fq -d csv -o comma="\t" to_csv file.tsv

Convert rows to objects based on header row
===========================================

$ fq -d csv '.[0] as $t | .[1:] | map(with_entries(.key = $t[.key]))' file.csv

34 changes: 14 additions & 20 deletions format/markdown/markdown.jq
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,35 @@ def _word_break($width):

def _markdown_to_text($width; $header_depth):
def lb: if $width > 0 then _word_break($width) | join("\n") end;
def _f($pln):
def _f:
if type == "string" then gsub("\n"; " ")
elif .type == "document" then .children[] | _f("\n\n")
elif .type == "document" then .children[] | _f
elif .type == "heading" then
( (.children[] | _f("\n\n")) as $title
| $title
, "\n"
, ("=" * ($title | length))
, "\n"
( (.children[] | _f) as $title
| "\($title)\n\("=" * ($title | length))"
)
elif .type == "paragraph" then
( ( [.children[] | _f("\n\n")]
| join("")
| lb
)
, $pln
( [.children[] | _f]
| join("")
| lb
)
elif .type == "link" then
( ( [ .children[]
| _f("")
| _f
]
| join("")
) as $text
| $text
, if $text != .destination then " (", .destination, ")"
else empty
| if $text == .destination then $text
else "\($text) (\(.destination))"
end
)
elif .type == "code_block" then "\n", (" ", .literal | split("\n") | join("\n ")), "\n"
elif .type == "code_block" then .literal | rtrimstr("\n") | split("\n") | " " + join("\n ")
elif .type == "code" then .literal
elif .type == "list" then (.children[] | _f("\n\n")), "\n" # TODO: delim
elif .type == "list_item" then .bullet_char, " ", (.children[] | _f("\n"))
elif .type == "list" then ([.children[] | _f] | join("\n")) # TODO: delim
elif .type == "list_item" then "\(.bullet_char) \(.children[] | _f)"
elif .type == "html_span" then .literal | gsub("<br>"; "\n") # TODO: more?
else empty
end;
[_f("\n\n")] | join("");
[_f] | join("\n\n");
def _markdown_to_text:
_markdown_to_text(-1; 0);
1 change: 0 additions & 1 deletion format/markdown/testdata/help_markdown.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@ Array with all level 1 and 2 headers
====================================

$ fq -d markdown '[.. | select(.type=="heading" and .level<=2)?.children[0]]' file.md

6 changes: 3 additions & 3 deletions format/matroska/testdata/help_matroska.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ Lookup element using path
=========================

$ fq 'matroska_path(".Segment.Tracks[0)")' file.mkv

Get path to element
===================

$ fq 'grep_by(.id == "Tracks") | matroska_path' file.mkv

References
==========

- https://tools.ietf.org/html/draft-ietf-cellar-ebml-00
- https://matroska.org/technical/specs/index.html
- https://www.matroska.org/technical/basics.html
- https://www.matroska.org/technical/codec_specs.html
- https://wiki.xiph.org/MatroskaOpus

14 changes: 7 additions & 7 deletions format/mp4/testdata/help_mp4.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -24,36 +24,36 @@ Speed up decoding by not decoding samples

# manually decode first sample as a aac_frame
$ fq -o decode_samples=false '.tracks[0].samples[0] | aac_frame | d' file.mp4

Entries for first edit list as values
=====================================

$ fq 'first(grep_by(.type=="elst").entries) | tovalue' file.mp4

Whole box tree as JSON (exclude mdat data and tracks)
=====================================================

$ fq 'del(.tracks) | grep_by(.type=="mdat").data = "<excluded>" | tovalue' file.mp4

Force decode a single box
=========================

$ fq -n '"AAAAHGVsc3QAAAAAAAAAAQAAADIAAAQAAAEAAA==" | from_base64 | mp4({force:true}) | d'

Lookup mp4 box using a mp4 box path.
====================================

# <decode value box> | mp4_path($path) -> <decode value box>
$ fq 'mp4_path(".moov.trak[1]")' file.mp4

Get mp4 box path for a decode value box.
========================================

# <decode value box> | mp4_path -> string
$ fq 'grep_by(.type == "trak") | mp4_path' file.mp4

References
==========

- ISO/IEC base media file format (MPEG-4 Part 12) (https://en.wikipedia.org/wiki/ISO/IEC_base_media_file_format)
- Quicktime file format (https://developer.apple.com/standards/qtff-2001.pdf)

4 changes: 2 additions & 2 deletions format/msgpack/testdata/help_msgpack.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Convert represented value to JSON
=================================

$ fq -d msgpack torepr file.msgpack

References
==========
- https://github.com/msgpack/msgpack/blob/master/spec.md

- https://github.com/msgpack/msgpack/blob/master/spec.md
4 changes: 2 additions & 2 deletions format/protobuf/testdata/help_protobuf.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Can decode sub messages
=======================

$ fq -d protobuf '.fields[6].wire_value | protobuf | d' file

References
==========
- https://developers.google.com/protocol-buffers/docs/encoding

- https://developers.google.com/protocol-buffers/docs/encoding
7 changes: 4 additions & 3 deletions format/riff/testdata/help_avi.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,22 @@ Decode examples

Samples
=======

AVI has many redundant ways to index samples so currently .streams[].samples will only include samples the most "modern" way used in
the file. That is in order of stream super index, movi ix index then idx1 index.

Extract samples for stream 1
============================

$ fq '.streams[1].samples[] | tobytes' file.avi > stream01.mp3

Show stream summary
===================

$ fq -o decode_samples=false '[.chunks[0] | grep_by(.id=="LIST" and .type=="strl") | grep_by(.id=="strh") as {$type} | grep_by(.id=="strf") as {$format_tag, $compression} | {$type,$format_tag,$compression}]' *.avi

References
==========

- AVI RIFF File Reference (https://learn.microsoft.com/en-us/windows/win32/directshow/avi-riff-file-reference)
- OpenDML AVI File Format Extensions (http://www.jmcgowan.com/odmlff2.pdf)

4 changes: 2 additions & 2 deletions format/rtmp/testdata/help_rtmp.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ Show rtmp streams in PCAP file
==============================

fq '.tcp_connections[] | select(.server.port=="rtmp") | d' file.cap

References
==========

- https://rtmp.veriskope.com/docs/spec/
- https://rtmp.veriskope.com/pdf/video_file_format_spec_v10.pdf

7 changes: 4 additions & 3 deletions format/tzif/testdata/help_tzif.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ Get last transition time
========================

fq '.v2plusdatablock.transition_times[-1] | tovalue' tziffile

Count leap second records
=========================

fq '.v2plusdatablock.leap_second_records | length' tziffile

Authors
=======

- Takashi Oguma @bitbears-dev (https://github.com/bitbears-dev) @0xb17bea125 (https://twitter.com/0xb17bea125)

References
==========
- https://datatracker.ietf.org/doc/html/rfc8536

- https://datatracker.ietf.org/doc/html/rfc8536
7 changes: 4 additions & 3 deletions format/wasm/testdata/help_wasm.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@ Count opcode usage
==================

$ fq '.sections[] | select(.id == "code_section") | [.. | .opcode? // empty] | count | map({key: .[0], value: .[1]}) | from_entries' file.wasm

List exports and imports
========================

$ fq '.sections | {import: map(select(.id == "import_section").content.im.x[].nm.b), export: map(select(.id == "export_section").content.ex.x[].nm.b)}' file.wasm

Authors
=======

- Takashi Oguma @bitbears-dev (https://github.com/bitbears-dev) @0xb17bea125 (https://twitter.com/0xb17bea125)

References
==========
- https://webassembly.github.io/spec/core/

- https://webassembly.github.io/spec/core/
3 changes: 1 addition & 2 deletions format/xml/testdata/help_html.fqtest
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Element as object
"head": ""
}
}

Element as array
================

Expand Down Expand Up @@ -78,4 +78,3 @@ Element as array

# <a> href:s in file
$ fq -r -o array=true -d html '.. | select(.[0] == "a" and .[1].href)?.[1].href' file.html

0 comments on commit dec433f

Please sign in to comment.