caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] A question about Format and semantic tags
@ 2016-03-23 12:47 Alan Schmitt
  2016-03-23 15:01 ` Benoit Vaugon
  0 siblings, 1 reply; 3+ messages in thread
From: Alan Schmitt @ 2016-03-23 12:47 UTC (permalink / raw)
  To: OCaml Mailing List

[-- Attachment #1: Type: text/plain, Size: 1493 bytes --]

Hello,

I need to output two versions of a string that only differ by the
presence of some markers in them, and I want the output to be pretty
(it's code to be read). It seems that semantic tags from the Format
module would allow me to do this, with identical line breaking as these
tags are considered 0 width. Unfortunately I cannot seem to make it
work. Here is what I have tried:

#+begin_src ocaml
let file_put_contents filename text =
   try
      let handle = open_out filename in
      output_string handle text;
      close_out handle
   with Sys_error s ->
     failwith ("Could not write in file: " ^ filename ^ "\n" ^ s)

let filename = "test_out.txt"

let out = "@[<hov 2>Hello World@ Hello@ Hello again@ Hello@ Hello again@ Hello@ Hello again@ @{<foo>Hello@ Cut again@}@]"

let _ =
  Format.set_tags true;
  Format.set_mark_tags true;
  Format.fprintf Format.str_formatter (Scanf.format_from_string out "");
  file_put_contents filename (Format.flush_str_formatter ())
#+end_src

To run this I do ~ocamlc -o tags tags.ml && ./tags && cat test_out.txt~.

As I have turned on treatment of tags and tag marking, I would expect to
see the tag marker in the output string using the default format (<foo>
and </foo>), but I do not see them.

Am I on the right track by using tags? Could someone point me to my
mistake?

Thanks a lot,

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Monthly Athmospheric CO₂ (2016-02, Mauna Loa Obs.): 404.02

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] A question about Format and semantic tags
  2016-03-23 12:47 [Caml-list] A question about Format and semantic tags Alan Schmitt
@ 2016-03-23 15:01 ` Benoit Vaugon
  2016-03-23 15:38   ` Alan Schmitt
  0 siblings, 1 reply; 3+ messages in thread
From: Benoit Vaugon @ 2016-03-23 15:01 UTC (permalink / raw)
  To: alan.schmitt, caml-list

Hello Alan,

In fact, the Format tag activation is local to a formatter. The function 
Format.set_tags enables tags only for the std_formatter (associated to 
stdout). If you want to activate tags for the Format.str_formatter, you 
should call:

Format.pp_set_tags Format.str_formatter true;

instead of:

Format.set_tags true;
Format.set_mark_tags true;

Benoît.


Le 23/03/2016 13:47, Alan Schmitt a écrit :
> Hello,
>
> I need to output two versions of a string that only differ by the
> presence of some markers in them, and I want the output to be pretty
> (it's code to be read). It seems that semantic tags from the Format
> module would allow me to do this, with identical line breaking as these
> tags are considered 0 width. Unfortunately I cannot seem to make it
> work. Here is what I have tried:
>
> #+begin_src ocaml
> let file_put_contents filename text =
>     try
>        let handle = open_out filename in
>        output_string handle text;
>        close_out handle
>     with Sys_error s ->
>       failwith ("Could not write in file: " ^ filename ^ "\n" ^ s)
>
> let filename = "test_out.txt"
>
> let out = "@[<hov 2>Hello World@ Hello@ Hello again@ Hello@ Hello again@ Hello@ Hello again@ @{<foo>Hello@ Cut again@}@]"
>
> let _ =
>    Format.set_tags true;
>    Format.set_mark_tags true;
>    Format.fprintf Format.str_formatter (Scanf.format_from_string out "");
>    file_put_contents filename (Format.flush_str_formatter ())
> #+end_src
>
> To run this I do ~ocamlc -o tags tags.ml && ./tags && cat test_out.txt~.
>
> As I have turned on treatment of tags and tag marking, I would expect to
> see the tag marker in the output string using the default format (<foo>
> and </foo>), but I do not see them.
>
> Am I on the right track by using tags? Could someone point me to my
> mistake?
>
> Thanks a lot,
>
> Alan
>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Caml-list] A question about Format and semantic tags
  2016-03-23 15:01 ` Benoit Vaugon
@ 2016-03-23 15:38   ` Alan Schmitt
  0 siblings, 0 replies; 3+ messages in thread
From: Alan Schmitt @ 2016-03-23 15:38 UTC (permalink / raw)
  To: Benoit Vaugon; +Cc: caml-list

[-- Attachment #1: Type: text/plain, Size: 596 bytes --]

On 2016-03-23 16:01, Benoit Vaugon <benoit.vaugon@gmail.com> writes:

> Hello Alan,
>
> In fact, the Format tag activation is local to a formatter. The function
> Format.set_tags enables tags only for the std_formatter (associated to stdout).
> If you want to activate tags for the Format.str_formatter, you should call:
>
> Format.pp_set_tags Format.str_formatter true;
>
> instead of:
>
> Format.set_tags true;
> Format.set_mark_tags true;

That's it, thanks a lot!

Alan

-- 
OpenPGP Key ID : 040D0A3B4ED2E5C7
Monthly Athmospheric CO₂ (2016-02, Mauna Loa Obs.): 404.02

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 472 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2016-03-23 15:38 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-23 12:47 [Caml-list] A question about Format and semantic tags Alan Schmitt
2016-03-23 15:01 ` Benoit Vaugon
2016-03-23 15:38   ` Alan Schmitt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).