2020-12-21 13:12:05 +01:00
# How to build Gtk4 Tutorial
2021-04-20 14:36:12 +02:00
## Quick start guide
2021-01-11 15:32:09 +01:00
2021-04-20 14:36:12 +02:00
1. You need linux operationg system, ruby, rake, pandoc and latex system.
2. download this repository and uncompress the files.
3. change your current directory to the top directory of the source files.
4. type `rake html` to create html files. The files are generated under `html` directory.
5. type `rake pdf` to create pdf file. The file is generated under `latex` directory.
2021-01-11 15:32:09 +01:00
2021-04-20 14:36:12 +02:00
## Prerequisites
2021-01-11 15:32:09 +01:00
2021-04-20 14:36:12 +02:00
- Linux operationg system.
The programs in the repository has been tested on Ubuntu 20.04.
- Download the files in this repository.
There are two ways to download.
1. Use git.
Type `git clone https://github.com/ToshioCP/Gtk4-tutorial.git` on the command-line.
2. Download a zip file.
Click on the code button (green button) in the top page of this repository.
Then, click "Download ZIP".
- Ruby and rake.
- Pandoc. It is used to generate html and latex files.
- Latex system. Texlive2020 or later version is recommended.
It is used to generate pdf file.
2021-01-11 15:32:09 +01:00
## Github flavored markdown
When you see [gtk4_tutorial github page ](https://github.com/ToshioCP/Gtk4-tutorial ), you'll find `Readme.md` contents below the list of files.
2021-04-20 14:36:12 +02:00
This file is written in markdown language.
A markdown file has `.md` suffix.
2021-01-11 15:32:09 +01:00
There are several kinds of markdown language.
`Readme.md` uses 'github flavored markdown', which is often shortened as GFM.
2021-04-20 14:36:12 +02:00
Markdown files in the gfm directory also written in GFM.
If you are not familiar with it, refer to the page [github flavor markdown spec ](https://github.github.com/gfm/ ).
2020-12-21 13:12:05 +01:00
2021-01-11 15:32:09 +01:00
## Pandoc's markdown
2021-04-20 14:36:12 +02:00
This tutorial also uses another markdown -- pandoc's markdown.
2021-01-11 15:32:09 +01:00
Pandoc is a converter between markdown, html, latex, word docx and so on.
2021-04-20 14:36:12 +02:00
This type of markdown is used to convert markdown to html and latex in this tutorial.
2020-12-21 13:12:05 +01:00
2021-01-13 05:29:35 +01:00
## Src.md file
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
Src.md file has ".src.md" suffix.
The syntax of src.md file is similar to markdown but it has a special command which isn't included in markdown syntax.
2021-02-08 14:24:54 +01:00
It is @@@ command.
2021-04-20 14:36:12 +02:00
The command starts with a line that begins with "@@@" and it ends with a line "@@@".
2021-02-08 14:24:54 +01:00
For example,
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
~~~
@@@include
tfeapplication.c
@@@
~~~
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
There are four types of @@@ command.
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
### @@@include
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
This type of @@@ command starts with a line "@@@include".
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
~~~
@@@include
tfeapplication.c
@@@
~~~
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
This command replaces itself with the text read from the C source files surrounded by `@@@include` and `@@@` .
If a function list follows the filename, only the functions are read.
If no function list is given, the command can read any text file other than C source file.
2021-02-08 14:24:54 +01:00
~~~
@@@include
tfeapplication.c main startup
@@@
~~~
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
The command above is replaced by the contents of `main` and `startup` functions in `tfeapplication.c` .
2021-02-08 14:24:54 +01:00
~~~
@@@include
lib_src2md.rb
@@@
~~~
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
This command is replaced by the contents of `lib_src2md.rb` which is a ruby script (not C file).
2021-03-03 15:45:54 +01:00
The inserted text is converted to fence code block.
2021-04-20 14:36:12 +02:00
Fence code block begins with `~~~` and ends with `~~~` .
The contents are displayed verbatim.
`~~~` is look like a fence so the block is called "fence code block".
2021-03-03 15:45:54 +01:00
If the target markdown is GFM, then an info string follows the beginning fence.
2021-04-20 14:36:12 +02:00
The following example shows that the @@@ command includes a C source file `sample.c` .
2020-12-21 13:12:05 +01:00
2021-03-03 15:45:54 +01:00
$ cat src/sample.c
int
main (int argc, char **argv) {
... ...
}
$cat src/sample.src.md
... ...
@@@include -N
sample.c
@@@
... ...
$ ruby src2md.rb src/sample.src.md gfm/sample.md
$ cat gfm/sample.md
... ...
2021-02-08 14:24:54 +01:00
~~~C
int
main (int argc, char **argv) {
... ...
}
~~~
2021-03-03 15:45:54 +01:00
... ...
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
Info strings are usually languages like C, ruby, xml and so on.
2021-02-08 14:24:54 +01:00
This string is decided with the filename extension.
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
- `.c` => C
- `.rb` => ruby
- `.xml` => xml
The supported language is written in line 274 and 275 in `lib/lib_src2md.rb` .
2021-02-08 14:24:54 +01:00
A line number is inserted at the top of each line in the code block.
If you don't want to insert it, give "-N" option to @@@include command.
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
Options:
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
- `-n` : Inserts a line number at the top of each line (default).
- `-N` : No line number is inserted.
The following shows that line numbers are inserted at the beginning of lines.
2020-12-21 13:12:05 +01:00
2021-03-03 15:45:54 +01:00
$cat src/sample.src.md
... ...
@@@include
sample.c
@@@
... ...
$ ruby src2md.rb src/sample.src.md gfm/sample.md
$ cat gfm/sample.md
... ...
~~~C
1 int
2 main (int argc, char **argv) {
... ...
14 }
~~~
... ...
If the target markdown is an intermediate file to html, then another type of info string follows the beginning fence.
If @@@include command doesn't have -N option, then the generated markdown is:
~~~{.C .numberLines}
int
main (int argc, char **argv) {
... ...
}
~~~
2021-04-20 14:36:12 +02:00
2021-03-03 15:45:54 +01:00
The info string `.C` specifies C language.
The info string `.numberLines` is a class of the pandoc markdown.
If the class is given, pandoc generates CSS to insert line numbers to the source code in the html file.
That's why the fence code block in the markdown doesn't have line numbers, which is different from gfm markdown.
2021-04-20 14:36:12 +02:00
If `-N` option is given, then the info string is `{.C}` only.
2021-03-03 15:45:54 +01:00
2021-04-20 14:36:12 +02:00
If the target markdown is an intermediate file to latex, then the same info string follows the beginning fence.
2021-03-03 15:45:54 +01:00
~~~{.C .numberLines}
int
main (int argc, char **argv) {
... ...
}
~~~
Rake uses pandoc with --listings option when it converts markdown to latex.
The generated latex file uses listings package to list source files instead of verbatim environment.
The markdwon above is converted to the following latex source file.
\begin{lstlisting}[language=C, numbers=left]
int
main (int argc, char **argv) {
... ...
}
\end{lstlisting}
Listing package can color or emphasize keywords, strings, comments and directives.
But it doesn't analyze the syntax or token of the language, so the kind of emphasis target is limited.
@@@include command have two advantages.
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
1. Less typing.
2. You don't need to modify your src.md file, even if the C source file is modified.
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
### @@@shell
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
This type of @@@ command starts with a line begins with "@@@shell".
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
~~~
@@@shell
shell command
... ...
@@@
~~~
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
This command replaces itself with:
2020-12-21 13:12:05 +01:00
2021-02-08 14:24:54 +01:00
- the shell command
- the standard output from the shell command
For example,
~~~
@@@shell
wc Rakefile
@@@
~~~
This is converted to:
2021-04-20 14:36:12 +02:00
~~~
$ wc Rakefile
164 475 4971 Rakefile
~~~
2021-02-08 14:24:54 +01:00
### @@@if series
2021-04-20 14:36:12 +02:00
This type of @@@ command starts with a line begins with "@@@if", "@@@elif", "@@@else" or "@@@end".
2021-02-08 14:24:54 +01:00
This command is similar to "#if", "#elif", #else " and "#end" directives in C preprocessor.
For example,
~~~
@@@if gfm
Refer to [tfetextview API reference ](tfetextview/tfetextview_doc.md )
@@@elif html
Refer to [tfetextview API reference ](tfetextview_doc.html )
@@@elif latex
Refer to tfetextview API reference in appendix.
@@@end
~~~
2021-04-20 14:36:12 +02:00
`@@@if` and `@@@elif` have conditions.
They are `gfm` , `html` or `latex` so far.
- gfm: builds a GFM file
- html: builds a html file
- latex: builds a latex file or pdf file.
Other type of conditions may be available in the future version.
2021-02-08 14:24:54 +01:00
## Conversion
2021-04-20 14:36:12 +02:00
The @@@ commands above (@@@include, @@@shell and @@@if series) are carried out by `src2md.rb` .
In addition, some other conversions are made by `src2md.rb` .
2021-02-08 14:24:54 +01:00
2021-04-20 14:36:12 +02:00
- Relative links are changed according to the change of the base directory.
2021-02-09 10:16:52 +01:00
- Size option in image link is removed when the destination is GFM or html.
- Relative link is removed when the destination is latex.
2021-02-08 14:24:54 +01:00
- Lines in fence code block are folded when the destination is latex.
2020-12-21 13:12:05 +01:00
2021-02-09 10:16:52 +01:00
There's a method `src2md` in the `lib/lib_src2md.rb` .
2020-12-21 13:12:05 +01:00
This method converts src.md file into md file.
2021-02-09 10:16:52 +01:00
The script `src2md.rb` just invokes this method.
In the same way, the method is called in the action in `Rakefile` .
2021-04-20 14:36:12 +02:00
The code analyzing @@@if series command is rather complicated.
2021-02-09 10:16:52 +01:00
It is based on the state diagram below.
![state diagram ](../image/state_diagram.png )
2020-12-21 13:12:05 +01:00
2021-03-03 15:45:54 +01:00
## mktbl.rb script
2021-04-20 14:36:12 +02:00
The fourth @@@ command begins with "@@@table".
The contents of this command is a table of GFM or pandoc's markdown.
The script `mktbl.rb` in `src` directory makes a table easy to read.
For example, a text file `sample.md` has a table like this:
2021-03-03 15:45:54 +01:00
2021-04-20 14:36:12 +02:00
Price list
2021-03-03 15:45:54 +01:00
2021-04-20 14:36:12 +02:00
@@@table
|item|price|
|:---:|:---:|
|mouse|$10|
|PC|$500|
@@@
2021-03-03 15:45:54 +01:00
Run the script.
~~~
2021-04-20 14:36:12 +02:00
$ cd src
2021-03-03 15:45:54 +01:00
$ ruby mktbl.rb sample.md
~~~
Then, the file is changed to:
~~~
Price list
|item |price|
|:---:|:---:|
|mouse| $10 |
| PC |$500 |
~~~
2021-04-20 14:36:12 +02:00
The script makes a backup file `sample.md.bak` .
2021-03-03 15:45:54 +01:00
The task of the script seems easy, but the program is not so simple.
2021-04-20 14:36:12 +02:00
The script `mktbl.rb` uses a library `lib/lib_mktbl.rb`
This script is independent from `src2md.rb` .
2021-03-03 15:45:54 +01:00
2020-12-21 13:12:05 +01:00
## Directory structure
2021-04-20 14:36:12 +02:00
There are seven directories under `gtk4_tutorial` directory.
They are `gfm` , `src` , `image` , `html` , `latex` , `doc` and `lib` .
2021-02-08 14:24:54 +01:00
Three directories `gfm` , `html` and `latex` are the destination directories for GFM, html and latex files respectively.
2021-01-14 02:02:41 +01:00
It is possible that these three directories don't exist before the conversion.
2020-12-21 13:12:05 +01:00
2021-01-13 12:33:33 +01:00
- src: This directory contains src.md files and C-related source files.
- image: This directory contains image files like png or jpg.
2021-04-20 14:36:12 +02:00
- gfm: `rake` converts src.md files to GFM files and store them in this directory.
- html: This directory is empty at first. `rake html` will convert src.md files to html files and store them in this directory.
- latex: This directory is empty at first. `rake latex` will convert src.md files to latex files and store them in this directory.
`rake pdf` creates pdf file in `latex` directory.
- doc: This directory contains `Readme_for_developers.md` (this file).
2021-01-13 12:33:33 +01:00
- lib: This directory includes ruby library files.
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
## Src directory and the top directory
2020-12-21 13:12:05 +01:00
2021-01-09 07:35:06 +01:00
Src directory contains src.md files and C-related source files.
2021-04-20 14:36:12 +02:00
The top directory, which is gtk\_tutorial directory, contains `Rakefile` , `src2md.rb` and some other files.
When `Readme.md` is generated, it will be located at the top directory.
`Readme.md` has title, abstract, table of contents and links to GFM files under `gfm` directory.
2021-01-11 15:32:09 +01:00
2021-01-14 02:02:41 +01:00
Rakefile describes how to convert src.md files into GFM files.
2021-04-20 14:36:12 +02:00
Rake carries out the conversion according to the `Rakefile` .
2021-01-11 15:32:09 +01:00
2021-01-13 05:29:35 +01:00
## The name of files in src directory
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
Each file in `src` directory is an abstract or section of the whole document.
A `abstract.src.md` contains the abstract of this tutorial.
Each section filename is "sec", number of the section and ".src.md" suffix.
2020-12-21 13:12:05 +01:00
For example, "sec1.src.md", "sec5.src.md" or "sec12.src.md".
They are the files correspond to section 1, section 5 and section 12 respectively.
2021-01-13 05:29:35 +01:00
## C source file directory
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
Most of Src.md files have `@@@include` commands and they include C source files.
Such C source files are located in the subdirectories of `src` directory.
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
Those C files have been compiled and tested.
When you compile source files, some auxiliary files and a target file like `a.out` are created.
Or `_build` directory is made when `meson` and `ninja` is used when compiling.
Those files are not tracked by `git` because they are specified in `.gitignore` .
2020-12-21 13:12:05 +01:00
The name of the subdirectories should be independent of section names.
It is because of renumbering, which will be explained in the next subsection.
2021-01-13 05:29:35 +01:00
## Renumbering
2020-12-21 13:12:05 +01:00
2021-04-20 14:36:12 +02:00
Sometimes you might want to insert a section.
For example, you want to insert it between section 4 and section 5.
2020-12-21 13:12:05 +01:00
You can make a temporary section 4.5, that is a rational number between 4 and 5.
2021-04-20 14:36:12 +02:00
However, section numbers are usually integer so section 4.5 must be changed to section 5.
And the numbers of the following sections must be increased by one.
2020-12-21 13:12:05 +01:00
2021-01-11 15:32:09 +01:00
This renumbering is done by a method `renum` of the class `Sec_files` .
2021-04-20 14:36:12 +02:00
The method and class is written in `lib/lib_sec_file.rb` .
2020-12-21 13:12:05 +01:00
- It changes file names.
- If there are references to sections in src.md files, the section numbers will be automatically renumbered.
## Rakefile
2021-01-09 07:35:06 +01:00
Rakefile is a similar file to Makefile but controlled by rake, which is a make-like program written in ruby.
2021-04-20 14:36:12 +02:00
Rakefile in this tutorial has the following tasks.
2020-12-21 13:12:05 +01:00
2021-01-14 02:02:41 +01:00
- md: generate GFM markdown files. This is the default.
2020-12-21 13:12:05 +01:00
- html: generate html files.
2021-04-20 14:36:12 +02:00
- pdf: generate latex files and a pdf file, which is compiled by lualatex.
2021-01-11 15:32:09 +01:00
- latex: generate latex files.
2020-12-21 13:12:05 +01:00
- all: generate md, html, latex and pdf files.
2021-01-11 15:32:09 +01:00
Rake does renumbering before the tasks above.
2020-12-21 13:12:05 +01:00
2021-01-14 02:02:41 +01:00
## Generate GFM markdown files
2021-01-09 07:35:06 +01:00
2021-01-11 15:32:09 +01:00
Markdown files (GFM) are generated by rake.
2021-01-09 07:35:06 +01:00
$ rake
2021-04-20 14:36:12 +02:00
This command generates `Readme.md` with `src/abstract.src.md` and titles of src.md files.
At the same time, it converts each src.md file into GFM file under `gfm` directory.
Navigation lines are added at the top and bottom of each markdown section file.
2021-01-09 07:35:06 +01:00
2021-04-20 14:36:12 +02:00
You can describe width and height of images in src.md files.
2021-01-11 15:32:09 +01:00
For example,
![sample image ](../image/sample_image.png ){width=10cm height=6cm}
2021-02-08 14:24:54 +01:00
The size between left brace and right brace is used in latex file and it is not fit to GFM syntax.
2021-04-20 14:36:12 +02:00
So the size is removed in the conversion.
2021-01-14 02:02:41 +01:00
If a src.md file has relative URL link, it will be changed by conversion.
Because src.md files are located under `src` directory and GFM files are located under `gfm` directory, base URL of GFM files is different from base URL of src.md files.
2021-04-20 14:36:12 +02:00
For example, `[src/sample.c](sample.c)` is translated to `[src/sample.c](../src/sample.c)` .
2021-01-14 02:02:41 +01:00
If a link points another src.md file, then the target filename will be changed to .md file.
For example, `[Section 5](sec5.src.md)` is translated to `[Section 5](sec5.md)` .
2021-01-11 15:32:09 +01:00
2021-01-09 07:35:06 +01:00
If you want to clean the directory, that means remove all the generated markdown files, type `rake clean` .
$ rake clean
2021-04-20 14:36:12 +02:00
If you see the github repository (ToshioCP/Gtk4-tutorial), `Readme.md` is shown below the code.
2021-01-09 07:35:06 +01:00
And `Readme.md` includes links to each markdown files.
2021-04-20 14:36:12 +02:00
The repository not only stores source files but also shows the whole tutorial.
2021-01-09 07:35:06 +01:00
2021-01-13 05:29:35 +01:00
## Generate html files
2021-01-09 07:35:06 +01:00
Src.md files can be translated to html files.
You need pandoc to do this.
Most linux distribution has pandoc package.
Refer to your distribution document to install it.
Type `rake html` to generate html files.
$ rake html
2021-01-11 15:32:09 +01:00
First, it generates pandoc's markdown files under `html` directory.
2021-01-13 12:33:33 +01:00
Then, pandoc converts them to html files.
2021-04-20 14:36:12 +02:00
The width and height of image files are removed.
2021-01-11 15:32:09 +01:00
2021-01-09 07:35:06 +01:00
`index.html` is the top html file.
If you want to clean `html` directory, type `rake cleanhtml`
$ rake cleanhtml
Every html file has stylesheet in its header.
2021-04-20 14:36:12 +02:00
This is created by `lib/lib_add_head_tail_html.rb` .
This script has a sample markdown code and convert it with pandoc and `-s` option.
Pandoc generates a html file with header.
The script extracts the header and use it for html files.
You can customize the style by modifying `lib/lib_add_head_tail_html.rb` .
2021-01-09 07:35:06 +01:00
2021-01-13 05:29:35 +01:00
## Generate latex files and a pdf file
2021-01-11 15:32:09 +01:00
Src.md files can be translated to latex files.
You need pandoc to do this.
Type `rake latex` to generate latex files.
$ rake latex
First, it generates pandoc's markdown files under `latex` directory.
2021-01-13 12:33:33 +01:00
Then, pandoc converts them to latex files.
2021-01-11 15:32:09 +01:00
Links to files or directories are removed because latex doesn't support them.
However, links to full URL are kept.
2021-04-20 14:36:12 +02:00
Image size is set with the size between the left brace and right brace.
![sample image ](../image/sample_image.png ){width=10cm height=6cm}
You need to specify appropriate width and height.
It is almost `0.015 x pixels` cm.
For example, if the width of an image is 400 pixels, the width in a latex file will be almost 6cm.
2021-01-11 15:32:09 +01:00
`main.tex` is the top latex file.
If you want to clean `latex` directory, type `rake cleanlatex`
$ rake cleanlatex
2021-04-20 14:36:12 +02:00
`main.tex` is a root file and it includes each section file between `\begin{document}` and `\end{document}` .
`main.tex` also includes `helper.tex` in its preamble.
`main.tex` and `helper.tex` is created by `lib/lib_gen_main_tex.rb` .
It has a sample markdown code and convert it witn pandoc and `-s` option.
Pandoc generates preamble.
`lib/lib_gen_main_tx.rb` extracts the preamble and put a part of it into `helper.tex` .
You can customize `helper.tex` by modifying `lib/lib_gen_main_tex.rb` .
2021-01-11 15:32:09 +01:00
You can generate pdf file by typing `rake pdf` .
$ rake pdf
2021-04-20 14:36:12 +02:00
This does `rake latex` first.
After that the latex files are compiled by lualatex.