slw2gmi

Конвертор из slweb-а у Џемини
Дневник | Датотеке | Референце | ПРОЧИТАЈМЕ | ЛИЦЕНЦА

чување 49a81a44b54176a392f0b70cd60c808d5ac06458
Аутор: Страхиња Радић <contact@strahinja.org>
Датум:   Sun, 21 Mar 2021 22:02:44 +0100

Initial commit

Signed-off-by: Страхиња Радић <contact@strahinja.org>

Diffstat:
A.gitignore | 22++++++++++++++++++++++
ALICENSE | 674+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
AREADME | 57+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ATODO | 8++++++++
Aall.do | 3+++
Aclean.do | 3+++
Adate.do | 4++++
Adefault.do | 11+++++++++++
Adefault.gz.do | 3+++
Adefault.o.do | 3+++
Adefault.pdf.do | 3+++
Adefs.h | 137+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ado | 446+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Ainstall.do | 10++++++++++
Arebuild.do | 3+++
Aslw2gmi.1.in | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aslw2gmi.c | 4016+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aslw2gmi.do | 3+++
Auninstall.do | 7+++++++
Aversion.do | 7+++++++
измењених датотека: 20, додавања: 5512(+), брисања: 0(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,22 @@ +.cache +.redo +*.session +.do_built +.do_built.dir +*~ +*.bak +*.orig +*.rej +*.o +*.pdf +*.ps +*.swp +*.did +nohup.out +*.gz +slw2gmi +slw2gmi.1 +compile_commands.json +date +version + diff --git a/LICENSE b/LICENSE @@ -0,0 +1,674 @@ + GNU GENERAL PUBLIC LICENSE + Version 3, 29 June 2007 + + Copyright (C) 2007 Free Software Foundation, Inc. <https://fsf.org/> + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + + Preamble + + The GNU General Public License is a free, copyleft license for +software and other kinds of works. + + The licenses for most software and other practical works are designed +to take away your freedom to share and change the works. By contrast, +the GNU General Public License is intended to guarantee your freedom to +share and change all versions of a program--to make sure it remains free +software for all its users. We, the Free Software Foundation, use the +GNU General Public License for most of our software; it applies also to +any other work released this way by its authors. You can apply it to +your programs, too. + + When we speak of free software, we are referring to freedom, not +price. Our General Public Licenses are designed to make sure that you +have the freedom to distribute copies of free software (and charge for +them if you wish), that you receive source code or can get it if you +want it, that you can change the software or use pieces of it in new +free programs, and that you know you can do these things. + + To protect your rights, we need to prevent others from denying you +these rights or asking you to surrender the rights. Therefore, you have +certain responsibilities if you distribute copies of the software, or if +you modify it: responsibilities to respect the freedom of others. + + For example, if you distribute copies of such a program, whether +gratis or for a fee, you must pass on to the recipients the same +freedoms that you received. You must make sure that they, too, receive +or can get the source code. And you must show them these terms so they +know their rights. + + Developers that use the GNU GPL protect your rights with two steps: +(1) assert copyright on the software, and (2) offer you this License +giving you legal permission to copy, distribute and/or modify it. + + For the developers' and authors' protection, the GPL clearly explains +that there is no warranty for this free software. For both users' and +authors' sake, the GPL requires that modified versions be marked as +changed, so that their problems will not be attributed erroneously to +authors of previous versions. + + Some devices are designed to deny users access to install or run +modified versions of the software inside them, although the manufacturer +can do so. This is fundamentally incompatible with the aim of +protecting users' freedom to change the software. The systematic +pattern of such abuse occurs in the area of products for individuals to +use, which is precisely where it is most unacceptable. Therefore, we +have designed this version of the GPL to prohibit the practice for those +products. If such problems arise substantially in other domains, we +stand ready to extend this provision to those domains in future versions +of the GPL, as needed to protect the freedom of users. + + Finally, every program is threatened constantly by software patents. +States should not allow patents to restrict development and use of +software on general-purpose computers, but in those that do, we wish to +avoid the special danger that patents applied to a free program could +make it effectively proprietary. To prevent this, the GPL assures that +patents cannot be used to render the program non-free. + + The precise terms and conditions for copying, distribution and +modification follow. + + TERMS AND CONDITIONS + + 0. Definitions. + + "This License" refers to version 3 of the GNU General Public License. + + "Copyright" also means copyright-like laws that apply to other kinds of +works, such as semiconductor masks. + + "The Program" refers to any copyrightable work licensed under this +License. Each licensee is addressed as "you". "Licensees" and +"recipients" may be individuals or organizations. + + To "modify" a work means to copy from or adapt all or part of the work +in a fashion requiring copyright permission, other than the making of an +exact copy. The resulting work is called a "modified version" of the +earlier work or a work "based on" the earlier work. + + A "covered work" means either the unmodified Program or a work based +on the Program. + + To "propagate" a work means to do anything with it that, without +permission, would make you directly or secondarily liable for +infringement under applicable copyright law, except executing it on a +computer or modifying a private copy. Propagation includes copying, +distribution (with or without modification), making available to the +public, and in some countries other activities as well. + + To "convey" a work means any kind of propagation that enables other +parties to make or receive copies. Mere interaction with a user through +a computer network, with no transfer of a copy, is not conveying. + + An interactive user interface displays "Appropriate Legal Notices" +to the extent that it includes a convenient and prominently visible +feature that (1) displays an appropriate copyright notice, and (2) +tells the user that there is no warranty for the work (except to the +extent that warranties are provided), that licensees may convey the +work under this License, and how to view a copy of this License. If +the interface presents a list of user commands or options, such as a +menu, a prominent item in the list meets this criterion. + + 1. Source Code. + + The "source code" for a work means the preferred form of the work +for making modifications to it. "Object code" means any non-source +form of a work. + + A "Standard Interface" means an interface that either is an official +standard defined by a recognized standards body, or, in the case of +interfaces specified for a particular programming language, one that +is widely used among developers working in that language. + + The "System Libraries" of an executable work include anything, other +than the work as a whole, that (a) is included in the normal form of +packaging a Major Component, but which is not part of that Major +Component, and (b) serves only to enable use of the work with that +Major Component, or to implement a Standard Interface for which an +implementation is available to the public in source code form. A +"Major Component", in this context, means a major essential component +(kernel, window system, and so on) of the specific operating system +(if any) on which the executable work runs, or a compiler used to +produce the work, or an object code interpreter used to run it. + + The "Corresponding Source" for a work in object code form means all +the source code needed to generate, install, and (for an executable +work) run the object code and to modify the work, including scripts to +control those activities. However, it does not include the work's +System Libraries, or general-purpose tools or generally available free +programs which are used unmodified in performing those activities but +which are not part of the work. For example, Corresponding Source +includes interface definition files associated with source files for +the work, and the source code for shared libraries and dynamically +linked subprograms that the work is specifically designed to require, +such as by intimate data communication or control flow between those +subprograms and other parts of the work. + + The Corresponding Source need not include anything that users +can regenerate automatically from other parts of the Corresponding +Source. + + The Corresponding Source for a work in source code form is that +same work. + + 2. Basic Permissions. + + All rights granted under this License are granted for the term of +copyright on the Program, and are irrevocable provided the stated +conditions are met. This License explicitly affirms your unlimited +permission to run the unmodified Program. The output from running a +covered work is covered by this License only if the output, given its +content, constitutes a covered work. This License acknowledges your +rights of fair use or other equivalent, as provided by copyright law. + + You may make, run and propagate covered works that you do not +convey, without conditions so long as your license otherwise remains +in force. You may convey covered works to others for the sole purpose +of having them make modifications exclusively for you, or provide you +with facilities for running those works, provided that you comply with +the terms of this License in conveying all material for which you do +not control copyright. Those thus making or running the covered works +for you must do so exclusively on your behalf, under your direction +and control, on terms that prohibit them from making any copies of +your copyrighted material outside their relationship with you. + + Conveying under any other circumstances is permitted solely under +the conditions stated below. Sublicensing is not allowed; section 10 +makes it unnecessary. + + 3. Protecting Users' Legal Rights From Anti-Circumvention Law. + + No covered work shall be deemed part of an effective technological +measure under any applicable law fulfilling obligations under article +11 of the WIPO copyright treaty adopted on 20 December 1996, or +similar laws prohibiting or restricting circumvention of such +measures. + + When you convey a covered work, you waive any legal power to forbid +circumvention of technological measures to the extent such circumvention +is effected by exercising rights under this License with respect to +the covered work, and you disclaim any intention to limit operation or +modification of the work as a means of enforcing, against the work's +users, your or third parties' legal rights to forbid circumvention of +technological measures. + + 4. Conveying Verbatim Copies. + + You may convey verbatim copies of the Program's source code as you +receive it, in any medium, provided that you conspicuously and +appropriately publish on each copy an appropriate copyright notice; +keep intact all notices stating that this License and any +non-permissive terms added in accord with section 7 apply to the code; +keep intact all notices of the absence of any warranty; and give all +recipients a copy of this License along with the Program. + + You may charge any price or no price for each copy that you convey, +and you may offer support or warranty protection for a fee. + + 5. Conveying Modified Source Versions. + + You may convey a work based on the Program, or the modifications to +produce it from the Program, in the form of source code under the +terms of section 4, provided that you also meet all of these conditions: + + a) The work must carry prominent notices stating that you modified + it, and giving a relevant date. + + b) The work must carry prominent notices stating that it is + released under this License and any conditions added under section + 7. This requirement modifies the requirement in section 4 to + "keep intact all notices". + + c) You must license the entire work, as a whole, under this + License to anyone who comes into possession of a copy. This + License will therefore apply, along with any applicable section 7 + additional terms, to the whole of the work, and all its parts, + regardless of how they are packaged. This License gives no + permission to license the work in any other way, but it does not + invalidate such permission if you have separately received it. + + d) If the work has interactive user interfaces, each must display + Appropriate Legal Notices; however, if the Program has interactive + interfaces that do not display Appropriate Legal Notices, your + work need not make them do so. + + A compilation of a covered work with other separate and independent +works, which are not by their nature extensions of the covered work, +and which are not combined with it such as to form a larger program, +in or on a volume of a storage or distribution medium, is called an +"aggregate" if the compilation and its resulting copyright are not +used to limit the access or legal rights of the compilation's users +beyond what the individual works permit. Inclusion of a covered work +in an aggregate does not cause this License to apply to the other +parts of the aggregate. + + 6. Conveying Non-Source Forms. + + You may convey a covered work in object code form under the terms +of sections 4 and 5, provided that you also convey the +machine-readable Corresponding Source under the terms of this License, +in one of these ways: + + a) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by the + Corresponding Source fixed on a durable physical medium + customarily used for software interchange. + + b) Convey the object code in, or embodied in, a physical product + (including a physical distribution medium), accompanied by a + written offer, valid for at least three years and valid for as + long as you offer spare parts or customer support for that product + model, to give anyone who possesses the object code either (1) a + copy of the Corresponding Source for all the software in the + product that is covered by this License, on a durable physical + medium customarily used for software interchange, for a price no + more than your reasonable cost of physically performing this + conveying of source, or (2) access to copy the + Corresponding Source from a network server at no charge. + + c) Convey individual copies of the object code with a copy of the + written offer to provide the Corresponding Source. This + alternative is allowed only occasionally and noncommercially, and + only if you received the object code with such an offer, in accord + with subsection 6b. + + d) Convey the object code by offering access from a designated + place (gratis or for a charge), and offer equivalent access to the + Corresponding Source in the same way through the same place at no + further charge. You need not require recipients to copy the + Corresponding Source along with the object code. If the place to + copy the object code is a network server, the Corresponding Source + may be on a different server (operated by you or a third party) + that supports equivalent copying facilities, provided you maintain + clear directions next to the object code saying where to find the + Corresponding Source. Regardless of what server hosts the + Corresponding Source, you remain obligated to ensure that it is + available for as long as needed to satisfy these requirements. + + e) Convey the object code using peer-to-peer transmission, provided + you inform other peers where the object code and Corresponding + Source of the work are being offered to the general public at no + charge under subsection 6d. + + A separable portion of the object code, whose source code is excluded +from the Corresponding Source as a System Library, need not be +included in conveying the object code work. + + A "User Product" is either (1) a "consumer product", which means any +tangible personal property which is normally used for personal, family, +or household purposes, or (2) anything designed or sold for incorporation +into a dwelling. In determining whether a product is a consumer product, +doubtful cases shall be resolved in favor of coverage. For a particular +product received by a particular user, "normally used" refers to a +typical or common use of that class of product, regardless of the status +of the particular user or of the way in which the particular user +actually uses, or expects or is expected to use, the product. A product +is a consumer product regardless of whether the product has substantial +commercial, industrial or non-consumer uses, unless such uses represent +the only significant mode of use of the product. + + "Installation Information" for a User Product means any methods, +procedures, authorization keys, or other information required to install +and execute modified versions of a covered work in that User Product from +a modified version of its Corresponding Source. The information must +suffice to ensure that the continued functioning of the modified object +code is in no case prevented or interfered with solely because +modification has been made. + + If you convey an object code work under this section in, or with, or +specifically for use in, a User Product, and the conveying occurs as +part of a transaction in which the right of possession and use of the +User Product is transferred to the recipient in perpetuity or for a +fixed term (regardless of how the transaction is characterized), the +Corresponding Source conveyed under this section must be accompanied +by the Installation Information. But this requirement does not apply +if neither you nor any third party retains the ability to install +modified object code on the User Product (for example, the work has +been installed in ROM). + + The requirement to provide Installation Information does not include a +requirement to continue to provide support service, warranty, or updates +for a work that has been modified or installed by the recipient, or for +the User Product in which it has been modified or installed. Access to a +network may be denied when the modification itself materially and +adversely affects the operation of the network or violates the rules and +protocols for communication across the network. + + Corresponding Source conveyed, and Installation Information provided, +in accord with this section must be in a format that is publicly +documented (and with an implementation available to the public in +source code form), and must require no special password or key for +unpacking, reading or copying. + + 7. Additional Terms. + + "Additional permissions" are terms that supplement the terms of this +License by making exceptions from one or more of its conditions. +Additional permissions that are applicable to the entire Program shall +be treated as though they were included in this License, to the extent +that they are valid under applicable law. If additional permissions +apply only to part of the Program, that part may be used separately +under those permissions, but the entire Program remains governed by +this License without regard to the additional permissions. + + When you convey a copy of a covered work, you may at your option +remove any additional permissions from that copy, or from any part of +it. (Additional permissions may be written to require their own +removal in certain cases when you modify the work.) You may place +additional permissions on material, added by you to a covered work, +for which you have or can give appropriate copyright permission. + + Notwithstanding any other provision of this License, for material you +add to a covered work, you may (if authorized by the copyright holders of +that material) supplement the terms of this License with terms: + + a) Disclaiming warranty or limiting liability differently from the + terms of sections 15 and 16 of this License; or + + b) Requiring preservation of specified reasonable legal notices or + author attributions in that material or in the Appropriate Legal + Notices displayed by works containing it; or + + c) Prohibiting misrepresentation of the origin of that material, or + requiring that modified versions of such material be marked in + reasonable ways as different from the original version; or + + d) Limiting the use for publicity purposes of names of licensors or + authors of the material; or + + e) Declining to grant rights under trademark law for use of some + trade names, trademarks, or service marks; or + + f) Requiring indemnification of licensors and authors of that + material by anyone who conveys the material (or modified versions of + it) with contractual assumptions of liability to the recipient, for + any liability that these contractual assumptions directly impose on + those licensors and authors. + + All other non-permissive additional terms are considered "further +restrictions" within the meaning of section 10. If the Program as you +received it, or any part of it, contains a notice stating that it is +governed by this License along with a term that is a further +restriction, you may remove that term. If a license document contains +a further restriction but permits relicensing or conveying under this +License, you may add to a covered work material governed by the terms +of that license document, provided that the further restriction does +not survive such relicensing or conveying. + + If you add terms to a covered work in accord with this section, you +must place, in the relevant source files, a statement of the +additional terms that apply to those files, or a notice indicating +where to find the applicable terms. + + Additional terms, permissive or non-permissive, may be stated in the +form of a separately written license, or stated as exceptions; +the above requirements apply either way. + + 8. Termination. + + You may not propagate or modify a covered work except as expressly +provided under this License. Any attempt otherwise to propagate or +modify it is void, and will automatically terminate your rights under +this License (including any patent licenses granted under the third +paragraph of section 11). + + However, if you cease all violation of this License, then your +license from a particular copyright holder is reinstated (a) +provisionally, unless and until the copyright holder explicitly and +finally terminates your license, and (b) permanently, if the copyright +holder fails to notify you of the violation by some reasonable means +prior to 60 days after the cessation. + + Moreover, your license from a particular copyright holder is +reinstated permanently if the copyright holder notifies you of the +violation by some reasonable means, this is the first time you have +received notice of violation of this License (for any work) from that +copyright holder, and you cure the violation prior to 30 days after +your receipt of the notice. + + Termination of your rights under this section does not terminate the +licenses of parties who have received copies or rights from you under +this License. If your rights have been terminated and not permanently +reinstated, you do not qualify to receive new licenses for the same +material under section 10. + + 9. Acceptance Not Required for Having Copies. + + You are not required to accept this License in order to receive or +run a copy of the Program. Ancillary propagation of a covered work +occurring solely as a consequence of using peer-to-peer transmission +to receive a copy likewise does not require acceptance. However, +nothing other than this License grants you permission to propagate or +modify any covered work. These actions infringe copyright if you do +not accept this License. Therefore, by modifying or propagating a +covered work, you indicate your acceptance of this License to do so. + + 10. Automatic Licensing of Downstream Recipients. + + Each time you convey a covered work, the recipient automatically +receives a license from the original licensors, to run, modify and +propagate that work, subject to this License. You are not responsible +for enforcing compliance by third parties with this License. + + An "entity transaction" is a transaction transferring control of an +organization, or substantially all assets of one, or subdividing an +organization, or merging organizations. If propagation of a covered +work results from an entity transaction, each party to that +transaction who receives a copy of the work also receives whatever +licenses to the work the party's predecessor in interest had or could +give under the previous paragraph, plus a right to possession of the +Corresponding Source of the work from the predecessor in interest, if +the predecessor has it or can get it with reasonable efforts. + + You may not impose any further restrictions on the exercise of the +rights granted or affirmed under this License. For example, you may +not impose a license fee, royalty, or other charge for exercise of +rights granted under this License, and you may not initiate litigation +(including a cross-claim or counterclaim in a lawsuit) alleging that +any patent claim is infringed by making, using, selling, offering for +sale, or importing the Program or any portion of it. + + 11. Patents. + + A "contributor" is a copyright holder who authorizes use under this +License of the Program or a work on which the Program is based. The +work thus licensed is called the contributor's "contributor version". + + A contributor's "essential patent claims" are all patent claims +owned or controlled by the contributor, whether already acquired or +hereafter acquired, that would be infringed by some manner, permitted +by this License, of making, using, or selling its contributor version, +but do not include claims that would be infringed only as a +consequence of further modification of the contributor version. For +purposes of this definition, "control" includes the right to grant +patent sublicenses in a manner consistent with the requirements of +this License. + + Each contributor grants you a non-exclusive, worldwide, royalty-free +patent license under the contributor's essential patent claims, to +make, use, sell, offer for sale, import and otherwise run, modify and +propagate the contents of its contributor version. + + In the following three paragraphs, a "patent license" is any express +agreement or commitment, however denominated, not to enforce a patent +(such as an express permission to practice a patent or covenant not to +sue for patent infringement). To "grant" such a patent license to a +party means to make such an agreement or commitment not to enforce a +patent against the party. + + If you convey a covered work, knowingly relying on a patent license, +and the Corresponding Source of the work is not available for anyone +to copy, free of charge and under the terms of this License, through a +publicly available network server or other readily accessible means, +then you must either (1) cause the Corresponding Source to be so +available, or (2) arrange to deprive yourself of the benefit of the +patent license for this particular work, or (3) arrange, in a manner +consistent with the requirements of this License, to extend the patent +license to downstream recipients. "Knowingly relying" means you have +actual knowledge that, but for the patent license, your conveying the +covered work in a country, or your recipient's use of the covered work +in a country, would infringe one or more identifiable patents in that +country that you have reason to believe are valid. + + If, pursuant to or in connection with a single transaction or +arrangement, you convey, or propagate by procuring conveyance of, a +covered work, and grant a patent license to some of the parties +receiving the covered work authorizing them to use, propagate, modify +or convey a specific copy of the covered work, then the patent license +you grant is automatically extended to all recipients of the covered +work and works based on it. + + A patent license is "discriminatory" if it does not include within +the scope of its coverage, prohibits the exercise of, or is +conditioned on the non-exercise of one or more of the rights that are +specifically granted under this License. You may not convey a covered +work if you are a party to an arrangement with a third party that is +in the business of distributing software, under which you make payment +to the third party based on the extent of your activity of conveying +the work, and under which the third party grants, to any of the +parties who would receive the covered work from you, a discriminatory +patent license (a) in connection with copies of the covered work +conveyed by you (or copies made from those copies), or (b) primarily +for and in connection with specific products or compilations that +contain the covered work, unless you entered into that arrangement, +or that patent license was granted, prior to 28 March 2007. + + Nothing in this License shall be construed as excluding or limiting +any implied license or other defenses to infringement that may +otherwise be available to you under applicable patent law. + + 12. No Surrender of Others' Freedom. + + If conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot convey a +covered work so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you may +not convey it at all. For example, if you agree to terms that obligate you +to collect a royalty for further conveying from those to whom you convey +the Program, the only way you could satisfy both those terms and this +License would be to refrain entirely from conveying the Program. + + 13. Use with the GNU Affero General Public License. + + Notwithstanding any other provision of this License, you have +permission to link or combine any covered work with a work licensed +under version 3 of the GNU Affero General Public License into a single +combined work, and to convey the resulting work. The terms of this +License will continue to apply to the part which is the covered work, +but the special requirements of the GNU Affero General Public License, +section 13, concerning interaction through a network will apply to the +combination as such. + + 14. Revised Versions of this License. + + The Free Software Foundation may publish revised and/or new versions of +the GNU General Public License from time to time. Such new versions will +be similar in spirit to the present version, but may differ in detail to +address new problems or concerns. + + Each version is given a distinguishing version number. If the +Program specifies that a certain numbered version of the GNU General +Public License "or any later version" applies to it, you have the +option of following the terms and conditions either of that numbered +version or of any later version published by the Free Software +Foundation. If the Program does not specify a version number of the +GNU General Public License, you may choose any version ever published +by the Free Software Foundation. + + If the Program specifies that a proxy can decide which future +versions of the GNU General Public License can be used, that proxy's +public statement of acceptance of a version permanently authorizes you +to choose that version for the Program. + + Later license versions may give you additional or different +permissions. However, no additional obligations are imposed on any +author or copyright holder as a result of your choosing to follow a +later version. + + 15. Disclaimer of Warranty. + + THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY +APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT +HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY +OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, +THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM +IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF +ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. Limitation of Liability. + + IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING +WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS +THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY +GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE +USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF +DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD +PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), +EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF +SUCH DAMAGES. + + 17. Interpretation of Sections 15 and 16. + + If the disclaimer of warranty and limitation of liability provided +above cannot be given local legal effect according to their terms, +reviewing courts shall apply local law that most closely approximates +an absolute waiver of all civil liability in connection with the +Program, unless a warranty or assumption of liability accompanies a +copy of the Program in return for a fee. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Programs + + If you develop a new program, and you want it to be of the greatest +possible use to the public, the best way to achieve this is to make it +free software which everyone can redistribute and change under these terms. + + To do so, attach the following notices to the program. It is safest +to attach them to the start of each source file to most effectively +state the exclusion of warranty; and each file should have at least +the "copyright" line and a pointer to where the full notice is found. + + <one line to give the program's name and a brief idea of what it does.> + Copyright (C) <year> <name of author> + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +Also add information on how to contact you by electronic and paper mail. + + If the program does terminal interaction, make it output a short +notice like this when it starts in an interactive mode: + + <program> Copyright (C) <year> <name of author> + This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. + This is free software, and you are welcome to redistribute it + under certain conditions; type `show c' for details. + +The hypothetical commands `show w' and `show c' should show the appropriate +parts of the General Public License. Of course, your program's commands +might be different; for a GUI interface, you would use an "about box". + + You should also get your employer (if you work as a programmer) or school, +if any, to sign a "copyright disclaimer" for the program, if necessary. +For more information on this, and how to apply and follow the GNU GPL, see +<https://www.gnu.org/licenses/>. + + The GNU General Public License does not permit incorporating your program +into proprietary programs. If your program is a subroutine library, you +may consider it more useful to permit linking proprietary applications with +the library. If this is what you want to do, use the GNU Lesser General +Public License instead of this License. But first, please read +<https://www.gnu.org/licenses/why-not-lgpl.html>. diff --git a/README b/README @@ -0,0 +1,57 @@ + + slw2gmi + ======= + +slw2gmi is a converter from slweb[1] to Gemini[2] format. Although both are +similar to Markdown, the two formats differ enough to warrant a full converter. + +[1]: https://strahinja.srht.site/slweb/index.html +[2]: https://gemini.circumlunar.space + + Prerequisites + ------------- + +Aside from the obvious (a C compiler, by default GNU C), slw2gmi requires GNU +libunistring[3], realpath(1) to determine paths in local links and groff(1) and +gzip(1) to create and compress documentation. git(1) is, aside from cloning the +repository, required to use the directive {git-log}. + +[3]: https://www.gnu.org/software/libunistring/ + + + Install + ------- + +$ git clone https://git.sr.ht/~strahinja/slw2gmi +$ cd slw2gmi +$ su + + Then, if you have djb redo: + +# redo install + + if you don't: + +# ./do install + + + License + ------- + + slw2gmi - slweb to Gemini converter + Copyright (C) 2021 Страхиња Радић + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + + diff --git a/TODO b/TODO @@ -0,0 +1,8 @@ + + TODO + ==== + + [ ] Fix leaks + [ ] Fix leaks in slw2gmi + [ ] Propagate the fixes to slweb + diff --git a/all.do b/all.do @@ -0,0 +1,3 @@ +redo-ifchange slw2gmi +redo-ifchange slw2gmi.1.gz slw2gmi.pdf + diff --git a/clean.do b/clean.do @@ -0,0 +1,3 @@ +redo-always +rm -f slw2gmi slw2gmi.1 slw2gmi.1.gz *.o *~ *.pdf + diff --git a/date.do b/date.do @@ -0,0 +1,4 @@ +LC_ALL=C date +'%d %b %Y' >$3 +redo-always +redo-stamp <$3 + diff --git a/default.do b/default.do @@ -0,0 +1,11 @@ +if [ -r $1.in ]; then + redo-ifchange $1.in version date + read VERSION <version + read DATE <date + sed -e "s/%VERSION%/$VERSION/g" \ + -e "s/%DATE%/$DATE/g" <$1.in +else + echo "$0: don't know how to build '$1'" >&2 + exit 99 +fi + diff --git a/default.gz.do b/default.gz.do @@ -0,0 +1,3 @@ +redo-ifchange $2 +gzip -cf $2 >$3 + diff --git a/default.o.do b/default.o.do @@ -0,0 +1,3 @@ +redo-ifchange $2.c +${SLW2GMI_CC:-gcc} -g -Wall -std=c99 -o $3 -c $2.c + diff --git a/default.pdf.do b/default.pdf.do @@ -0,0 +1,3 @@ +redo-ifchange $2.1 +groff -mandoc -t -T pdf $2.1 >$3 + diff --git a/defs.h b/defs.h @@ -0,0 +1,137 @@ +/* + * slw2gmi - slweb to Gemini converter + * Copyright (C) 2021 Страхиња Радић + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +#ifndef __DEFS_H +#define __DEFS_H + +#define _POSIX_C_SOURCE 200809L + +#include <dirent.h> +#include <errno.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdint.h> +#include <string.h> +#include <sys/prctl.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <sys/wait.h> +#include <unistd.h> +#include <unistr.h> +#include <unistdio.h> +#include <uniwidth.h> + +#define PROGRAMNAME "slw2gmi" +#define VERSION "0.1.0" +#define COPYRIGHTYEAR "2021" +#define MADEBY_URL "https://strahinja.srht.site/slw2gmi/index.html" + +#define BUFSIZE 1024 +#define KEYSIZE 256 +#define SMALL_ARGSIZE 256 +#define DATEBUFSIZE 12 + +static const char timestamp_format[] = "d.m.y"; +static const char timestamp_output_ext[] = ".html"; + +static const char CMD_GIT_LOG[] = "xargs"; +static const char* CMD_GIT_LOG_ARGS[] + = { "xargs", "-I{}", "git", "log", "-1", + "--pretty=format:{} %h %ci (%cn) %d", NULL }; + +typedef enum +{ + FALSE = 0, + TRUE = 1 +} BOOL; + +typedef unsigned char UBYTE; +typedef unsigned long ULONG; + +typedef enum +{ + CMD_NONE, + CMD_BODY_ONLY, + CMD_BASEDIR, + CMD_HELP, + CMD_VERSION +} Command; + +typedef struct +{ + uint8_t* key; + uint8_t* value; + size_t value_size; + BOOL seen; /* for use with macros */ +} KeyValue; + +typedef int (*csv_callback_t)(FILE* output, uint8_t** csv_header, uint8_t** csv_register); + +#pragma GCC diagnostic push +#pragma GCC diagnostic ignored "-Wunused-const-variable" +#define MAX_HEADING_LEVEL 4 +#define MAX_CSV_REGISTERS 9 + +#define ST_NONE 0 +#define ST_YAML 1 +#define ST_YAML_VAL (1 << 1) +#define ST_PARA_OPEN (1 << 2) +#define ST_TAG (1 << 3) +#define ST_HEADING (1 << 4) +#define ST_HEADING_TEXT (1 << 5) +#define ST_BOLD (1 << 6) +#define ST_ITALIC (1 << 7) +#define ST_PRE (1 << 8) +#define ST_CODE (1 << 9) +#define ST_BLOCKQUOTE (1 << 10) +#define ST_LINK (1 << 11) +#define ST_LINK_SECOND_ARG (1 << 12) +#define ST_LINK_SECOND_ARG_END (1 << 13) +#define ST_LINK_SPAN (1 << 14) +#define ST_LINK_MACRO (1 << 15) +#define ST_IMAGE (1 << 16) +#define ST_IMAGE_SECOND_ARG (1 << 17) +#define ST_MACRO_BODY (1 << 18) +#define ST_CSV_BODY (1 << 19) +#define ST_HTML_TAG (1 << 20) +#define ST_KBD (1 << 21) +#define ST_LIST (1 << 22) +#define ST_NUMLIST (1 << 23) +#define ST_FOOTNOTE (1 << 24) +#define ST_FOOTNOTE_TEXT (1 << 25) +#define ST_INLINE_FOOTNOTE (1 << 26) +#define ST_FORMULA (1 << 27) +#define ST_DISPLAY_FORMULA (1 << 28) +#define ST_TABLE_HEADER (1 << 29) +#define ST_TABLE_LINE (1 << 30) +#define ST_TABLE (1 << 31) + +#define ST_CS_NONE 0 +#define ST_CS_HEADER 1 +#define ST_CS_REGISTER (1 << 2) +#define ST_CS_QUOTE (1 << 3) +#define ST_CS_COND (1 << 4) +#define ST_CS_COND_NONEMPTY (1 << 5) +#define ST_CS_COND_EMPTY (1 << 6) +#define ST_CS_ESCAPE (1 << 7) +#pragma GCC diagnostic pop + +#endif /* __DEFS_H */ + diff --git a/do b/do @@ -0,0 +1,446 @@ +#!/usr/bin/env sh +# +# A minimal alternative to djb redo that doesn't support incremental builds. +# For the full version, visit http://github.com/apenwarr/redo +# +# The author disclaims copyright to this source file and hereby places it in +# the public domain. (2010 12 14; updated 2019 02 24) +# +USAGE=" +usage: do [-d] [-x] [-v] [-c] <targets...> + -d print extra debug messages (mostly about dependency checks) + -v run .do files with 'set -v' + -x run .do files with 'set -x' + -c clean up all old targets before starting + + Note: do is an implementation of redo that does *not* check dependencies. + It will never rebuild a target it has already built, unless you use -c. +" + +# CDPATH apparently causes unexpected 'cd' output on some platforms. +unset CDPATH + +# By default, no output coloring. +green="" +bold="" +plain="" + +if [ -n "$TERM" -a "$TERM" != "dumb" ] && tty <&2 >/dev/null 2>&1; then + green="$(printf '\033[32m')" + bold="$(printf '\033[1m')" + plain="$(printf '\033[m')" +fi + +# The 'seq' command is not available on all platforms. +_seq() { + local x=0 max="$1" + while [ "$x" -lt "$max" ]; do + x=$((x + 1)) + echo "$x" + done +} + +# Split $1 into a dir part ($_dirsplit_dir) and base filename ($_dirsplit_base) +_dirsplit() { + _dirsplit_base=${1##*/} + _dirsplit_dir=${1%$_dirsplit_base} +} + +# Like /usr/bin/dirname, but avoids a fork and uses _dirsplit semantics. +qdirname() ( + _dirsplit "$1" + dir=${_dirsplit_dir%/} + echo "${dir:-.}" +) + +_dirsplit "$0" +REDO=$(cd "$(pwd -P)" && + cd "${_dirsplit_dir:-.}" && + echo "$PWD/$_dirsplit_base") +export REDO +_cmd=$_dirsplit_base + +DO_TOP= +if [ -z "$DO_BUILT" ]; then + export _do_opt_debug= + export _do_opt_exec= + export _do_opt_verbose= + export _do_opt_clean= +fi +while getopts 'dxvcj:h?' _opt; do + case $_opt in + d) _do_opt_debug=1 ;; + x) _do_opt_exec=x ;; + v) _do_opt_verbose=v ;; + c) _do_opt_clean=1 ;; + j) ;; # silently ignore, for compat with real redo + \?|h|*) printf "%s" "$USAGE" >&2 + exit 99 + ;; + esac +done +shift "$((OPTIND - 1))" +_debug() { + [ -z "$_do_opt_debug" ] || echo "$@" >&2 +} + +if [ -z "$DO_BUILT" -a "$_cmd" != "redo-whichdo" ]; then + DO_TOP=1 + if [ "$#" -eq 0 ] && [ "$_cmd" = "do" -o "$_cmd" = "redo" ]; then + set all # only toplevel redo has a default target + fi + export DO_STARTDIR="$(pwd -P)" + # If starting /bin/pwd != $PWD, this will fix it. + # That can happen when $PWD contains symlinks that the shell is + # trying helpfully (but unsuccessfully) to hide from the user. + cd "$DO_STARTDIR" || exit 99 + export DO_BUILT="$PWD/.do_built" + if [ -z "$_do_opt_clean" -a -e "$DO_BUILT" ]; then + echo "do: Incremental mode. Use -c for clean rebuild." >&2 + fi + : >>"$DO_BUILT" + sort -u "$DO_BUILT" >"$DO_BUILT.new" + while read f; do + [ -n "$_do_opt_clean" ] && printf "%s\0%s.did\0" "$f" "$f" + printf "%s.did.tmp\0" "$f" + done <"$DO_BUILT.new" | + xargs -0 rm -f 2>/dev/null + mv "$DO_BUILT.new" "$DO_BUILT" + export DO_PATH="$DO_BUILT.dir" + export PATH="$DO_PATH:$PATH" + rm -rf "$DO_PATH" + mkdir "$DO_PATH" + for d in redo redo-ifchange redo-whichdo; do + ln -s "$REDO" "$DO_PATH/$d" + done + for d in redo-ifcreate redo-stamp redo-always redo-ood \ + redo-targets redo-sources; do + echo "#!/bin/sh" >"$DO_PATH/$d" + chmod a+rx "$DO_PATH/$d" + done +fi + + +# Chop the "file" part off a /path/to/file pathname. +# Note that if the filename already ends in a /, we just remove the slash. +_updir() +{ + local v="${1%/*}" + [ "$v" != "$1" ] && echo "$v" + # else "empty" which means we went past the root +} + + +# Returns true if $1 starts with $2. +_startswith() +{ + [ "${1#"$2"}" != "$1" ] +} + + +# Returns true if $1 ends with $2. +_endswith() +{ + [ "${1%"$2"}" != "$1" ] +} + + +# Prints $1 if it's absolute, or $2/$1 if $1 is not absolute. +_abspath() +{ + local here="$2" there="$1" + if _startswith "$1" "/"; then + echo "$1" + else + echo "$2/$1" + fi +} + + +# Prints $1 as a path relative to $PWD (not starting with /). +# If it already doesn't start with a /, doesn't change the string. +_relpath() +{ + local here="$2" there="$1" out= hadslash= + #echo "RP start '$there' hs='$hadslash'" >&2 + _startswith "$there" "/" || { echo "$there" && return; } + [ "$there" != "/" ] && _endswith "$there" "/" && hadslash=/ + here=${here%/}/ + while [ -n "$here" ]; do + #echo "RP out='$out' here='$here' there='$there'" >&2 + [ "${here%/}" = "${there%/}" ] && there= && break; + [ "${there#$here}" != "$there" ] && break + out=../$out + _dirsplit "${here%/}" + here=$_dirsplit_dir + done + there=${there#$here} + if [ -n "$there" ]; then + echo "$out${there%/}$hadslash" + else + echo "${out%/}$hadslash" + fi +} + + +# Prints a "normalized relative" path, with ".." resolved where possible. +# For example, a/b/../c will be reduced to just a/c. +_normpath() +( + local path="$1" relto="$2" out= isabs= + #echo "NP start '$path'" >&2 + if _startswith "$path" "/"; then + isabs=1 + else + path="${relto%/}/$path" + fi + set -f + IFS=/ + for d in ${path%/}; do + #echo "NP out='$out' d='$d'" >&2 + if [ "$d" = ".." ]; then + out=$(_updir "${out%/}")/ + else + out=$out$d/ + fi + done + #echo "NP out='$out' (done)" >&2 + out=${out%/} + if [ -n "$isabs" ]; then + echo "${out:-/}" + else + _relpath "${out:-/}" "$relto" + fi +) + + +# Prints a "real" path, with all symlinks resolved where possible. +_realpath() +{ + local path="$1" relto="$2" isabs= rest= + if _startswith "$path" "/"; then + isabs=1 + else + path="${relto%/}/$path" + fi + ( + for d in $(_seq 100); do + #echo "Trying: $PWD--$path" >&2 + if cd -P "$path" 2>/dev/null; then + # success + pwd=$(pwd -P) + #echo " chdir ok: $pwd--$rest" >&2 + np=$(_normpath "${pwd%/}/$rest" "$relto") + if [ -n "$isabs" ]; then + echo "$np" + else + _relpath "$np" "$relto" + fi + break + fi + _dirsplit "${path%/}" + path=$_dirsplit_dir + rest="$_dirsplit_base/$rest" + done + ) +} + + +# List the possible names for default*.do files in dir $1 matching the target +# pattern in $2. We stop searching when we find the first one that exists. +_find_dofiles_pwd() +{ + local dodir="$1" dofile="$2" + _startswith "$dofile" "default." || dofile=${dofile#*.} + while :; do + dofile=default.${dofile#default.*.} + echo "$dodir$dofile" + [ -e "$dodir$dofile" ] && return 0 + [ "$dofile" = default.do ] && break + done + return 1 +} + + +# List the possible names for default*.do files in $PWD matching the target +# pattern in $1. We stop searching when we find the first name that works. +# If there are no matches in $PWD, we'll search in .., and so on, to the root. +_find_dofiles() +{ + local target="$1" dodir= dofile= newdir= + _debug "find_dofile: '$PWD' '$target'" + dofile="$target.do" + echo "$dofile" + [ -e "$dofile" ] && return 0 + + # Try default.*.do files, walking up the tree + _dirsplit "$dofile" + dodir=$_dirsplit_dir + dofile=$_dirsplit_base + [ -n "$dodir" ] && dodir=${dodir%/}/ + [ -e "$dodir$dofile" ] && return 0 + for i in $(_seq 100); do + [ -n "$dodir" ] && dodir=${dodir%/}/ + #echo "_find_dofiles: '$dodir' '$dofile'" >&2 + _find_dofiles_pwd "$dodir" "$dofile" && return 0 + newdir=$(_realpath "${dodir}.." "$PWD") + [ "$newdir" = "$dodir" ] && break + dodir=$newdir + done + return 1 +} + + +# Print the last .do file returned by _find_dofiles. +# If that file exists, returns 0, else 1. +_find_dofile() +{ + local files="$(_find_dofiles "$1")" + rv=$? + #echo "files='$files'" >&2 + [ "$rv" -ne 0 ] && return $rv + echo "$files" | { + while read -r linex; do line=$linex; done + printf "%s\n" "$line" + } +} + + +# Actually run the given $dofile with the arguments in $@. +# Note: you should always run this in a subshell. +_run_dofile() +{ + export DO_DEPTH="$DO_DEPTH " + export REDO_TARGET="$PWD/$target" + local line1 + set -e + read line1 <"$PWD/$dofile" || true + cmd=${line1#"#!/"} + if [ "$cmd" != "$line1" ]; then + set -$_do_opt_verbose$_do_opt_exec + exec /$cmd "$PWD/$dofile" "$@" + else + set -$_do_opt_verbose$_do_opt_exec + # If $dofile is empty, "." might not change $? at + # all, so we clear it first with ":". + :; . "$PWD/$dofile" + fi +} + + +# Find and run the right .do file, starting in dir $1, for target $2, +# providing a temporary output file as $3. Renames the temp file to $2 when +# done. +_do() +{ + local dir="$1" target="$1$2" tmp="$1$2.redo.tmp" tdir= + local dopath= dodir= dofile= ext= + if [ "$_cmd" = "redo" ] || + ( [ ! -e "$target" -o -d "$target" ] && + [ ! -e "$target.did" ] ); then + printf '%sdo %s%s%s%s\n' \ + "$green" "$DO_DEPTH" "$bold" "$target" "$plain" >&2 + dopath=$(_find_dofile "$target") + if [ ! -e "$dopath" ]; then + echo "do: $target: no .do file ($PWD)" >&2 + return 1 + fi + _dirsplit "$dopath" + dodir=$_dirsplit_dir dofile=$_dirsplit_base + if _startswith "$dofile" "default."; then + ext=${dofile#default} + ext=${ext%.do} + else + ext= + fi + target=$PWD/$target + tmp=$PWD/$tmp + cd "$dodir" || return 99 + target=$(_relpath "$target" "$PWD") || return 98 + tmp=$(_relpath "$tmp" "$PWD") || return 97 + base=${target%$ext} + tdir=$(qdirname "$target") + [ ! -e "$DO_BUILT" ] || [ ! -w "$tdir/." ] || + : >>"$target.did.tmp" + # $qtmp is a temporary file used to capture stdout. + # Since it might be accidentally deleted as a .do file + # does its work, we create it, then open two fds to it, + # then immediately delete the name. We use one fd to + # redirect to stdout, and the other to read from after, + # because there's no way to fseek(fd, 0) in sh. + qtmp=$DO_PATH/do.$$.tmp + ( + rm -f "$qtmp" + ( _run_dofile "$target" "$base" "$tmp" >&3 3>&- 4<&- ) + rv=$? + if [ $rv != 0 ]; then + printf "do: %s%s\n" "$DO_DEPTH" \ + "$target: got exit code $rv" >&2 + rm -f "$tmp.tmp" "$tmp.tmp2" "$target.did" + return $rv + fi + echo "$PWD/$target" >>"$DO_BUILT" + if [ ! -e "$tmp" ]; then + # if $3 wasn't created, copy from stdout file + cat <&4 >$tmp + # if that's zero length too, forget it + [ -s "$tmp" ] || rm -f "$tmp" + fi + ) 3>$qtmp 4<$qtmp # can't use "|| return" here... + # ...because "|| return" would mess up "set -e" inside the () + # on some shells. Running commands in "||" context, even + # deep inside, will stop "set -e" from functioning. + rv=$? + [ "$rv" = 0 ] || return "$rv" + mv "$tmp" "$target" 2>/dev/null + [ -e "$target.did.tmp" ] && + mv "$target.did.tmp" "$target.did" || + : >>"$target.did" + else + _debug "do $DO_DEPTH$target exists." >&2 + fi +} + + +# Implementation of the "redo" command. +_redo() +{ + local i startdir="$PWD" dir base + set +e + for i in "$@"; do + i=$(_abspath "$i" "$startdir") + ( + cd "$DO_STARTDIR" || return 99 + i=$(_realpath "$(_relpath "$i" "$PWD")" "$PWD") + _dirsplit "$i" + dir=$_dirsplit_dir base=$_dirsplit_base + _do "$dir" "$base" + ) + [ "$?" = 0 ] || return 1 + done +} + + +# Implementation of the "redo-whichdo" command. +_whichdo() +{ + _find_dofiles "$1" +} + + +case $_cmd in + do|redo|redo-ifchange) _redo "$@" ;; + redo-whichdo) _whichdo "$1" ;; + do.test) ;; + *) printf "do: '%s': unexpected redo command" "$_cmd" >&2; exit 99 ;; +esac +[ "$?" = 0 ] || exit 1 + +if [ -n "$DO_TOP" ]; then + if [ -n "$_do_opt_clean" ]; then + echo "do: Removing stamp files..." >&2 + [ ! -e "$DO_BUILT" ] || + while read f; do printf "%s.did\0" "$f"; done <"$DO_BUILT" | + xargs -0 rm -f 2>/dev/null + fi +fi diff --git a/install.do b/install.do @@ -0,0 +1,10 @@ +redo-ifchange all +PREFIX=/usr/local +BINDIR=$PREFIX/bin +DOCDIR=$PREFIX/share/doc/slw2gmi +MANDIR=$PREFIX/share/man/man1 +install -d $BINDIR $DOCDIR $MANDIR +install -m 0755 slw2gmi $BINDIR +install -m 0644 slw2gmi.pdf $DOCDIR +install -m 0644 slw2gmi.1.gz $MANDIR + diff --git a/rebuild.do b/rebuild.do @@ -0,0 +1,3 @@ +redo clean +redo all + diff --git a/slw2gmi.1.in b/slw2gmi.1.in @@ -0,0 +1,92 @@ +'\" +.\" Manpage for slweb(1) +. +.mso an-ext.tmac +.de CDS +.EX +.RS \\$1 +.sp 1 +.. +.de CDE +.sp 1 +.RE +.EE +.. +. +.TH SLW2GMI "1" "%DATE%" "slw2gmi %VERSION%" "General Commands Manual" +.SH NAME +slw2gmi \- slweb to Gemini converter +. +.SH SYNOPSIS +. +.SY slw2gmi +.OP "\-h \fR|\fP \-\-help" +.YS +. +.SY slw2gmi +.OP "\-v \fR|\fP \-\-version" +.YS +. +.SY slw2gmi +.OP "\-d \fR|\fP \-\-basedir" directory +.RI [ filename ] +.YS +. +.SH COPYRIGHT +slw2gmi Copyright \(co 2021 Strahinya Radich. +.br +This program is licensed under GNU GPL v3 or later. See the file +.I LICENSE +in the slweb repository for details. +. +.SH DESCRIPTION +.B slw2gmi +is a converter from +.BR slweb (1) +to Gemini format. Although both are +similar to Markdown, the two formats differ enough to warrant a full converter. +. +.SH OPTIONS +. +.TP +.BI \-d " directory" +.TQ +.BI \-\-basedir " directory" +.br +Set the base directory as a reference point to normalize paths in includes (the +argument to +.B --relative-to +option for the +.BR realpath (1) +command). Defaults to the current directory. +. +.TP +.B \-h +.TQ +.B \-\-help +.br +Print this usage information screen. +. +.TP +.B \-v +.TQ +.B \-\-version +.br +Print program version and exit. +. +.SH REFERENCE +. +.LP +Refer to +.BR slweb (1) +manual for the description of .slw file format. +. +.SH BUGS +. +.LP +Bugs can be reported using the ticket tracker at: +.UR https://\:sr.ht/\:~strahinja/\:slw2gmi/\:trackers + +.UE +.\" vim: set filetype=groff: + diff --git a/slw2gmi.c b/slw2gmi.c @@ -0,0 +1,4016 @@ +/* + * slw2gmi - slweb to Gemini converter + * Copyright (C) 2021 Страхиња Радић + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License as published by the Free + * Software Foundation, either version 3 of the License, or (at your option) + * any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program. If not, see <https://www.gnu.org/licenses/>. + * + */ + +#include "defs.h" + +static size_t lineno = 0; +static size_t colno = 1; +static size_t output_colno = 1; +static char* input_filename = NULL; +static char* input_dirname = NULL; +static char* basedir = NULL; +static size_t basedir_size = 0; +static char* incdir = NULL; +static KeyValue* vars = NULL; +static KeyValue* pvars = NULL; +static size_t vars_count = 0; +static KeyValue* macros = NULL; +static KeyValue* pmacros = NULL; +static size_t macros_count = 0; +static KeyValue* links = NULL; +static KeyValue* plinks = NULL; +static size_t links_count = 0; +static KeyValue* paralinks = NULL; +static KeyValue* pparalinks = NULL; +static size_t paralinks_count = 0; +static KeyValue* footnotes = NULL; +static KeyValue* pfootnotes = NULL; +static size_t footnote_count = 0; +static size_t current_footnote = 0; +static uint8_t** inline_footnotes = NULL; +static size_t inline_footnote_count = 0; +static size_t current_inline_footnote = 0; +static uint8_t* csv_template = NULL; +static size_t csv_template_size = 0; +static char* csv_filename = NULL; +static long csv_iter = 0; +static ULONG state = ST_NONE; + +#define CHECKEXITNOMEM(ptr) { if (!ptr) exit(error(ENOMEM, \ + (uint8_t*)"Memory allocation failed (out of memory?)")); } + +#define CALLOC(ptr, ptrtype, nmemb) { ptr = calloc(nmemb, sizeof(ptrtype)); \ + CHECKEXITNOMEM(ptr) } + +#define REALLOC(ptr, ptrtype, newsize) { ptrtype* newptr = realloc(ptr, newsize); \ + CHECKEXITNOMEM(newptr) \ + ptr = newptr; } + +#define REALLOCARRAY(ptr, membtype, newcount) \ + REALLOC(ptr, membtype, sizeof(membtype) * newcount) + +#define CHECKCOPY(token, ptoken, token_size, pline) { \ + if (ptoken + 2 > token + token_size) \ + { \ + size_t old_size = token_size; \ + token_size += BUFSIZE; \ + REALLOC(token, uint8_t, token_size) \ + ptoken = token + old_size - 1; \ + } \ + *ptoken++ = *pline++; } + +#define RESET_TOKEN(token, ptoken, token_size) { \ + token_size = BUFSIZE; \ + REALLOC(token, uint8_t, token_size) \ + *token = 0; ptoken = token; } + +#define ALL(var, mask) ( ((var) & (mask)) == (mask) ) +#define ANY(var, mask) ( (var) & (mask) ) + +int +version() +{ + printf("%s v%s\n", PROGRAMNAME, VERSION); + return 0; +} + +int +usage() +{ + printf("Usage: %s [-b|--body-only] [-d|--basedir <dir>] [-h|--help]" + " [-v|--version] [filename]\n", PROGRAMNAME); + return 0; +} + +int +error(int code, uint8_t* fmt, ...) +{ + uint8_t buf[BUFSIZE]; + va_list args; + va_start(args, fmt); + u8_vsnprintf(buf, sizeof(buf), (const char*)fmt, args); + va_end(args); + fprintf(stderr, "%s:%s:%lu:%lu: %s\n", PROGRAMNAME, + input_filename ? input_filename : "(stdin)", + lineno, colno, buf); + return code; +} + +int +warning(int code, uint8_t* fmt, ...) +{ + uint8_t buf[BUFSIZE]; + va_list args; + va_start(args, fmt); + u8_vsnprintf(buf, sizeof(buf), (const char*)fmt, args); + va_end(args); + fprintf(stderr, "Warning: %s\n", buf); + return code; +} + +int +free_keyvalue(KeyValue** list, size_t list_count) +{ + if (!list || !*list) + return 1; + + for (size_t index = 0; index < list_count; index++) + { + KeyValue* current = *list + index; + if (current->value) + free(current->value); + if (current->key) + free(current->key); + } + return 0; +} + +int +slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links); + +char* +substr(const char* src, int start, int finish) +{ + int len = strlen(src); + if (finish > len) + finish = len; + int substr_len = finish-start; + if (substr_len < 0) + substr_len = 0; + char* result = NULL; + CALLOC(result, char, substr_len+1) + char* presult = result; + + for (int i = start; i < finish && *(src+i) != 0; i++) + *presult++ = *(src+i); + *presult = 0; + + return result; +} + +BOOL +startswith(const char* s, const char* what) +{ + if (!s || !what) + return 0; + + char* subs = substr(s, 0, strlen(what)); + BOOL result = !strcmp(subs, what); + + free(subs); + + return result; +} + +uint8_t* +get_value(KeyValue* list, size_t list_count, uint8_t* key, BOOL* seen) +{ + KeyValue* plist = list; + while (plist < list + list_count) + { + if (!u8_strcmp(plist->key, key)) + { + if (seen) + { + *seen = plist->seen; + plist->seen = TRUE; + } + return plist->value; + } + plist++; + } + return NULL; +} + +int +set_basedir(char* arg, char** basedir, size_t* basedir_size) +{ + size_t basedir_len = 0; + size_t arg_len = strlen(arg); + + if (arg_len < 1) + exit(error(1, (uint8_t*)"--basedir: Argument required")); + + if (arg_len + 1 > *basedir_size) + { + *basedir_size = arg_len+1; + REALLOC(*basedir, char, *basedir_size) + } + strncpy(*basedir, arg, *basedir_size-1); + *(*basedir + arg_len) = 0; + basedir_len = strlen(*basedir); + if (*(*basedir + basedir_len - 1) == '/') + *(*basedir + basedir_len - 1) = 0; + + return 0; +} + +char* +strip_ext(const char* fn) +{ + char* newname = NULL; + char* pnewname = NULL; + const char* pfn = NULL; + char* dot = NULL; + dot = strrchr(fn, '.'); + if (!dot) + return NULL; + CALLOC(newname, char, strlen(fn)+1) + pnewname = newname; + pfn = fn; + while (pfn != dot && *pfn) + *pnewname++ = *pfn++; + return newname; +} + +int +print_output(FILE* output, char* fmt, ...) +{ + uint8_t buf[BUFSIZE]; + va_list args; + + if (!output || !fmt) + exit(error(EINVAL, (uint8_t*)"print_output: Invalid argument")); + + va_start(args, fmt); + if (state & ST_CSV_BODY) + { + size_t buf_len = 0; + vsnprintf((char*)buf, BUFSIZE, fmt, args); + buf_len = u8_strlen(buf); + if (!csv_template) + { + csv_template_size = BUFSIZE; + CALLOC(csv_template, uint8_t, csv_template_size) + u8_strncpy(csv_template, buf, BUFSIZE-1); + *(csv_template + buf_len) = 0; + } + else + { + if (u8_strlen(csv_template) + buf_len > csv_template_size) + { + csv_template_size += BUFSIZE; + REALLOC(csv_template, uint8_t, csv_template_size) + } + u8_strncat(csv_template, buf, csv_template_size + - u8_strlen(csv_template) - 1); + } + } + else + vfprintf(output, fmt, args); + va_end(args); + return 0; +} + +#define PIPE_READ_INDEX 0 +#define PIPE_WRITE_INDEX 1 + +int +print_command(const char* command, + const uint8_t* pass_arguments[], const uint8_t* pipe_arguments[], + FILE* output, BOOL strip_newlines) +{ + if (!command || !pass_arguments) + exit(error(EINVAL, (uint8_t*)"print_command: Invalid argument")); + + pid_t pid = 0; + int arg_pipe_fds[2]; + int output_pipe_fds[2]; + int pstatus = 0; + + pipe(arg_pipe_fds); + pipe(output_pipe_fds); + pid = fork(); + if (pid == 0) + { + close(arg_pipe_fds[PIPE_WRITE_INDEX]); + close(output_pipe_fds[PIPE_READ_INDEX]); + + prctl(PR_SET_PDEATHSIG, SIGTERM); + + dup2(arg_pipe_fds[PIPE_READ_INDEX], STDIN_FILENO); + dup2(output_pipe_fds[PIPE_WRITE_INDEX], STDOUT_FILENO); + + execvp(command, (char* const*)pass_arguments); + exit(1); + } + else if (pid < 0) + exit(error(errno, (uint8_t*)"Fork failed")); + + /* Parent */ + close(arg_pipe_fds[PIPE_READ_INDEX]); + close(output_pipe_fds[PIPE_WRITE_INDEX]); + FILE* cmd_input = fdopen(arg_pipe_fds[PIPE_WRITE_INDEX], "w"); + FILE* cmd_output = fdopen(output_pipe_fds[PIPE_READ_INDEX], "r"); + + if (!cmd_input || !cmd_output) + exit(error(1, (uint8_t*)"Cannot fdopen")); + + if (pipe_arguments) + { + const uint8_t** ppipe_argument = pipe_arguments; + while (ppipe_argument && *ppipe_argument) + { + fprintf(cmd_input, "%s\n", *ppipe_argument); + ppipe_argument++; + } + } + fclose(cmd_input); + + uint8_t* cmd_output_line = NULL; + CALLOC(cmd_output_line, uint8_t, BUFSIZE) + while (!feof(cmd_output)) + { + if (!fgets((char*)cmd_output_line, BUFSIZE, cmd_output)) + continue; + + char* eol = strchr((char*)cmd_output_line, '\n'); + if (eol) + *eol = 0; + + print_output(output, "%s%s", cmd_output_line, + strip_newlines ? "" : "\n"); + } + if (!strip_newlines) + output_colno = 1; + free(cmd_output_line); + + fclose(cmd_output); + + kill(pid, SIGKILL); + pid_t wpid = waitpid(pid, &pstatus, 0); + if (wpid < 0) + warning(pstatus, (uint8_t*)"Child returned nonzero status, errno = %d", + errno); + if (WIFEXITED(pstatus)) + return WEXITSTATUS(pstatus); + + return wpid; +} + +int +read_file_into_buffer(uint8_t** buffer, size_t* buffer_size, char* input_filename, + char** input_dirname, FILE** input) +{ + struct stat fs; + char* slash = NULL; + + *input = fopen(input_filename, "r"); + if (!*input) + return error(ENOENT, (uint8_t*)"No such file: %s", input_filename); + + fstat(fileno(*input), &fs); + if (*buffer) + free(*buffer); + *buffer_size = fs.st_size+1; + CALLOC(*buffer, uint8_t, *buffer_size) + fread((void*)*buffer, 1, *buffer_size, *input); + + if (*input_dirname) + free(*input_dirname); + + slash = strrchr(input_filename, '/'); + if (slash) + { + CALLOC(*input_dirname, char, strlen(input_filename)+1) + char* pinput_dirname = *input_dirname; + char* pinput_filename = input_filename; + while (pinput_filename && *pinput_filename + && pinput_filename != slash) + *pinput_dirname++ = *pinput_filename++; + } + else + { + CALLOC(*input_dirname, char, 2) + **input_dirname = '.'; + } + + fclose(*input); + + return 0; +} + +int +read_csv(FILE* output, const char* filename, csv_callback_t callback); + +int +process_heading_start(FILE* output, UBYTE heading_level) +{ + print_output(output, "\n"); + for (size_t i = 0; i < heading_level; i++) + print_output(output, "#"); + print_output(output, " "); + output_colno = heading_level + 1; + return 0; +} + +int +process_heading(const uint8_t* token, FILE* output) +{ + if (!token || u8_strlen(token) < 1) + warning(1, (uint8_t*)"Empty heading"); + + print_output(output, "%s\n\n", token ? (char*)token : ""); + output_colno = 1; + + return 0; +} + +int +process_git_log(FILE* output) +{ + if (!input_filename) + return warning(1, (uint8_t*)"Cannot use 'git-log' in stdin"); + + char* basename = NULL; + size_t basename_size = strlen(input_filename)+1; + CALLOC(basename, char, basename_size) + char* slash = strrchr(input_filename, '/'); + pid_t result = 0; + + if (slash) + strncpy(basename, slash+1, basename_size-1); + else + strncpy(basename, input_filename, basename_size-1); + + uint8_t* pipe_args[] = { (uint8_t*)basename, NULL }; + + print_output(output, "Previous commit: "); + result = print_command(CMD_GIT_LOG, + (const uint8_t**)CMD_GIT_LOG_ARGS, + (const uint8_t**)pipe_args, output, FALSE); + print_output(output, "\n"); + output_colno = 1; + + free(basename); + + return result ? warning(result, (uint8_t*)"git-log: Cannot run git") : 0; +} + +#define PRINTIF(format, arg) { \ + if ((ALL(csv_state, ST_CS_COND_NONEMPTY) \ + && *csv_register[conditional_index-1]) \ + || (ALL(csv_state, ST_CS_COND_EMPTY) \ + && !*csv_register[conditional_index-1]) \ + || !ANY(csv_state, ST_CS_COND_EMPTY | ST_CS_COND_NONEMPTY)) \ + { fprintf(output, format, arg); } } + +int +print_csv_row(FILE* output, uint8_t** csv_header, uint8_t** csv_register) +{ + uint8_t* pcsv_template = csv_template; + UBYTE csv_state = ST_CS_NONE; + UBYTE num = 0; + UBYTE conditional_index = 0; + + while (*pcsv_template) + { + if (csv_state & ST_CS_ESCAPE) + { + PRINTIF("%c", *pcsv_template) + csv_state &= ~ST_CS_ESCAPE; + pcsv_template++; + continue; + } + + switch (*pcsv_template) + { + case '\\': + csv_state |= ST_CS_ESCAPE; + pcsv_template++; + break; + case '$': + if (csv_state & ST_CS_REGISTER) + { + PRINTIF("%c", '$') + csv_state &= ~ST_CS_REGISTER; + } + else + csv_state |= ST_CS_REGISTER; + pcsv_template++; + break; + case '#': + if (csv_state & ST_CS_HEADER) + { + error(1, (uint8_t*)"csv: Invalid header register mark"); + csv_state &= ~(ST_CS_REGISTER | ST_CS_HEADER); + } + else if (csv_state & ST_CS_REGISTER) + { + csv_state &= ~ST_CS_REGISTER; + csv_state |= ST_CS_HEADER; + } + else + PRINTIF("%c", '#') + pcsv_template++; + break; + case '?': + if (csv_state & ST_CS_COND) + { + error(1, (uint8_t*)"csv: Invalid conditional mark"); + csv_state &= ~ST_CS_COND; + } + else if (csv_state & ST_CS_REGISTER) + { + csv_state &= ~ST_CS_REGISTER; + csv_state |= ST_CS_COND; + } + else + PRINTIF("%c", '?') + pcsv_template++; + break; + case '!': + if (ALL(csv_state, ST_CS_COND | ST_CS_COND_NONEMPTY)) + { + csv_state &= ~(ST_CS_COND | ST_CS_COND_NONEMPTY); + csv_state |= ST_CS_COND_EMPTY; + } + else if (csv_state & ST_CS_COND) + { + error(1, (uint8_t*)"Empty conditional before/without nonempty" + " conditional"); + csv_state &= ~(ST_CS_COND | ST_CS_COND_EMPTY); + } + else + PRINTIF("%c", '!') + pcsv_template++; + break; + case '/': + if (ALL(csv_state, ST_CS_COND | ST_CS_COND_EMPTY)) + csv_state &= ~(ST_CS_COND | ST_CS_COND_EMPTY); + else if (ALL(csv_state, ST_CS_COND)) + csv_state &= ~(ST_CS_COND | ST_CS_COND_NONEMPTY); + else + PRINTIF("%c", '/') + pcsv_template++; + break; + case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '8': case '9': + if (csv_state & ST_CS_COND) + { + conditional_index = *pcsv_template - '0'; + csv_state &= ~ST_CS_COND; + csv_state |= ST_CS_COND_NONEMPTY; + } + else if (csv_state & ST_CS_HEADER) + { + num = *pcsv_template - '0'; + PRINTIF("%s", csv_header[num-1]) + csv_state &= ~ST_CS_HEADER; + } + else if (csv_state & ST_CS_REGISTER) + { + num = *pcsv_template - '0'; + PRINTIF("%s", csv_register[num-1]) + csv_state &= ~ST_CS_REGISTER; + } + else + PRINTIF("%c", *pcsv_template) + pcsv_template++; + break; + default: + if (csv_state & ST_CS_HEADER) + { + error(1, (uint8_t*)"csv: Invalid header register mark"); + csv_state &= ~ST_CS_HEADER; + } + else if (csv_state & ST_CS_REGISTER) + { + error(1, (uint8_t*)"csv: Invalid register mark"); + csv_state &= ~ST_CS_REGISTER; + } + else + PRINTIF("%c", *pcsv_template) + pcsv_template++; + } + } + return 0; +} + +int +read_csv(FILE* output, const char* filename, csv_callback_t callback) +{ + if (!callback) + exit(error(EINVAL, (uint8_t*)"read_csv: Invalid callback argument")); + + FILE* csv = NULL; + size_t csv_lineno = 0; + uint8_t* bufline = NULL; + uint8_t* pbufline = NULL; + uint8_t* token = NULL; + uint8_t* ptoken = NULL; + size_t token_size = 0; + UBYTE csv_state = ST_CS_NONE; + uint8_t* csv_header[MAX_CSV_REGISTERS]; + UBYTE current_header = 0; + uint8_t* csv_register[MAX_CSV_REGISTERS]; + UBYTE current_register = 0; + uint8_t* csv_delimiter = get_value(vars, vars_count, + (uint8_t*)"csv-delimiter", NULL); + + if (!(csv = fopen(filename, "rt"))) + exit(error(ENOENT, (uint8_t*)"csv: No such file: %s", filename)); + + CALLOC(bufline, uint8_t, BUFSIZE) + token_size = BUFSIZE; + CALLOC(token, uint8_t, token_size) + for (UBYTE i = 0; i < MAX_CSV_REGISTERS; i++) + CALLOC(csv_header[i], uint8_t, BUFSIZE) + for (UBYTE i = 0; i < MAX_CSV_REGISTERS; i++) + CALLOC(csv_register[i], uint8_t, BUFSIZE) + + while (!feof(csv) && (!csv_iter || csv_lineno <= csv_iter)) + { + uint8_t* eol = NULL; + if (!fgets((char*)bufline, BUFSIZE, csv)) + break; + eol = u8_strchr(bufline, (ucs4_t)'\n'); + if (eol) + *eol = 0; + + pbufline = bufline; + RESET_TOKEN(token, ptoken, token_size) + current_register = 0; + while (*pbufline) + { + switch (*pbufline) + { + case '"': + if (csv_state & ST_CS_QUOTE) + csv_state &= ~ST_CS_QUOTE; + else + csv_state |= ST_CS_QUOTE; + pbufline++; + break; + case ';': + case ',': + if (csv_state & ST_CS_QUOTE) + CHECKCOPY(token, ptoken, token_size, pbufline) + else + { + *ptoken = 0; + if (csv_lineno > 0) + { + if (current_register < MAX_CSV_REGISTERS) + u8_strncpy(csv_register[current_register++], token, + BUFSIZE-1); + } + else + { + if (current_header < MAX_CSV_REGISTERS) + u8_strncpy(csv_header[current_header++], token, + BUFSIZE-1); + } + RESET_TOKEN(token, ptoken, token_size) + pbufline++; + } + break; + default: + if (csv_state & ST_CS_QUOTE) + CHECKCOPY(token, ptoken, token_size, pbufline) + else + { + if (csv_delimiter && *pbufline == *csv_delimiter) + { + *ptoken = 0; + if (csv_lineno > 0) + { + if (current_register < MAX_CSV_REGISTERS) + u8_strncpy(csv_register[current_register++], token, + BUFSIZE-1); + } + else + { + if (current_header < MAX_CSV_REGISTERS) + u8_strncpy(csv_header[current_header++], token, + BUFSIZE-1); + } + RESET_TOKEN(token, ptoken, token_size) + pbufline++; + } + else + CHECKCOPY(token, ptoken, token_size, pbufline) + } + } + } + *ptoken = 0; + if (csv_lineno > 0) + { + if (current_register < MAX_CSV_REGISTERS) + u8_strncpy(csv_register[current_register++], token, + BUFSIZE-1); + } + else + { + if (current_header < MAX_CSV_REGISTERS) + u8_strncpy(csv_header[current_header++], token, BUFSIZE-1); + } + RESET_TOKEN(token, ptoken, token_size) + + if (csv_lineno > 0 && pbufline != bufline) + (*callback)(output, csv_header, csv_register); + + for (UBYTE i = 0; i < MAX_CSV_REGISTERS; i++) + *csv_register[i] = 0; + csv_lineno++; + } + fclose(csv); + for (UBYTE i = MAX_CSV_REGISTERS; i > 0; i--) + free(csv_register[i-1]); + for (UBYTE i = MAX_CSV_REGISTERS; i > 0; i--) + free(csv_header[i-1]); + free(token); + free(bufline); + + return 0; +} + +int +process_csv(uint8_t* arg_token, FILE* output, BOOL read_yaml_macros_and_links, + BOOL end_tag) +{ + if (end_tag) + { + state &= ~ST_CSV_BODY; + + if (read_yaml_macros_and_links) + return 0; + + read_csv(output, csv_filename, &print_csv_row); + + free(csv_filename); + csv_filename = NULL; + free(csv_template); + csv_template = NULL; + csv_template_size = 0; + } + else + { + if (state & ST_CSV_BODY) + exit(error(1, (uint8_t*)"Can't nest csv directives")); + + state |= ST_CSV_BODY; + + if (read_yaml_macros_and_links) + return 0; + + csv_iter = 0; + uint8_t* saveptr = NULL; + uint8_t* args = u8_strtok(arg_token, (uint8_t*)" ", &saveptr); + args = u8_strtok(NULL, (uint8_t*)" ", &saveptr); + if (!args) + exit(error(EINVAL, (uint8_t*)"csv: Arguments required")); + size_t args_len = u8_strlen(args); + if (*args != '"' || *(args + args_len - 1) != '"') + exit(error(EINVAL, (uint8_t*)"csv: First argument must be a string")); + if (!csv_filename) + CALLOC(csv_filename, uint8_t, BUFSIZE) + uint8_t* args_base = u8_strdup(args+1); + *(args_base + u8_strlen(args_base) - 1) = 0; + snprintf(csv_filename, BUFSIZE, "%s/%s.csv", input_dirname, (char*)args_base); + free(args_base); + args = u8_strtok(NULL, (uint8_t*)" ", &saveptr); + if (args) + { + errno = 0; + csv_iter = strtol((char*)args, NULL, 10); + if (errno) + exit(error(errno, (uint8_t*)"csv: Invalid argument '%s'", args)); + } + } + + return 0; +} + +int +process_include(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links) +{ + if (read_yaml_macros_and_links) + return 0; + + if (!input_filename) + return warning(1, (uint8_t*)"Cannot use 'include' in stdin"); + + uint8_t* ptoken = u8_strchr(token, (ucs4_t)' '); + char* include_filename = NULL; + char* pinclude_filename = NULL; + + if (!ptoken) + exit(error(1, (uint8_t*)"Directive 'include' requires" + " an argument")); + + fflush(output); + pid_t pid = fork(); + int pstatus = 0; + + if (pid > 0) + wait(&pstatus); + else if (pid == 0) + { + prctl(PR_SET_PDEATHSIG, SIGTERM); + + CALLOC(include_filename, char, BUFSIZE) + pinclude_filename = include_filename; + ptoken++; + while (ptoken && *ptoken) + if (*ptoken != '"') + *pinclude_filename++ = *ptoken++; + else + ptoken++; + + if (!strcmp(basedir, ".")) + set_basedir(input_dirname, &basedir, &basedir_size); + + CALLOC(input_filename, char, BUFSIZE) + snprintf(input_filename, BUFSIZE, "%s/%s.slw", basedir, include_filename); + free(include_filename); + + FILE* input = NULL; + FILE* output = stdout; + uint8_t* buffer = NULL; + size_t buffer_size = 0; + int result = 0; + ULONG saved_state = ST_NONE; + + read_file_into_buffer(&buffer, &buffer_size, input_filename, + &input_dirname, &input); + + free(links); + CALLOC(links, KeyValue, 1) + links->key = NULL; + links->value = NULL; + links->value_size = 0; + links_count = 0; + + free(paralinks); + CALLOC(paralinks, KeyValue, 1) + paralinks->key = NULL; + paralinks->value = NULL; + paralinks->value_size = 0; + paralinks_count = 0; + + free(footnotes); + CALLOC(footnotes, KeyValue, 1) + footnotes->key = NULL; + footnotes->value = NULL; + footnotes->value_size = 0; + footnote_count = 0; + current_footnote = 0; + + free(inline_footnotes); + CALLOC(inline_footnotes, uint8_t*, 1) + *inline_footnotes = NULL; + inline_footnote_count = 0; + current_inline_footnote = 0; + + saved_state = state; + state = ST_NONE; + + /* First pass: read YAML, macros and links */ + result = slweb_parse(buffer, output, TRUE); + + if (result) + { + free(input_filename); + free(buffer); + return result; + } + state = ST_NONE; + current_footnote = 0; + current_inline_footnote = 0; + + /* Second pass: parse and output */ + result = slweb_parse(buffer, output, FALSE); + + state = saved_state; + + free(input_filename); + free(buffer); + exit(result); + } + else + exit(error(1, (uint8_t*)"Fork failed")); + + return 0; +} + +int +process_list_start(FILE* output) +{ + return 0; +} + +int +process_list_item_start(FILE* output) +{ + fprintf(output, "%s* ", output_colno == 1 ? "" : "\n"); + state |= ST_PARA_OPEN; + return 0; +} + +int +process_paralinks(FILE* output); + +int +process_list_item_end(FILE* output) +{ + if (state & ST_PARA_OPEN) + { + state &= ~ST_PARA_OPEN; + fprintf(output, "\n"); + output_colno = 1; + process_paralinks(output); + } + return 0; +} + +int +process_list_end(FILE* output) +{ + return 0; +} + +int +process_numlist_start(FILE* output) +{ + return 0; +} + +int +process_numlist_end(FILE* output) +{ + return 0; +} + +int +filter_subdirs(const struct dirent* node) +{ + if (!node || ((*node->d_name == '.') + && (!*(node->d_name+1) + || ((*(node->d_name+1) == '.') + && !(*(node->d_name+2)))))) + return 0; + + struct stat st; + char* nodename = NULL; + + CALLOC(nodename, char, BUFSIZE) + snprintf(nodename, BUFSIZE, "%s/%s", incdir, node->d_name); + + if (lstat(nodename, &st) < 0 || !S_ISDIR(st.st_mode)) + { + free(nodename); + return 0; + } + + free(nodename); + + return 1; +} + +int +filter_slw(const struct dirent* node) +{ + if (!node || ((*node->d_name == '.') + && (!*(node->d_name+1) + || ((*(node->d_name+1) == '.') + && !(*(node->d_name+2)))))) + return 0; + + size_t node_len = strlen(node->d_name); + size_t slw_len = strlen(".slw"); + + if (slw_len >= node_len) + return 0; + return !strcmp(node->d_name + strlen(node->d_name) - slw_len, ".slw"); +} + +int +reverse_alphacompare(const struct dirent** a, const struct dirent** b) +{ + if (!a || !b) + return 0; + + return -1 * strcmp((*a)->d_name, (*b)->d_name); +} + +int +process_timestamp(FILE* output, const char* link, uint8_t* permalink_macro, + uint8_t* date, uint8_t* title); + +int +process_incdir_subdir(const char* subdirname, FILE* output, BOOL details_open, + uint8_t* macro_body) +{ + /* + *print_output(output, "%s\n", subdirname); + */ + + struct dirent** namelist; + struct dirent** pnamelist; + long names_total = 0; + long names_output; + char* abs_subdirname = NULL; + + CALLOC(abs_subdirname, char, BUFSIZE) + snprintf(abs_subdirname, BUFSIZE, "%s/%s/%s", basedir, incdir, subdirname); + + if ((names_total = scandir(abs_subdirname, &namelist, &filter_slw, + &reverse_alphacompare)) < 0) + { + perror("scandir"); + free(namelist); + free(abs_subdirname); + exit(error(errno, (uint8_t*)"incdir_subdir: scandir error")); + } + + pnamelist = namelist; + names_output = 0; + while (names_output < names_total && pnamelist && *pnamelist) + { + int pstatus = 0; + char* basename = strip_ext((*pnamelist)->d_name); + char* link = NULL; + CALLOC(link, char, BUFSIZE) + + //print_output(output, "\n=> %s/%s.gmi\t", subdirname, basename); + snprintf(link, BUFSIZE, "%s/%s", subdirname, basename); + + fflush(output); + pid_t pid = fork(); + if (pid > 0) + wait(&pstatus); + else if (pid == 0) + { + set_basedir(abs_subdirname, &basedir, &basedir_size); + snprintf(input_filename, BUFSIZE, "%s/%s", abs_subdirname, + (*pnamelist)->d_name); + + /* + *print_output(output, "\n=> %s/%s.gmi\t%s", subdirname, + * basename, subdirname); + */ + FILE* input = NULL; + FILE* output = stdout; + uint8_t* buffer = NULL; + size_t buffer_size = 0; + int result = 0; + ULONG saved_state = ST_NONE; + + read_file_into_buffer(&buffer, &buffer_size, input_filename, + &input_dirname, &input); + + free(links); + CALLOC(links, KeyValue, 1) + links->key = NULL; + links->value = NULL; + links->value_size = 0; + links_count = 0; + + free(paralinks); + CALLOC(paralinks, KeyValue, 1) + paralinks->key = NULL; + paralinks->value = NULL; + paralinks->value_size = 0; + paralinks_count = 0; + + free(footnotes); + CALLOC(footnotes, KeyValue, 1) + footnotes->key = NULL; + footnotes->value = NULL; + footnotes->value_size = 0; + footnote_count = 0; + current_footnote = 0; + + free(inline_footnotes); + CALLOC(inline_footnotes, uint8_t*, 1) + *inline_footnotes = NULL; + inline_footnote_count = 0; + current_inline_footnote = 0; + + saved_state = state; + state = ST_NONE; + + saved_state = state; + state = ST_NONE; + + /* Read YAML, macros and links */ + result = slweb_parse(buffer, output, TRUE); + + if (result) + { + free(buffer); + free(link); + free(basename); + free(abs_subdirname); + while (names_total--) + free(namelist[names_total]); + free(namelist); + return result; + } + + uint8_t* date = get_value(vars, vars_count, (uint8_t*)"date", NULL); + uint8_t* title = get_value(vars, vars_count, (uint8_t*)"title", + NULL); + if (title && date) + process_timestamp(output, link, NULL, date, title); + /* + *print_output(output, "%s%s", link, title); + */ + + state = saved_state; + + fflush(output); + free(buffer); + free(link); + free(basename); + free(abs_subdirname); + while (names_total--) + free(namelist[names_total]); + free(namelist); + exit(result); + } + else + exit(error(1, (uint8_t*)"Fork failed")); + + free(link); + free(basename); + pnamelist++; + names_output++; + } + + while (names_total--) + free(namelist[names_total]); + free(namelist); + free(abs_subdirname); + + return 0; +} + +int +process_incdir(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links) +{ + if (read_yaml_macros_and_links) + return 0; + + uint8_t* saveptr = NULL; + /* skipping the first token (incdir) */ + uint8_t* arg = u8_strtok(token, (uint8_t*)" ", &saveptr); + size_t arg_len = 0; + long num = 5; + uint8_t* macro_body = NULL; + int names_total = 0; + struct dirent** namelist; + struct dirent** pnamelist; + long names_output; + BOOL details_open = TRUE; + + + arg = u8_strtok(NULL, (uint8_t*)" ", &saveptr); + if (!arg) + exit(error(1, (uint8_t*)"incdir: Arguments required")); + + arg_len = u8_strlen(arg); + + if (*arg != '"' || *(arg + arg_len - 1) != '"') + exit(error(1, (uint8_t*)"incdir: First argument not string")); + + incdir = strdup((char*)(arg+1)); + *(incdir + strlen(incdir) - 1) = 0; + + arg = u8_strtok(NULL, (uint8_t*)" ", &saveptr); + if (!arg) + exit(error(1, (uint8_t*)"incdir: Second argument required")); + + if (*arg == '=') + macro_body = get_value(macros, macros_count, arg+1, NULL); + else + { + uint8_t* parg = arg; + while (parg && *parg) + { + if (*parg < '0' || *parg > '9') + exit(error(1, (uint8_t*)"incdir: Non-numeric argument")); + parg++; + } + num = strtol((char*)arg, NULL, 10); + if (errno) + exit(error(errno, (uint8_t*)"incdir: Invalid parameter 'num'")); + arg = u8_strtok(NULL, (uint8_t*)" ", &saveptr); + if (arg) + { + if (*arg != '=') + { + free(incdir); + exit(error(1, (uint8_t*)"incdir: Third argument not macro")); + } + macro_body = get_value(macros, macros_count, arg+1, NULL); + } + } + + ; + if ((names_total = scandir(incdir, &namelist, &filter_subdirs, + &reverse_alphacompare)) < 0) + { + perror("scandir"); + pnamelist = namelist; + while (pnamelist && *pnamelist) + { + free(*pnamelist); + pnamelist++; + } + free(namelist); + free(incdir); + exit(error(errno, (uint8_t*)"incdir: scandir '%s' error", incdir)); + } + + pnamelist = namelist; + names_output = 0; + print_output(output, "\n"); + while (names_output < num && pnamelist && *pnamelist) + { + process_incdir_subdir((*pnamelist)->d_name, output, details_open, + macro_body); + details_open = FALSE; + pnamelist++; + names_output++; + } + print_output(output, "\n"); + output_colno = 1; + + while (names_total--) + free(namelist[names_total]); + free(namelist); + free(incdir); + + return 0; +} + +int +process_timestamp(FILE* output, const char* link, uint8_t* permalink_macro, + uint8_t* date, uint8_t* title) +{ + uint8_t* day = NULL; + uint8_t* month = NULL; + uint8_t* year = NULL; + uint8_t* formatted_date = NULL; + uint8_t* ptr = NULL; + const char* ptimestamp_format = NULL; + char* in_filename = NULL; + char* in_line = NULL; + + CALLOC(formatted_date, uint8_t, DATEBUFSIZE) + ptr = NULL; + year = u8_strtok(date, (uint8_t*)"-", &ptr); + if (year) + { + month = u8_strtok(NULL, (uint8_t*)"-", &ptr); + if (month) + { + day = u8_strtok(NULL, (uint8_t*)"T", &ptr); + if (day) + { + ptimestamp_format = timestamp_format; + while (*ptimestamp_format) + { + if (*ptimestamp_format == 'd' + || *ptimestamp_format == 'D') + u8_strncat(formatted_date, day, + DATEBUFSIZE-u8_strlen(formatted_date)-1); + else if (*ptimestamp_format == 'm' + || *ptimestamp_format == 'M') + u8_strncat(formatted_date, month, + DATEBUFSIZE-u8_strlen(formatted_date)-1); + else if (*ptimestamp_format == 'y' + || *ptimestamp_format == 'Y') + u8_strncat(formatted_date, year, + DATEBUFSIZE-u8_strlen(formatted_date)-1); + else + *(formatted_date + u8_strlen(formatted_date)) + = *ptimestamp_format; + + ptimestamp_format++; + } + if (title) + print_output(output, "=> %s.gmi\t%s [%s]\n", link, + title, formatted_date); + else + print_output(output, "=> %s.gmi\t%s\n", link, formatted_date); + output_colno = 1; + } + } + } + + free(formatted_date); + free(in_line); + free(in_filename); + + return 0; +} + +int +process_macro(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links, + BOOL end_tag) +{ + if (!end_tag) + { + if (state & ST_MACRO_BODY) + exit(error(1, (uint8_t*)"Macro undefined or nested")); + + BOOL seen = FALSE; + uint8_t* macro_body = get_value(macros, macros_count, token+1, + read_yaml_macros_and_links ? NULL : &seen); + + if (macro_body) + { + if (!read_yaml_macros_and_links) + { + if (seen) + { + uint8_t* eol = u8_strrchr(macro_body, '\n'); + if (eol) + output_colno = macro_body + u8_strlen(macro_body) - + u8_strrchr(macro_body, '\n') + 1; + else + output_colno += u8_strlen(macro_body); + print_output(output, "%s", macro_body); + } + else + state |= ST_MACRO_BODY; + } + } + else + { + if (read_yaml_macros_and_links) + { + macros_count++; + + if (macros_count > 1) + { + REALLOC(macros, KeyValue, macros_count * sizeof(KeyValue)) + pmacros = macros + macros_count - 1; + } + CALLOC(pmacros->key, uint8_t, KEYSIZE) + pmacros->seen = FALSE; + u8_strncpy(pmacros->key, token+1, KEYSIZE-1); + pmacros->value = NULL; + pmacros->value_size = 0; + } + state |= ST_MACRO_BODY; + } + } + else + state &= ~ST_MACRO_BODY; + + return 0; +} + +int +process_tag(uint8_t* token, FILE* output, BOOL read_yaml_macros_and_links, + BOOL* skip_eol, BOOL end_tag) +{ + if (!token || u8_strlen(token) < 1) + return warning(1, (uint8_t*)"%s:%ld:%ld: Empty tag name", + input_filename, lineno, colno); + + if (!strcmp((char*)token, "git-log") + && !read_yaml_macros_and_links) /* {git-log} */ + { + process_git_log(output); + } + else if (!strcmp((char*)token, "made-by") + && !read_yaml_macros_and_links) /* {made-by} */ + { + print_output(output, "Generated by slw2gmi © %s Strahinya Radich.\n", + COPYRIGHTYEAR); + output_colno = 1; + } + else if (startswith((char*)token, "csv")) /* {csv} */ + { + process_csv(token, output, read_yaml_macros_and_links, end_tag); + } + else if (startswith((char*)token, "include")) /* {include} */ + { + process_include(token, output, read_yaml_macros_and_links); + *skip_eol = TRUE; + } + else if (startswith((char*)token, "incdir")) /* {incdir} */ + { + process_incdir(token, output, read_yaml_macros_and_links); + *skip_eol = TRUE; + } + else if (*token == '=') /* {=macro} */ + { + process_macro(token, output, read_yaml_macros_and_links, end_tag); + *skip_eol = TRUE; + } + + return 0; +} + +int +process_bold(FILE* output, BOOL end_tag) +{ + return 0; +} + +int +process_italic(FILE* output, BOOL end_tag) +{ + return 0; +} + +int +process_code(FILE* output, BOOL end_tag) +{ + return 0; +} + +int +process_blockquote(FILE* output, BOOL end_tag) +{ + if (end_tag) + { + print_output(output, "\n\n"); + output_colno = 1; + } + else + { + print_output(output, "%s>", output_colno == 1 ? "" : "\n"); + output_colno = 2; + } + return 0; +} + +int +process_kbd(FILE* output, BOOL end_tag) +{ + return 0; +} + +int +process_table_start(FILE* output) +{ + return 0; +} + +int +process_table_header_start(FILE* output) +{ + /*print_output(output, "<thead>\n<tr><th>");*/ + return 0; +} + +int +process_table_header_cell(FILE* output) +{ + /*print_output(output, "</th><th>");*/ + return 0; +} + +int +process_table_header_end(FILE* output) +{ + /*print_output(output, "</th></tr>\n</thead>\n");*/ + return 0; +} + +int +process_table_body_start(FILE* output, BOOL start_row) +{ + /* + *print_output(output, "<tbody>\n"); + *if (start_row) + * print_output(output, "<tr><td>"); + */ + return 0; +} + +int +process_table_body_row_start(FILE* output) +{ + /*print_output(output, "<tr><td>");*/ + return 0; +} + +int +process_table_body_cell(FILE* output) +{ + /*print_output(output, "</td><td>");*/ + return 0; +} + +int +process_table_body_row_end(FILE* output) +{ + /*print_output(output, "</td></tr>\n");*/ + return 0; +} + +int +process_table_end(FILE* output) +{ + /*print_output(output, "</tbody>\n</table>\n");*/ + return 0; +} + +BOOL +url_is_local(char* url) +{ + return !(startswith(url, "http://") + || startswith(url, "https://") + || startswith(url, "ftp://") + || startswith(url, "ftps://") + || startswith(url, "mailto://")); +} + +int +get_realpath(char** realpath, char* relativeto, char* path) +{ + uint8_t* command = NULL; + FILE* cmd_output = NULL; + + CALLOC(command, uint8_t, BUFSIZE) + strncpy(*realpath, ".", BUFSIZE-1); + u8_snprintf(command, BUFSIZE-1, "realpath --relative-to=%s %s", + relativeto, path); + + cmd_output = popen((char*)command, "r"); + if (cmd_output) + { + uint8_t* cmd_output_line = NULL; + CALLOC(cmd_output_line, uint8_t, BUFSIZE) + while (!feof(cmd_output)) + { + if (!fgets((char*)cmd_output_line, BUFSIZE, cmd_output)) + continue; + + char* eol = strchr((char*)cmd_output_line, '\n'); + if (eol) + *eol = 0; + + strncpy(*realpath, (char*)cmd_output_line, BUFSIZE-1); + } + pclose(cmd_output); + free(cmd_output_line); + } + else + warning(1, (uint8_t*)"get_realpath: Cannot popen"); + + free(command); + + return 0; +} + +int +process_inline_link(uint8_t* link_text, uint8_t* link_macro_body, + uint8_t* link_url, FILE* output) +{ + /* + *print_output(output, "\n\n=> %s\t%s\n\n", link_url ? (char*)link_url : "", + * link_text); + */ + + paralinks_count++; + if (paralinks_count > 1) + { + REALLOCARRAY(paralinks, KeyValue, paralinks_count) + pparalinks = paralinks + paralinks_count - 1; + } + pparalinks->key = u8_strdup(link_url); + pparalinks->value_size = u8_strlen(link_text)+1; + pparalinks->value = u8_strdup(link_text); + + print_output(output, "%s", link_text); + output_colno += u8_strlen(link_text); + return 0; +} + +int +process_link(uint8_t* link_text, uint8_t* link_macro_body, uint8_t* link_id, + FILE* output) +{ + uint8_t* url = get_value(links, links_count, link_id, NULL); + return process_inline_link(link_text, link_macro_body, + url, output); +} + +int +process_inline_image(uint8_t* image_text, uint8_t* image_url, FILE* output, + BOOL add_link, BOOL add_figcaption) +{ + /* + *print_output(output, "\n\n=> %s\t%s\n\n", + * image_url ? (char*)image_url : "", + * image_text); + */ + paralinks_count++; + if (paralinks_count > 1) + { + REALLOCARRAY(paralinks, KeyValue, paralinks_count) + pparalinks = paralinks + paralinks_count - 1; + } + pparalinks->key = u8_strdup(image_url); + pparalinks->value_size = u8_strlen(image_text)+1; + pparalinks->value = u8_strdup(image_text); + + print_output(output, "%s", image_text); + output_colno += u8_strlen(image_text); + + return 0; +} + +int +process_image(uint8_t* image_text, uint8_t* image_id, FILE* output, + BOOL add_link, BOOL add_figcaption) +{ + uint8_t* url = get_value(links, links_count, image_id, NULL); + return process_inline_image(image_text, url, output, add_link, + add_figcaption); +} + +int +process_paralinks(FILE* output) +{ + KeyValue* pparalink = paralinks; + while (pparalink < paralinks + paralinks_count) + { + if (pparalink->key) + { + print_output(output, "%s=> %s\t%s\n", + output_colno == 1 ? "" : "\n", + pparalink->key, pparalink->value); + output_colno = 1; + } + pparalink++; + } + if (paralinks_count > 0) + print_output(output, "\n"); + + free_keyvalue(&paralinks, paralinks_count); + free(paralinks); + CALLOC(paralinks, KeyValue, 1) + paralinks->key = NULL; + paralinks->value = NULL; + paralinks->value_size = 0; + paralinks_count = 0; + pparalinks = paralinks; + + return 0; +} + +int +process_line_start(uint8_t* line, BOOL first_line_in_doc, + BOOL previous_line_blank, BOOL read_yaml_macros_and_links, + BOOL list_para, FILE* output, uint8_t** token, uint8_t** ptoken) +{ + if ((first_line_in_doc || previous_line_blank) + && !(ANY(state, ST_BLOCKQUOTE | ST_PRE))) + { + if (!list_para) + { + if (state & ST_LIST) + { + state &= ~ST_LIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_list_end(output); + } + } + + if (state & ST_NUMLIST) + { + state &= ~ST_NUMLIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_numlist_end(output); + } + } + + if (state & ST_FOOTNOTE_TEXT) + { + if (!read_yaml_macros_and_links && (state & ST_PARA_OPEN)) + { + print_output(output, "\n\n"); + output_colno = 1; + process_paralinks(output); + } + state &= ~(ST_FOOTNOTE_TEXT | ST_PARA_OPEN); + } + } + if (!ANY(state, ST_TABLE | ST_TABLE_HEADER | ST_TABLE_LINE)) + { + state |= ST_PARA_OPEN; + } + } + return 0; +} + +int +process_text_token(uint8_t* line, BOOL first_line_in_doc, + BOOL previous_line_blank, + BOOL processed_start_of_line, + BOOL read_yaml_macros_and_links, BOOL list_para, + FILE* output, uint8_t** token, + uint8_t** ptoken, size_t* token_size, + BOOL add_enclosing_paragraph) +{ + if (!(state & ST_YAML)) + { + if (add_enclosing_paragraph && !processed_start_of_line) + process_line_start(line, first_line_in_doc, previous_line_blank, + read_yaml_macros_and_links, list_para, output, token, ptoken); + **ptoken = 0; + if (**token && !read_yaml_macros_and_links + && !(state & ST_MACRO_BODY)) + { + print_output(output, "%s", *token); + uint8_t* eol = u8_strrchr(*token, '\n'); + if (eol) + output_colno = *token + u8_strlen(*token) - eol + 1; + else + output_colno += u8_strlen(*token); + } + } + RESET_TOKEN(*token, *ptoken, *token_size) + return 0; +} + +int +process_inline_footnote(uint8_t* token, BOOL read_yaml_macros_and_links, + FILE* output) +{ + current_inline_footnote++; + + if (read_yaml_macros_and_links) + { + size_t token_len = u8_strlen(token); + + inline_footnote_count++; + if (inline_footnote_count == 1 && footnote_count > 0) + warning(1, (uint8_t*)"Both inline and regular footnotes present"); + else if (inline_footnote_count > 1) + REALLOC(inline_footnotes, uint8_t*, sizeof(uint8_t*) * inline_footnote_count) + CALLOC(inline_footnotes[inline_footnote_count-1], uint8_t, token_len+1) + + u8_strncpy(inline_footnotes[inline_footnote_count-1], token, token_len); + *(inline_footnotes[inline_footnote_count-1] + token_len) = 0; + } + else + { + print_output(output, "(%d)", current_inline_footnote); + output_colno += 3; // We only care if it's the first column anyway + } + + return 0; +} + +int +process_footnote(uint8_t* token, BOOL footnote_definition, BOOL footnote_output, + FILE* output) +{ + current_footnote++; + + if (footnote_definition) + { + footnote_count++; + if (footnote_count == 1 && inline_footnote_count > 0) + warning(1, (uint8_t*)"Both inline and regular footnotes present"); + else if (footnote_count > 1) + { + REALLOC(footnotes, KeyValue, footnote_count * sizeof(KeyValue)) + pfootnotes = footnotes + footnote_count - 1; + } + CALLOC(pfootnotes->key, uint8_t, KEYSIZE) + u8_strncpy(pfootnotes->key, token, KEYSIZE-1); + pfootnotes->value = NULL; + pfootnotes->value_size = 0; + } + + if (footnote_output) + { + print_output(output, "(%d)", current_footnote); + output_colno += 3; + } + + return 0; +} + +int +process_horizontal_rule(FILE* output) +{ + /* Temporarily break paragraph as hr is para-level */ + if (state & ST_PARA_OPEN) + print_output(output, "\n\n"); + print_output(output, "————————————————————\n\n"); + output_colno = 1; + return 0; +} + +int +process_formula(FILE* output, const uint8_t* token, BOOL display_formula) +{ + int result = 0; + return result; +} + +int +begin_article(FILE* output, const BOOL add_article_header, + const uint8_t* author, const uint8_t* title, + const uint8_t* header_text, const char* title_heading_level, + uint8_t* date, const BOOL ext_in_permalink, const char* permalink_url) +{ + if (title) + { + process_heading_start(output, 1); + process_heading(title, output); + } + + if (header_text) + { + print_output(output, "%s\n\n", (char*)header_text); + output_colno = 1; + } + + if (author) + { + print_output(output, "%s\n\n", author); + output_colno = 1; + } + + if (date && input_filename) + { + char* link = strip_ext(input_filename); + /* + *uint8_t* samedir_permalink = get_value(vars, vars_count, + * (uint8_t*)"samedir-permalink", NULL); + */ + char* real_link = NULL; + /* + *uint8_t* permalink_macro = get_value(macros, macros_count, + * (uint8_t*)"permalink", NULL); + */ + CALLOC(real_link, char, BUFSIZE) + + if (ext_in_permalink) + strncat(link, timestamp_output_ext, BUFSIZE-strlen(link)-1); + + /* + *if (permalink_url) + * process_timestamp(output, permalink_url, permalink_macro, date, + * NULL); + *else //if (samedir_permalink && !u8_strcmp(samedir_permalink, (uint8_t*)"1")) + *{ + * get_realpath(&real_link, input_dirname, link); + * process_timestamp(output, real_link, permalink_macro, date, NULL); + *} + */ + /* + *else + * process_timestamp(output, link, permalink_macro, date); + */ + + free(real_link); + free(link); + } + + return 0; +} + +int +end_footnotes(FILE* output, BOOL add_footnote_div) +{ + size_t footnote = 0; + + if (state & ST_PARA_OPEN) + { + print_output(output, "\n\n"); + output_colno = 1; + state &= ~ST_PARA_OPEN; + process_paralinks(output); + } + + process_horizontal_rule(output); + + for (footnote = 0; footnote < inline_footnote_count; footnote++) + { + print_output(output, "%d. %s\n\n", footnote+1, + (char*)inline_footnotes[footnote]); + output_colno = 1; + } + + KeyValue* pfootnote = footnotes; + footnote = 0; + while (pfootnote && footnote < footnote_count) + { + print_output(output, "%d. %s\n\n", footnote+1, (char*)pfootnote->value); + output_colno = 1; + pfootnote++; + footnote++; + } + + return 0; +} + +int +slweb_parse(uint8_t* buffer, FILE* output, BOOL read_yaml_macros_and_links) +{ + uint8_t* title = NULL; + uint8_t* title_heading_level = NULL; + uint8_t* header_text = NULL; + uint8_t* author = NULL; + uint8_t* date = NULL; + uint8_t* permalink_url = NULL; + uint8_t* ext_in_permalink = NULL; + uint8_t* var_add_article_header = NULL; + uint8_t* var_add_image_links = NULL; + uint8_t* var_add_figcaption = NULL; + uint8_t* var_add_footnote_div = NULL; + uint8_t* pbuffer = NULL; + uint8_t* line = NULL; + uint8_t* pline = NULL; + size_t line_len = 0; + uint8_t* token = NULL; + uint8_t* ptoken = NULL; + size_t token_size = 0; + uint8_t* link_text = NULL; + size_t link_size = 0; + uint8_t* link_macro = NULL; + UBYTE heading_level = 0; + BOOL end_tag = FALSE; + BOOL first_line_in_doc = TRUE; + BOOL skip_change_first_line_in_doc = FALSE; + BOOL skip_eol = FALSE; + BOOL keep_token = FALSE; + BOOL previous_line_blank = FALSE; + BOOL processed_start_of_line = FALSE; + BOOL add_image_links = TRUE; + BOOL add_figcaption = TRUE; + BOOL add_footnote_div = FALSE; + BOOL list_para = FALSE; + BOOL footnote_at_line_start = FALSE; + size_t pline_len = 0; + + if (!buffer) + exit(error(1, (uint8_t*)"Empty buffer")); + + if (!vars) + exit(error(EINVAL, (uint8_t*)"Invalid argument (vars)")); + + if (!links) + exit(error(EINVAL, (uint8_t*)"Invalid argument (links)")); + + if (!macros) + exit(error(EINVAL, (uint8_t*)"Invalid argument (macros)")); + + title = get_value(vars, vars_count, (uint8_t*)"title", NULL); + title_heading_level = get_value(vars, vars_count, + (uint8_t*)"title-heading-level", NULL); + header_text = get_value(vars, vars_count, (uint8_t*)"header-text", NULL); + author = get_value(vars, vars_count, (uint8_t*)"author", NULL); + date = get_value(vars, vars_count, (uint8_t*)"date", NULL); + permalink_url = get_value(vars, vars_count, (uint8_t*)"permalink-url", NULL); + ext_in_permalink = get_value(vars, vars_count, (uint8_t*)"ext-in-permalink", NULL); + var_add_article_header = get_value(vars, vars_count, (uint8_t*)"add-article-header", NULL); + var_add_image_links = get_value(vars, vars_count, (uint8_t*)"add-image-links", NULL); + add_image_links = !(var_add_image_links && *var_add_image_links == '0'); + var_add_figcaption = get_value(vars, vars_count, (uint8_t*)"add-figcaption", NULL); + add_figcaption = !(var_add_figcaption && *var_add_figcaption == '0'); + var_add_footnote_div = get_value(vars, vars_count, (uint8_t*)"add-footnote-div", NULL); + add_footnote_div = var_add_footnote_div && *var_add_footnote_div == '1'; + + CALLOC(line, uint8_t, BUFSIZE) + token_size = BUFSIZE; + CALLOC(token, uint8_t, BUFSIZE) + CALLOC(link_macro, uint8_t, BUFSIZE) + + pbuffer = buffer; + pvars = vars; + pmacros = macros; + plinks = links; + pparalinks = paralinks; + pfootnotes = footnotes; + lineno = 0; + + if (!read_yaml_macros_and_links) + begin_article(output, + var_add_article_header && *var_add_article_header == '1', + author, title, header_text, (char*)title_heading_level, date, + ext_in_permalink && *ext_in_permalink != '0', + (char*)permalink_url); + + RESET_TOKEN(token, ptoken, token_size) + + do + { + uint8_t* eol = u8_strchr(pbuffer, (ucs4_t)'\n'); + if (!eol) + break; + + pline = line; + while (pbuffer != eol) + *pline++ = *pbuffer++; + pbuffer++; + *pline = 0; + pline = line; + line_len = u8_strlen(line); + + lineno++; + colno = 1; + output_colno = 1; + processed_start_of_line = FALSE; + skip_eol = FALSE; + /*list_item = FALSE;*/ + list_para = FALSE; + + while (pline && *pline) + { + switch (*pline) + { + case '-': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_MACRO_BODY | ST_PRE | ST_YAML_VAL)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + else if (colno == 1 + && u8_strlen(pline) > 2 + && startswith((char*)pline, "---")) + { + skip_eol = TRUE; + + if (!(state & ST_YAML) && lineno > 1 + && !read_yaml_macros_and_links) + process_horizontal_rule(output); + else + { + if (lineno == 1) + state |= ST_YAML; + else + state &= ~ST_YAML; + + skip_change_first_line_in_doc = TRUE; + } + pline = NULL; + } + else if (colno == 1 + && u8_strlen(pline) > 1 + && *(pline+1) == ' ') + { + if (state & ST_NUMLIST) + { + state &= ~ST_NUMLIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_numlist_end(output); + } + } + if (!read_yaml_macros_and_links) + { + if (!(state & ST_LIST)) + process_list_start(output); + else + process_list_item_end(output); + process_list_item_start(output); + } + + processed_start_of_line = TRUE; + skip_eol = FALSE; + + state |= ST_LIST; + + pline += 2; + colno += 2; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case ':': + if (state & ST_YAML + && !(ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_HEADING | ST_IMAGE | ST_MACRO_BODY + | ST_PRE | ST_TAG | ST_YAML_VAL)) + && read_yaml_macros_and_links) + { + *ptoken = 0; + + vars_count++; + + if (vars_count > 1) + { + REALLOCARRAY(vars, KeyValue, vars_count) + pvars = vars + vars_count - 1; + } + CALLOC(pvars->key, uint8_t, KEYSIZE) + u8_strncpy(pvars->key, token, KEYSIZE-1); + pvars->value = NULL; + pvars->value_size = 0; + + state |= ST_YAML_VAL; + RESET_TOKEN(token, ptoken, token_size) + pline++; + colno++; + + while (pline && (*pline == ' ' || *pline == '\t')) + { + pline++; + colno++; + } + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + else { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case '`': + if (ANY(state, ST_DISPLAY_FORMULA | ST_FORMULA | ST_IMAGE + | ST_MACRO_BODY)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (colno == 1 + && u8_strlen(pline) > 2 + && startswith((char*)pline, "```")) + { + state ^= ST_PRE; + + if (!read_yaml_macros_and_links) + { + if (state & ST_PRE) + print_output(output, "\n```\n"); + else + print_output(output, "\n```\n\n"); + output_colno = 1; + } + + /* Skip the rest of the line (language) */ + pline = NULL; + } + else if (!ANY(state, ST_PRE | ST_TAG | ST_YAML)) + { + /* Handle ` within footnotes, headings and link text specially */ + if (ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING + | ST_FOOTNOTE_TEXT | ST_LINK)) + { + uint8_t* tag = state & ST_CODE + ? (uint8_t*)"</code>" + : (uint8_t*)"<code>"; + size_t tag_len = u8_strlen(tag); + size_t token_len = 0; + + *ptoken = 0; + token_len = u8_strlen(token); + + if (token_len + tag_len < BUFSIZE) + { + u8_strncat(token, tag, BUFSIZE-token_len-1); + ptoken += tag_len; + } + } + else + { + /* Output existing text up to ` */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, + &token_size, TRUE); + processed_start_of_line = TRUE; + + if (!read_yaml_macros_and_links + && !(ANY(state, ST_PRE | ST_HEADING))) + process_code(output, state & ST_CODE); + } + + state ^= ST_CODE; + + pline++; + colno++; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case '#': + if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + if (colno == 1 + && !(ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_MACRO_BODY | ST_PRE | ST_YAML))) + { + if (state & ST_LIST) + { + state &= ~ST_LIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_list_end(output); + } + } + + if (state & ST_NUMLIST) + { + state &= ~ST_NUMLIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_numlist_end(output); + } + } + + state |= ST_HEADING; + heading_level = 1; + pline++; + colno++; + } + else if (state & ST_HEADING && *(pline-1) == '#') + { + if (heading_level < MAX_HEADING_LEVEL) + heading_level++; + pline++; + colno++; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case '_': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + /*| ST_HTML_TAG */| ST_IMAGE | ST_LINK_SECOND_ARG + | ST_LINK_SECOND_ARG_END | ST_MACRO_BODY | ST_PRE + | ST_TAG | ST_YAML)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (u8_strlen(pline) > 1 && *(pline+1) == '_') + { + /* Handle __ within footnotes, headings and link text specially */ + if (!ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING + | ST_FOOTNOTE_TEXT | ST_LINK)) + { + /* Output existing text up to __ */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, + &token_size, TRUE); + processed_start_of_line = TRUE; + + if (!read_yaml_macros_and_links + && !(ANY(state, ST_PRE | ST_CODE | ST_HEADING))) + process_bold(output, state & ST_BOLD); + } + + state ^= ST_BOLD; + pline += 2; + colno += 2; + } + else + { + /* Handle _ within footnotes, headings and link text specially */ + if (!ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING + | ST_FOOTNOTE_TEXT | ST_LINK)) + { + /* Output existing text up to _ */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, + &token_size, TRUE); + processed_start_of_line = TRUE; + + if (!read_yaml_macros_and_links + && !(ANY(state, ST_PRE | ST_CODE | ST_HEADING))) + process_italic(output, state & ST_ITALIC); + } + + state ^= ST_ITALIC; + pline++; + colno++; + } + break; + + case 'o': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + /*| ST_HTML_TAG */| ST_IMAGE | ST_MACRO_BODY | ST_PRE + | ST_YAML)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (colno == 1 + && u8_strlen(pline) > 1 + && *(pline+1) == ' ') + { + if (state & ST_NUMLIST) + { + state &= ~ST_NUMLIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_numlist_end(output); + } + } + if (!read_yaml_macros_and_links) + { + if (!(state & ST_LIST)) + process_list_start(output); + else + process_list_item_end(output); + process_list_item_start(output); + } + + processed_start_of_line = TRUE; + skip_eol = FALSE; + + state |= ST_LIST; + + pline += 2; + colno += 2; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline); + colno++; + } + break; + + case '*': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + /*| ST_HTML_TAG */| ST_IMAGE | ST_MACRO_BODY | ST_PRE + | ST_YAML)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + pline_len = u8_strlen(pline); + if (colno == 1 + && pline_len > 1 && *(pline+1) == '[') + { + process_line_start(line, first_line_in_doc, + previous_line_blank, read_yaml_macros_and_links, + list_para, output, &token, &ptoken); + + /* Ignore abbreviations (for now) */ + pline = NULL; + } + else if (colno == 1 + && pline_len > 2 && startswith((char*)pline, "***")) + { + skip_eol = TRUE; + if (!read_yaml_macros_and_links) + process_horizontal_rule(output); + pline = NULL; + } + else if (colno == 1 + && u8_strlen(pline) > 1 + && *(pline+1) == ' ') + { + if (state & ST_NUMLIST) + { + state &= ~ST_NUMLIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_numlist_end(output); + } + } + if (!read_yaml_macros_and_links) + { + if (!(state & ST_LIST)) + process_list_start(output); + else + process_list_item_end(output); + process_list_item_start(output); + } + + processed_start_of_line = TRUE; + skip_eol = FALSE; + + state |= ST_LIST; + + pline += 2; + colno += 2; + } + else if (pline_len > 1 && *(pline+1) == '*') + { + /* Handle ** within footnotes, headings and link text specially */ + if (!ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING + | ST_FOOTNOTE_TEXT | ST_LINK)) + { + /* Output existing text up to * */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, + &token_size, TRUE); + processed_start_of_line = TRUE; + + if (!read_yaml_macros_and_links + && !(ANY(state, ST_PRE | ST_CODE | ST_HEADING))) + process_bold(output, state & ST_BOLD); + } + + state ^= ST_BOLD; + pline += 2; + colno += 2; + } + else + { + /* Handle * within footnotes, headings and link text specially */ + if (!ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING + | ST_FOOTNOTE_TEXT | ST_LINK)) + + { + /* Output existing text up to * */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, + &token_size, TRUE); + processed_start_of_line = TRUE; + + if (!read_yaml_macros_and_links + && !(ANY(state, ST_PRE | ST_CODE | ST_HEADING))) + process_italic(output, state & ST_ITALIC); + } + + state ^= ST_ITALIC; + pline++; + colno++; + } + break; + + case ' ': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_IMAGE | ST_MACRO_BODY | ST_PRE | ST_YAML_VAL)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + + if ((state & ST_HEADING) && !(state & ST_HEADING_TEXT)) + { + if (!read_yaml_macros_and_links) + process_heading_start(output, heading_level); + state |= ST_HEADING_TEXT; + pline++; + colno++; + break; + } + + if (colno == 1 + && u8_strlen(pline) > 1 + && startswith((char*)pline, " ")) + { + list_para = TRUE; + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, + &token_size, TRUE); + processed_start_of_line = TRUE; + while (*pline == ' ' || *pline == '\t') + { + pline++; + colno++; + } + break; + } + + if (u8_strlen(pline) == 2 + && *(pline+1) == ' ') + { + *ptoken = 0; + u8_strncat(ptoken, (uint8_t*)"\n", + BUFSIZE-u8_strlen(token)-1); + ptoken += strlen("\n"); + output_colno = 1; + pline++; + colno++; + } + else if (state & ST_LINK_MACRO) + { + *ptoken = 0; + u8_strncpy(link_macro, token, BUFSIZE-1); + *(link_macro + u8_strlen(token)) = 0; + RESET_TOKEN(token, ptoken, token_size) + state &= ~ST_LINK_MACRO; + } + else if (!(state & ST_HEADING + && *(pline-1) == '#')) + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + + case '{': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + /*| ST_HTML_TAG*/ | ST_PRE | ST_YAML | ST_YAML_VAL)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (state & ST_MACRO_BODY) + { + *ptoken = 0; + size_t token_len = u8_strlen(token); + + skip_eol = TRUE; + if (read_yaml_macros_and_links) + { + if (pmacros->value) + { + size_t value_len = u8_strlen(pmacros->value); + + if (pmacros->value_size < value_len + token_len + 1) + { + pmacros->value_size += token_len; + REALLOC(pmacros->value, uint8_t, pmacros->value_size) + } + u8_strncat(pmacros->value, token, pmacros->value_size-1); + } + else + { + pmacros->value_size = token_len+1; + CALLOC(pmacros->value, uint8_t, pmacros->value_size) + u8_strncpy(pmacros->value, token, pmacros->value_size-1); + } + } + } + else + { + /* Output existing text up to { */ + process_text_token(line, first_line_in_doc, + previous_line_blank, processed_start_of_line, + read_yaml_macros_and_links, list_para, output, + &token, &ptoken, &token_size, FALSE); + processed_start_of_line = TRUE; + } + + state |= ST_TAG; + RESET_TOKEN(token, ptoken, token_size) + + pline++; + colno++; + break; + + case '/': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_IMAGE | ST_PRE | ST_HEADING | ST_YAML_VAL)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if ((state & ST_TAG) && pline-1 && *(pline-1) == '{') + { + end_tag = TRUE; + pline++; + } + else + CHECKCOPY(token, ptoken, token_size, pline) + + colno++; + break; + + case '}': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_IMAGE | ST_PRE | ST_YAML | ST_YAML_VAL)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + + if (state & ST_TAG) + { + state &= ~ST_TAG; + *ptoken = 0; + + process_tag(token, output, read_yaml_macros_and_links, + &skip_eol, end_tag); + + RESET_TOKEN(token, ptoken, token_size) + end_tag = FALSE; + } + + pline++; + colno++; + break; + + case '|': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + /*| ST_HTML_TAG*/ | ST_IMAGE | ST_PRE | ST_TAG + | ST_YAML)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (u8_strlen(pline) > 1 && *(pline+1) == '|') + { + /* Handle || within footnotes, headings and link text specially */ + if (ANY(state, ST_INLINE_FOOTNOTE | ST_HEADING + | ST_FOOTNOTE_TEXT | ST_LINK)) + { + uint8_t* tag = state & ST_KBD + ? (uint8_t*)"</kbd>" + : (uint8_t*)"<kbd>"; + size_t tag_len = u8_strlen(tag); + size_t token_len = 0; + + *ptoken = 0; + token_len = u8_strlen(token); + + if (token_len + tag_len < BUFSIZE) + { + u8_strncat(token, tag, BUFSIZE-token_len-1); + ptoken += tag_len; + } + } + else + { + /* Output existing text up to || */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, + &token_size, TRUE); + processed_start_of_line = TRUE; + + if (!read_yaml_macros_and_links + && !(ANY(state, ST_PRE | ST_CODE | ST_HEADING))) + { + state ^= ST_KBD; + process_kbd(output, !(state & ST_KBD)); + } + } + + pline += 2; + colno += 2; + } + /* Partial tables (for templating) */ + else if (!(state & ST_PRE) && colno == 1 && u8_strlen(pline) > 1 + && *(pline+1) == '@') + { + skip_eol = TRUE; + + pline += 2; + colno += 2; + + switch (*pline) + { + case '\\': + if (!read_yaml_macros_and_links) + { + process_table_start(output); + process_table_header_start(output); + } + pline = NULL; + break; + + case '#': + state |= ST_TABLE_HEADER; + pline++; + colno++; + break; + + case '-': + state &= ~ST_TABLE_HEADER; + if (!read_yaml_macros_and_links) + process_table_body_start(output, FALSE); + pline = NULL; + break; + + case ' ': + state |= ST_TABLE; + if (!read_yaml_macros_and_links) + process_table_body_row_start(output); + break; + + case '/': + state &= ~ST_TABLE; + process_table_end(output); + pline = NULL; + break; + + default: + warning(1, (uint8_t*)"Invalid partial table line"); + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + } + else if (!(state & ST_PRE) && colno == 1) + { + skip_eol = TRUE; + + if (state & ST_TABLE_HEADER) + { + state &= ~ST_TABLE_HEADER; + state |= ST_TABLE_LINE; + pline = NULL; + break; + } + + if (state & ST_TABLE_LINE) + { + state &= ~ST_TABLE_LINE; + state |= ST_TABLE; + if (!read_yaml_macros_and_links) + process_table_body_start(output, TRUE); + } + else if (state & ST_TABLE) + { + if (!read_yaml_macros_and_links) + process_table_body_row_start(output); + } + else + { + state |= ST_TABLE_HEADER; + if (!read_yaml_macros_and_links) + { + process_table_start(output); + process_table_header_start(output); + } + } + + pline++; + colno++; + } + else if (state & ST_TABLE_HEADER) + { + *ptoken = 0; + if (!read_yaml_macros_and_links) + { + print_output(output, "%s", token); + output_colno += u8_strlen(token); + if (u8_strlen(pline) > 1) + process_table_header_cell(output); + else + process_table_header_end(output); + } + RESET_TOKEN(token, ptoken, token_size) + pline++; + colno++; + } + else if (state & ST_TABLE) + { + *ptoken = 0; + if (!read_yaml_macros_and_links) + { + print_output(output, "%s", token); + output_colno += u8_strlen(token); + if (u8_strlen(pline) > 1) + process_table_body_cell(output); + else + process_table_body_row_end(output); + } + RESET_TOKEN(token, ptoken, token_size) + pline++; + colno++; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case '<': + if (read_yaml_macros_and_links + || ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_IMAGE | ST_PRE)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + + /* + *if (ANY(state, ST_CODE | ST_KBD | ST_PRE)) + *{ + * size_t lt_len = u8_strlen((uint8_t*)"&lt;"); + * *ptoken = 0; + * u8_strncat(token, (uint8_t*)"&lt;", + * BUFSIZE-(ptoken-token)-1); + * ptoken += lt_len; + * pline++; + * colno++; + * break; + *} + */ + + state |= ST_HTML_TAG; + /*CHECKCOPY(token, ptoken, token_size, pline)*/ + pline++; + colno++; + break; + + case '>': + if (state & ST_HTML_TAG) + { + state &= ~ST_HTML_TAG; + pline++; + colno++; + break; + } + + if (ANY(state, ST_DISPLAY_FORMULA | ST_FORMULA | ST_IMAGE + | ST_MACRO_BODY)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + + if (!read_yaml_macros_and_links + && !ANY(state, ST_CODE | ST_HEADING | ST_PRE) + && colno == 1) + { + if (!read_yaml_macros_and_links + && !(state & ST_BLOCKQUOTE)) + process_blockquote(output, FALSE); + + state |= ST_BLOCKQUOTE; + + pline++; + colno++; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case '!': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_FOOTNOTE_TEXT | ST_HEADING | ST_IMAGE + | ST_INLINE_FOOTNOTE | ST_LINK | ST_MACRO_BODY + | ST_PRE)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (u8_strlen(pline) > 1 && *(pline+1) == '[') + { + /* Output existing text up to ! */ + *ptoken = 0; + process_text_token(line, first_line_in_doc, + previous_line_blank, processed_start_of_line, + read_yaml_macros_and_links, list_para, output, + &token, &ptoken, &token_size, FALSE); + processed_start_of_line = TRUE; + + RESET_TOKEN(token, ptoken, token_size) + + state |= ST_IMAGE; + keep_token = TRUE; + pline += 2; + colno += 2; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + + break; + + case '=': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_MACRO_BODY | ST_PRE | ST_TAG)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (colno != 1 && *(pline-1) == '[') + { + state |= ST_LINK_MACRO; + pline++; + colno++; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case '[': + pline_len = u8_strlen(pline); + + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_HEADING | ST_IMAGE | ST_MACRO_BODY | ST_PRE)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (state & ST_FOOTNOTE_TEXT) + { + if (!read_yaml_macros_and_links && (state & ST_PARA_OPEN)) + { + print_output(output, "\n\n"); + output_colno = 1; + process_paralinks(output); + } + state &= ~(ST_FOOTNOTE_TEXT | ST_PARA_OPEN); + } + + if (pline_len > 1 && *(pline+1) == '^') + { + footnote_at_line_start = colno == 1; + if (*token) + { + /* Output existing text up to [^ */ + *ptoken = 0; + process_text_token(line, first_line_in_doc, + previous_line_blank, processed_start_of_line, + read_yaml_macros_and_links, list_para, output, + &token, &ptoken, &token_size, TRUE); + } + processed_start_of_line = TRUE; + + RESET_TOKEN(token, ptoken, token_size) + state |= ST_FOOTNOTE; + pline += 2; + colno += 2; + break; + } + + if (*token) + { + /* Output existing text up to [ */ + *ptoken = 0; + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, &token_size, + TRUE); + } + processed_start_of_line = TRUE; + + RESET_TOKEN(token, ptoken, token_size) + state |= ST_LINK; + keep_token = TRUE; + *link_macro = 0; + pline++; + colno++; + break; + + case '(': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_FOOTNOTE_TEXT | ST_HEADING + | ST_INLINE_FOOTNOTE | ST_LINK_SECOND_ARG_END + | ST_MACRO_BODY | ST_PRE)) + CHECKCOPY(token, ptoken, token_size, pline) + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + else if (state & ST_LINK) + { + uint8_t* tag = (uint8_t*)"<span>"; + size_t tag_len = u8_strlen(tag); + size_t token_len = 0; + + *ptoken = 0; + token_len = u8_strlen(token); + + if (token_len + tag_len < BUFSIZE) + { + u8_strncat(token, tag, BUFSIZE-token_len-1); + ptoken += tag_len; + } + + state |= ST_LINK_SPAN; + pline++; + } + else + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + + case ')': + if (ANY(state, ST_DISPLAY_FORMULA | ST_FORMULA + | ST_LINK_SECOND_ARG_END)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (state & ST_LINK_SPAN) + { + if (u8_strlen(pline) > 1 + && *(pline+1) == ']') + { + uint8_t* tag = (uint8_t*)"</span>"; + size_t tag_len = u8_strlen(tag); + size_t token_len = 0; + + *ptoken = 0; + token_len = u8_strlen(token); + + if (token_len + tag_len + 1 < BUFSIZE) + { + u8_strncat(token, tag, BUFSIZE-token_len-1); + ptoken += tag_len; + } + state &= ~ST_LINK_SPAN; + } + pline++; + } + else if (ANY(state, ST_LINK_SECOND_ARG | ST_IMAGE_SECOND_ARG)) + { + *ptoken = 0; + if (!read_yaml_macros_and_links) + { + if (state & ST_LINK_SECOND_ARG) + process_inline_link(link_text, + get_value(macros, macros_count, + link_macro, NULL), + token, output); + else + process_inline_image(link_text, token, output, + add_image_links, add_figcaption); + } + RESET_TOKEN(token, ptoken, token_size) + state &= ~(ST_LINK | ST_LINK_SECOND_ARG + | ST_IMAGE | ST_IMAGE_SECOND_ARG); + pline++; + } + else + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + + case ']': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_HEADING | ST_MACRO_BODY | ST_PRE)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + *ptoken = 0; + if (state & ST_INLINE_FOOTNOTE) + { + process_inline_footnote(token, read_yaml_macros_and_links, + output); + + keep_token = FALSE; + RESET_TOKEN(token, ptoken, token_size) + state &= ~ST_INLINE_FOOTNOTE; + pline++; + colno++; + } + else if (state & ST_FOOTNOTE) + { + BOOL footnote_definition = (u8_strlen(pline) > 1) + && (*(pline+1) == ':') && footnote_at_line_start; + + process_footnote(token, + footnote_definition && read_yaml_macros_and_links, + !footnote_definition && !read_yaml_macros_and_links, + output); + + RESET_TOKEN(token, ptoken, token_size) + state &= ~ST_FOOTNOTE; + footnote_at_line_start = FALSE; + pline++; + colno++; + if (footnote_definition) + { + pline++; + colno++; + while (*pline == ' ' || *pline == '\t') + { + pline++; + colno++; + } + state |= ST_FOOTNOTE_TEXT; + } + } + else if (state & ST_LINK_SECOND_ARG) + { + if (!read_yaml_macros_and_links) + process_link(link_text, + get_value(macros, macros_count, + link_macro, NULL), + token, output); + RESET_TOKEN(token, ptoken, token_size) + state &= ~(ST_LINK | ST_LINK_SECOND_ARG); + pline++; + colno++; + } + else if (state & ST_IMAGE_SECOND_ARG) + { + if (!read_yaml_macros_and_links) + process_image(link_text, token, output, + add_image_links, add_figcaption); + RESET_TOKEN(token, ptoken, token_size) + state &= ~(ST_IMAGE | ST_IMAGE_SECOND_ARG); + pline++; + colno++; + } + else if (u8_strlen(pline) > 1) + { + size_t token_len = 0; + switch (*(pline+1)) + { + case ':': + links_count++; + + if (links_count > 1) + { + REALLOCARRAY(links, KeyValue, links_count) + plinks = links + links_count - 1; + } + CALLOC(plinks->key, uint8_t, KEYSIZE) + u8_strncpy(plinks->key, token, KEYSIZE-1); + plinks->value = NULL; + plinks->value_size = 0; + pline += 2; + colno += 2; + while (pline && (*pline == ' ' || *pline == '\t')) + { + pline++; + colno++; + } + RESET_TOKEN(token, ptoken, token_size) + keep_token = FALSE; + state |= ST_LINK_SECOND_ARG_END; + break; + + case '[': + case '(': + token_len = u8_strlen(token); + if (link_text) + { + if (token_len + 1 > link_size) + { + link_size = token_len+1; + REALLOC(link_text, uint8_t, link_size) + } + } + else + { + link_size = token_len+1; + CALLOC(link_text, uint8_t, link_size) + } + u8_strncpy(link_text, token, link_size-1); + *(link_text + token_len) = 0; + pline += 2; + colno += 2; + RESET_TOKEN(token, ptoken, token_size) + keep_token = FALSE; + if (state & ST_LINK) + state |= ST_LINK_SECOND_ARG; + else + state |= ST_IMAGE_SECOND_ARG; + break; + default: + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + state &= ~(ST_LINK | ST_IMAGE); + break; + } + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + case '^': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_FOOTNOTE_TEXT | ST_IMAGE | ST_INLINE_FOOTNOTE + | ST_PRE | ST_YAML)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (u8_strlen(pline) > 1 && *(pline+1) == '[') + { + /* Output existing text up to ^[ */ + *ptoken = 0; + process_text_token(line, first_line_in_doc, + previous_line_blank, processed_start_of_line, + read_yaml_macros_and_links, list_para, output, + &token, &ptoken, &token_size, TRUE); + processed_start_of_line = TRUE; + + RESET_TOKEN(token, ptoken, token_size) + state |= ST_INLINE_FOOTNOTE; + keep_token = TRUE; + pline += 2; + colno += 2; + break; + } + + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + + case '\\': + *ptoken = 0; + if (ANY(state, ST_DISPLAY_FORMULA | ST_FORMULA + /*| ST_HTML_TAG*/ | ST_IMAGE | ST_LINK + | ST_MACRO_BODY)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (u8_strlen(pline) > 1) + { + pline++; + colno++; + + CHECKCOPY(token, ptoken, token_size, pline) + } + else + { + pline = NULL; + break; + } + + colno++; + break; + + case '$': + if (ANY(state, ST_CODE | ST_CSV_BODY | ST_IMAGE | ST_PRE + | ST_YAML)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + break; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + + if (u8_strlen(pline) > 1 + && *(pline+1) == '$') + { + if (state & ST_FORMULA) + exit(error(1, (uint8_t*)"Display formula within an" + " open formula")); + + if (!(state & ST_DISPLAY_FORMULA)) + { + /* Output existing text up to $$ */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, &token_size, + TRUE); + } + processed_start_of_line = TRUE; + + state ^= ST_DISPLAY_FORMULA; + + if (state & ST_DISPLAY_FORMULA) + keep_token = TRUE; + else + { + *ptoken = 0; + + if (!read_yaml_macros_and_links) + process_formula(output, token, TRUE); + + keep_token = FALSE; + RESET_TOKEN(token, ptoken, token_size) + } + + pline += 2; + colno += 2; + } + else + { + if (state & ST_DISPLAY_FORMULA) + exit(error(1, (uint8_t*)"Formula within an open" + " display formula")); + + if (!(state & ST_FORMULA)) + { + /* Output existing text up to $ */ + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, &token_size, + TRUE); + } + processed_start_of_line = TRUE; + + state ^= ST_FORMULA; + + if (state & ST_FORMULA) + keep_token = TRUE; + else + { + *ptoken = 0; + + if (!read_yaml_macros_and_links) + process_formula(output, token, FALSE); + + keep_token = FALSE; + RESET_TOKEN(token, ptoken, token_size) + } + + pline++; + colno++; + } + break; + + case '1': case '2': case '3': case '4': case '5': + case '6': case '7': case '8': case '9': case '0': + if (ANY(state, ST_CODE | ST_DISPLAY_FORMULA | ST_FORMULA + | ST_MACRO_BODY | ST_PRE | ST_YAML_VAL)) + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + else if (state & ST_HTML_TAG) + { + pline++; + colno++; + break; + } + else if (colno == 1 + && u8_strlen(pline) > 1 + && (*(pline+1) == '.' || *(pline+1) == ')')) + { + if (state & ST_LIST) + { + state &= ~ST_LIST; + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_list_end(output); + } + } + if (!read_yaml_macros_and_links) + { + if (!(state & ST_NUMLIST)) + process_numlist_start(output); + else + process_list_item_end(output); + process_list_item_start(output); + } + + processed_start_of_line = TRUE; + skip_eol = FALSE; + + state |= ST_NUMLIST; + + pline += 2; + colno += 2; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + break; + + default: + if (state & ST_HTML_TAG) + { + pline++; + colno++; + } + else + { + CHECKCOPY(token, ptoken, token_size, pline) + colno++; + } + } + } + + *ptoken = 0; + + if (*token && read_yaml_macros_and_links + && (state & ST_YAML_VAL)) + { + *ptoken = 0; + pvars->value_size = u8_strlen(token)+1; + CALLOC(pvars->value, uint8_t, pvars->value_size) + u8_strncpy(pvars->value, token, pvars->value_size-1); + } + else if (keep_token) + { + size_t token_len = u8_strlen(token); + + if (ptoken + 2 > token + token_size) + { + size_t old_size = token_size; + token_size += BUFSIZE; + REALLOC(token, uint8_t, token_size) + ptoken = token + old_size - 1; + } + u8_strncat(token, (uint8_t*)" ", + token_size - token_len - 1); + ptoken++; + *ptoken = 0; + } + else + { + if (*token) + { + *ptoken = 0; + size_t token_len = u8_strlen(token); + if (state & ST_MACRO_BODY) + { + skip_eol = TRUE; + if (read_yaml_macros_and_links) + { + if (pmacros->value) + { + size_t value_len = u8_strlen(pmacros->value); + + if (pmacros->value_size < value_len + token_len + 1) + { + pmacros->value_size += BUFSIZE; + REALLOC(pmacros->value, uint8_t, pmacros->value_size) + } + u8_strncat(pmacros->value, token, + pmacros->value_size-value_len-1); + u8_strncat(pmacros->value, (uint8_t*)" ", + pmacros->value_size-u8_strlen(pmacros->value)-1); + } + else + { + pmacros->value_size = BUFSIZE; + CALLOC(pmacros->value, uint8_t, pmacros->value_size) + u8_strncpy(pmacros->value, token, pmacros->value_size-1); + u8_strncat(pmacros->value, (uint8_t*)" ", + pmacros->value_size-u8_strlen(pmacros->value)-1); + } + } + } + else if (state & ST_FOOTNOTE_TEXT) + { + skip_eol = TRUE; + if (read_yaml_macros_and_links) + { + if (pfootnotes->value) + { + size_t value_len = u8_strlen(pfootnotes->value); + + if (pfootnotes->value_size < value_len + token_len + 1) + { + pfootnotes->value_size += BUFSIZE; + REALLOC(pfootnotes->value, uint8_t, pfootnotes->value_size) + } + u8_strncat(pfootnotes->value, token, + pfootnotes->value_size-value_len-1); + u8_strncat(pfootnotes->value, (uint8_t*)" ", + pfootnotes->value_size-u8_strlen(pfootnotes->value)-1); + } + else + { + pfootnotes->value_size = BUFSIZE; + CALLOC(pfootnotes->value, uint8_t, pfootnotes->value_size) + u8_strncpy(pfootnotes->value, token, pfootnotes->value_size-1); + u8_strncat(pfootnotes->value, (uint8_t*)" ", + pfootnotes->value_size-u8_strlen(pfootnotes->value)-1); + } + } + } + else if (ANY(state, ST_LINK_SECOND_ARG | ST_LINK_SECOND_ARG_END)) + { + if (read_yaml_macros_and_links) + { + plinks->value_size = u8_strlen(token)+1; + CALLOC(plinks->value, uint8_t, plinks->value_size) + u8_strncpy(plinks->value, token, plinks->value_size-1); + } + } + else if (state & ST_HEADING) + { + state &= ~(ST_HEADING | ST_HEADING_TEXT); + if (!read_yaml_macros_and_links) + process_heading(token, output); + first_line_in_doc = FALSE; + RESET_TOKEN(token, ptoken, token_size) + heading_level = 0; + } + else if (!ANY(state, ST_DISPLAY_FORMULA | ST_FORMULA + | ST_IMAGE | ST_LINK)) + process_text_token(line, first_line_in_doc, + previous_line_blank, + processed_start_of_line, + read_yaml_macros_and_links, + list_para, output, &token, &ptoken, &token_size, + TRUE); + } + + if (!ANY(state, ST_PRE | ST_IMAGE_SECOND_ARG | ST_LINK_SECOND_ARG + | ST_LINK_SECOND_ARG_END) + && (!*pbuffer || *pbuffer == '\n')) + { + if (state & ST_PARA_OPEN) + { + if (!read_yaml_macros_and_links) + { + print_output(output, "%s\n", + (line_len == 0) || (output_colno == 1) + || previous_line_blank + ? "" : "\n"); + output_colno = 1; + process_paralinks(output); + } + if (state & ST_LIST) + skip_eol = TRUE; + state &= ~ST_PARA_OPEN; + } + + if (!*pbuffer && (state & ST_LIST)) + { + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_list_end(output); + } + state &= ~ST_LIST; + } + + if (!*pbuffer && (state & ST_NUMLIST)) + { + if (!read_yaml_macros_and_links) + { + process_list_item_end(output); + process_numlist_end(output); + } + state &= ~ST_NUMLIST; + } + + if (!*pbuffer && (state & ST_FOOTNOTE_TEXT)) + { + state &= ~ST_FOOTNOTE_TEXT; + + *ptoken = 0; + size_t token_len = u8_strlen(token); + + skip_eol = TRUE; + if (read_yaml_macros_and_links) + { + if (pfootnotes->value) + { + size_t value_len = u8_strlen(pfootnotes->value); + + if (pfootnotes->value_size < value_len + token_len + 1) + { + pfootnotes->value_size + = value_len + token_len + 1; + REALLOC(pfootnotes->value, uint8_t, pfootnotes->value_size) + } + u8_strncat(pfootnotes->value, token, pfootnotes->value_size-1); + } + else + { + pfootnotes->value_size = token_len + 1; + CALLOC(pfootnotes->value, uint8_t, pfootnotes->value_size) + u8_strncpy(pfootnotes->value, token, pfootnotes->value_size-1); + } + } + } + } + + if (state & ST_BLOCKQUOTE + && (!*pbuffer || *pbuffer != '>')) + { + state &= ~ST_BLOCKQUOTE; + process_blockquote(output, TRUE); + } + + if (ANY(state, ST_TABLE_HEADER | ST_TABLE_LINE) + && (!line_len || !*pbuffer)) + { + if (!read_yaml_macros_and_links) + { + warning(1, (uint8_t*)"Malformed table"); + process_table_end(output); + } + state &= ~(ST_TABLE_HEADER | ST_TABLE_LINE); + } + + if ((state & ST_TABLE) && (!line_len || !*pbuffer)) + { + if (!read_yaml_macros_and_links) + process_table_end(output); + state &= ~ST_TABLE; + } + + previous_line_blank = FALSE; + } + + if (!skip_eol && !keep_token && !read_yaml_macros_and_links + && line_len > 0 + && output_colno > 1 + && !ANY(state, ST_YAML | ST_YAML_VAL | ST_LINK_SECOND_ARG + | ST_LINK_SECOND_ARG_END)) + { + print_output(output, "%s", state & ST_PRE ? "\n" : " "); + if (state & ST_PRE) + output_colno = 1; + else + output_colno++; + } + + if (!keep_token) + RESET_TOKEN(token, ptoken, token_size) + + if (!skip_change_first_line_in_doc + && !(state & ST_YAML)) + first_line_in_doc = FALSE; + + skip_change_first_line_in_doc = FALSE; + if (line_len == 0) + previous_line_blank = TRUE; + + /* Lasts until the end of line */ + state &= ~(ST_YAML_VAL | ST_IMAGE_SECOND_ARG | ST_LINK_SECOND_ARG + | ST_LINK_SECOND_ARG_END); + } + while (pbuffer && *pbuffer); + + if (!read_yaml_macros_and_links + && (footnote_count > 0 || inline_footnote_count > 0)) + end_footnotes(output, add_footnote_div); + + if (!read_yaml_macros_and_links) + { + print_output(output, "\n"); + output_colno = 1; + } + + if (link_text) + free(link_text); + + free(link_macro); + free(token); + free(line); + + return 0; +} + +int +main(int argc, char** argv) +{ + char* arg; + Command cmd = CMD_NONE; + int result = 0; + + basedir_size = 2; + CALLOC(basedir, char, basedir_size) + *basedir = '.'; + + while ((arg = *++argv)) + { + if (*arg == '-') + { + arg++; + char c = *arg++; + int result; + + if (c == '-') + { + if (!strcmp(arg, "version")) + cmd = CMD_VERSION; + else if (startswith(arg, "basedir")) + { + arg += strlen("basedir"); + result = set_basedir(arg, &basedir, &basedir_size); + if (result) + return result; + } + else if (!strcmp(arg, "help")) + return usage(); + else + { + error(EINVAL, (uint8_t*)"Invalid argument: --%s", arg); + return usage(); + } + } + else + { + switch (c) + { + case 'd': + cmd = CMD_BASEDIR; + break; + case 'h': + return usage(); + break; + case 'v': + cmd = CMD_VERSION; + break; + default: + error(EINVAL, (uint8_t*)"Invalid argument: -%c", c); + return usage(); + } + } + } + else + { + int result; + + if (cmd == CMD_BASEDIR) + { + result = set_basedir(arg, &basedir, &basedir_size); + if (result) + return result; + } + else + input_filename = arg; + cmd = CMD_NONE; + } + } + + if (cmd == CMD_BASEDIR) + return error(1, (uint8_t*)"-d: Argument required"); + + if (cmd == CMD_VERSION) + return version(); + + FILE* input = NULL; + FILE* output = stdout; + uint8_t* buffer = NULL; + size_t buffer_size = 0; + + if (input_filename) + read_file_into_buffer(&buffer, &buffer_size, input_filename, &input_dirname, &input); + else + { + uint8_t* bufline = NULL; + size_t buffer_len = 0; + size_t buffer_size = 0; + size_t bufline_len = 0; + + input = stdin; + + CALLOC(bufline, uint8_t, BUFSIZE) + buffer_size = BUFSIZE; + CALLOC(buffer, uint8_t, buffer_size) + + while (!feof(input)) + { + uint8_t* eol = NULL; + if (!fgets((char*)bufline, BUFSIZE-1, input)) + break; + eol = u8_strchr(bufline, (ucs4_t)'\n'); + if (eol) + *(eol+1) = 0; + bufline_len = u8_strlen(bufline); + if (buffer_len + bufline_len + 1 > buffer_size) + { + buffer_size += BUFSIZE; + REALLOC(buffer, uint8_t, buffer_size) + } + u8_strncat(buffer, bufline, buffer_size-buffer_len-1); + buffer_len += bufline_len; + } + free(bufline); + } + + CALLOC(vars, KeyValue, 1) + vars->key = NULL; + vars->value = NULL; + vars->value_size = 0; + + CALLOC(macros, KeyValue, 1) + macros->key = NULL; + macros->value = NULL; + macros->value_size = 0; + + CALLOC(links, KeyValue, 1) + links->key = NULL; + links->value = NULL; + links->value_size = 0; + + CALLOC(paralinks, KeyValue, 1) + paralinks->key = NULL; + paralinks->value = NULL; + paralinks->value_size = 0; + + CALLOC(footnotes, KeyValue, 1) + footnotes->key = NULL; + footnotes->value = NULL; + footnotes->value_size = 0; + + CALLOC(inline_footnotes, uint8_t*, 1) + *inline_footnotes = NULL; + inline_footnote_count = 0; + + /* First pass: read YAML, macros and links */ + result = slweb_parse(buffer, output, TRUE); + + if (result) + { + free(buffer); + return result; + } + + state = ST_NONE; + current_footnote = 0; + current_inline_footnote = 0; + + /* Second pass: parse and output */ + result = slweb_parse(buffer, output, FALSE); + + if (basedir) + free(basedir); + if (input_dirname) + free(input_dirname); + if (inline_footnotes) + free(inline_footnotes); + free_keyvalue(&footnotes, footnote_count); + free_keyvalue(&paralinks, paralinks_count); + free_keyvalue(&links, links_count); + free_keyvalue(&macros, macros_count); + free_keyvalue(&vars, vars_count); + free(footnotes); + free(paralinks); + free(links); + free(macros); + free(vars); + free(buffer); + + return result; +} + diff --git a/slw2gmi.do b/slw2gmi.do @@ -0,0 +1,3 @@ +redo-ifchange slw2gmi.o slw2gmi.c defs.h +${SLW2GMI_CC:-gcc} -g -Wall -std=c99 -o $3 slw2gmi.o -lunistring + diff --git a/uninstall.do b/uninstall.do @@ -0,0 +1,7 @@ +redo-always +PREFIX=/usr/local +BINDIR=$PREFIX/bin +DOCDIR=$PREFIX/share/doc/slw2gmi +MANDIR=$PREFIX/share/man/man1 +rm -f $BINDIR/slw2gmi $DOCDIR/slw2gmi.pdf $MANDIR/slw2gmi.1.gz + diff --git a/version.do b/version.do @@ -0,0 +1,7 @@ +if ! git describe >$3; then + echo "$0: can't call git describe, falling back to 'unknown'" >&2 + echo 'unknown' >$3 +fi +redo-always +redo-stamp <$3 +