├── .gitignore ├── AUTHORS ├── ChangeLog ├── DEVELOPERS.md ├── LICENSE ├── MANIFEST.in ├── README.md ├── pandocxnos ├── README.md ├── __init__.py ├── core.py ├── main.py └── pandocattributes.py ├── setup.py └── test ├── integration ├── Makefile ├── demo.md └── fig.png └── test.py /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | dist 3 | *.egg-info 4 | test/integration/out/* -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | 2 | pandoc-xnos was written and is maintained by Thomas J. Duck 3 | . 4 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | pandoc-xnos 2.5.0 (2020-11-10) 2 | 3 | * Updated for pandoc api 1.22 (pandoc-fignos Issue #85, 4 | pandoc-tablenos Issue #23) 5 | 6 | 7 | pandoc-xnos 2.4.3 (2020-08-04) 8 | 9 | * Use nonbreaking spaces in references (Issue #14). 10 | 11 | 12 | pandoc-xnos 2.4.2 (2020-07-31) 13 | 14 | * Fixed processing of uncaptioned tables with pandoc 2.10 15 | (pandoc-fignos Issue #82)" 16 | 17 | 18 | pandoc-xnos 2.4.1 (2020-07-30) 19 | 20 | * Fixed "Failed reading: not a valid json value" error 21 | affecting python 2 users (pandoc-tablenos Issue #17 and 22 | pandoc-eqnos Issue #45). 23 | 24 | 25 | pandoc-xnos 2.4.0 (2020-07-30) 26 | 27 | * Handle attributed tables for pandoc >= 2.10. 28 | * Added version() function to aid version string comparisons. 29 | * Use nonbreaking spaces in references (Issue #14). 30 | 31 | 32 | pandoc-xnos 2.3.0 (2019-11-03) 33 | 34 | * Allow get_meta() to process MetaLists of MetaMaps 35 | (addresses issue in pandoc-theoremnos PR #1) 36 | 37 | 38 | pandoc-xnos 2.2.2 (2019-11-02) 39 | 40 | * Enable insert_secnos_factory() and delete_secnos_factory() 41 | to operate on Span elements (PR #10). 42 | 43 | 44 | pandoc-xnos 2.2.0 (2019-10-27) 45 | 46 | * Enable insert_secnos_factory() and delete_secnos_factory() 47 | to operate on Div elements. 48 | 49 | 50 | pandoc-xnos 2.1.2 (2019-09-26) 51 | 52 | * Fixed "'warninglevel' is not defined" crash. 53 | * Added `set_warning_level()` 54 | 55 | 56 | pandoc-xnos 2.1.0 (2019-09-26) 57 | 58 | * Checks for and warns about duplicate reference targets. 59 | * Use inspect module to get filter name. 60 | * API changes, with backward compatibility: 61 | - process_refs_factory(): removed filtername as first argument 62 | - attach_attrs_factory(): removed filtername as first argument 63 | 64 | pandoc-xnos 2.0.1 (2019-09-19) 65 | 66 | * Fixed handling of spans ending with a reference. 67 | 68 | 69 | pandoc-xnos 2.0.0 (2019-09-17) 70 | 71 | * Rebranded as the pandoc-xnos filter suite. 72 | * Removed cleveref fakery (pandoc-fignos Issue #68). 73 | * Added warning message system. 74 | * Fixed epub links across chapters. 75 | * Fixed quoted attribute handling. 76 | * Fixed span handling (pandoc-fignos Issue #58). 77 | * Support attributes on references (pandoc-fignos Issue #54). 78 | * Support disabling links on selected references (pandoc-fignos 79 | Issue #54). 80 | * Improved handling of missing reference targets. 81 | * Parse MetaMaps in get_meta(). 82 | * Added pandoc-xnos filter which calls all of the filters in the 83 | pandoc-xnos filter suite if available. 84 | 85 | 86 | pandoc-xnos 1.2.0 (2019-05-15) 87 | 88 | * Support for references in bracketed spans (pandoc-fignos 89 | Issue #58). 90 | 91 | 92 | pandoc-xnos 1.1.2 (2019-05-15) 93 | 94 | * Allow xnos-number-sections with docx (pandoc-fignos Issue #59). 95 | 96 | 97 | pandoc-xnos 1.1.1 (2018-08-09) 98 | 99 | * Fixed new problem with section numbers and html output. 100 | 101 | 102 | pandoc-xnos 1.1.0 (2018-08-09) 103 | 104 | * A workaround is provided for a bug in boolean metadata handling 105 | for pandoc 2.2.3 and 2.2.3.1 (see pandoc Issue #4819). 106 | * A bug involving xnos-number-sections and unattributed 107 | equations is fixed (see pandoc-xnos Issue #30). 108 | * Added processing for references in spans (pandoc-xnos 109 | merge request #6). 110 | * Provides a helpful error message when On or Off are used as 111 | boolean xnos metadata values. As of pandoc 2.2.2 this is no 112 | longer allowed. 113 | 114 | 115 | pandoc-xnos 1.0.1 (2018-01-14) 116 | 117 | * Moving to MAJOR.MINOR.PATCH release semantic versioning as per 118 | https://semver.org/. API changes will result in an increment 119 | of the MAJOR number. This helps fix pandoc-fignos Issue #46. 120 | 121 | 122 | pandoc-xnos 0.15 (2018-01-13) 123 | 124 | * Added support for AMS-style equations referencing 125 | (pandoc-eqnos Issue #27). 126 | 127 | 128 | pandoc-xnos 0.14 (2018-01-09) 129 | 130 | * Fixed bug that prevented bolding or italicizing of references 131 | (pandoc-fignos Issue #45). 132 | 133 | 134 | pandoc-xnos 0.12, 0.13 (2017-12-16) 135 | 136 | * Copied pandocattributes.py into pandoc-xnos to work around 137 | an install error (see 138 | https://github.com/aaren/pandoc-attributes/issues/1) 139 | 140 | 141 | pandoc-xnos 0.11 (2017-11-18) 142 | 143 | * Updated tests for pandoc-2.0. 144 | 145 | 146 | pandoc-xnos 0.10 (2017-08-20) 147 | 148 | * Allows version numbers starting with 2 (pandoc-fignos 149 | Issue #40). 150 | 151 | 152 | pandoc-xnos 0.9 (2016-12-10) 153 | 154 | * Uses the new PANDOC_VERSION environment variable 155 | (pandoc Issue #2640). 156 | 157 | 158 | pandoc-xnos 0.8, 0.8.1, 0.8.2 (2016-12-05) 159 | 160 | * New factory functions for adding and removing section numbers 161 | from an element's attributes. 162 | 163 | 164 | pandoc-xnos 0.7 (2016-11-17) 165 | 166 | * Fixed regex for python 2.7.3 (pandoc-fignos Issue #32). 167 | 168 | 169 | pandoc-xnos 0.6 (2016-11-05) 170 | 171 | * Updated for pandoc 1.18 (pandoc-fignos Issue #30). 172 | 173 | 174 | pandoc-xnos 0.5 (2016-10-22) 175 | 176 | * Fixed bug in broken refs code (Issue #1). 177 | * Fixed textless doc crash (pandoc-fignos Issue #27). 178 | 179 | 180 | pandoc-xnos 0.4, 0.4.1 (2016-08-21) 181 | 182 | * Added insert_rawblock_factory(). 183 | * More improvements to TeX output. 184 | 185 | 186 | pandoc-xnos 0.3 (2016-05-18) 187 | 188 | * Improved TeX output. 189 | * Added integration tests. 190 | 191 | 192 | pandoc-xnos 0.2 (2016-05-16) 193 | 194 | * Fixed treatment of math references for NON-html/tex/pdf output. 195 | 196 | 197 | pandoc-xnos 0.1 (2016-05-14) 198 | 199 | * Initial release. 200 | -------------------------------------------------------------------------------- /DEVELOPERS.md: -------------------------------------------------------------------------------- 1 | 2 | Developer Notes 3 | =============== 4 | 5 | Branches 6 | -------- 7 | 8 | The next release is developed in the `nextrelease` branch. When ready, the changes are merged into the `master` branch. 9 | 10 | A copy of the 1.x release series is maintained in the 1.x branch. 11 | 12 | 13 | Install Alternatives 14 | -------------------- 15 | 16 | There are a few different options for installing from source: 17 | 18 | 1) To install from the github `master` branch use: 19 | 20 | pip install git+https://github.com/tomduck/pandoc-xnos.git --user 21 | 22 | (to upgrade append the `--upgrade` flag). 23 | 24 | 2) To install from the `nextrelease` branch on github, use 25 | 26 | pip install git+https://github.com/tomduck/pandoc-xnos.git@nextrelease --user 27 | 28 | (to upgrade use the --upgrade flag). 29 | 30 | 3) To install from a local source distribution, `cd` into its root 31 | and use 32 | 33 | pip install -e . --user 34 | 35 | Note that any changes made to the source will be automatically 36 | reflected when a filter is run (which is useful for development). 37 | 38 | 39 | Testing 40 | ------- 41 | 42 | Unit tests for pandoc-xnos are provided in `test/`. Integration tests are in `test/integration`. 43 | 44 | 45 | Preparing a Release 46 | ------------------- 47 | 48 | ### Merging #### 49 | 50 | Merge the `nextrelease` branch into `master` using 51 | 52 | git checkout master 53 | git merge nextrelease 54 | git push 55 | 56 | 57 | ### Tagging ### 58 | 59 | See https://www.python.org/dev/peps/pep-0440/ for numbering conventions, including for pre-releases. 60 | 61 | Tagging (update the version number): 62 | 63 | git tag -a 2.5.0 -m "New release." 64 | git push origin 2.5.0 65 | 66 | 67 | ### Distributing ### 68 | 69 | Creating source and binary distributions: 70 | 71 | python3 setup.py sdist bdist_wheel 72 | 73 | (see https://packaging.python.org/tutorials/packaging-projects/). 74 | 75 | Uploading to pypi (update the version number): 76 | 77 | twine upload dist/pandoc-xnos-2.5.0.tar.gz \ 78 | dist/pandoc_xnos-2.5.0-py3-none-any.whl 79 | 80 | (see https://pypi.python.org/pypi/twine). 81 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | -------------------------------------------------------------------------------- /MANIFEST.in: -------------------------------------------------------------------------------- 1 | include README.md 2 | include DEVELOPERS.md 3 | include LICENSE 4 | include ChangeLog 5 | include AUTHORS 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 2 | pandoc-xnos 3 | =========== 4 | 5 | The *pandoc-xnos* filter suite provides facilities for cross-referencing in markdown documents processed by [pandoc]. Individual filters are maintained in separate projects. They are: 6 | 7 | * [pandoc-fignos]: Numbers figures and figure references. 8 | * [pandoc-eqnos]: Numbers equations and equation references. 9 | * [pandoc-tablenos]: Numbers tables and table references. 10 | * [pandoc-secnos]: Numbers section references (sections are 11 | numbered by pandoc itself). 12 | 13 | Click on the above links to access documentation for each filter. LaTeX/pdf, html, and epub output have native support. Native support for docx output is a work in progress. 14 | 15 | This project provides library code for the suite and a `pandoc-xnos` filter that applies all of the above filters in sequence. 16 | 17 | See also: [pandoc-comments], [pandoc-latex-extensions] 18 | 19 | [pandoc]: http://pandoc.org/ 20 | [pandoc-fignos]: https://github.com/tomduck/pandoc-fignos 21 | [pandoc-eqnos]: https://github.com/tomduck/pandoc-eqnos 22 | [pandoc-tablenos]: https://github.com/tomduck/pandoc-tablenos 23 | [pandoc-secnos]: https://github.com/tomduck/pandoc-secnos 24 | [pandoc-comments]: https://github.com/tomduck/pandoc-comments 25 | [pandoc-latex-extensions]: https://github.com/tomduck/pandoc-latex-extensions 26 | 27 | 28 | Contents 29 | -------- 30 | 31 | 1. [Installation](#installation) 32 | 2. [Usage](#usage) 33 | 3. [Getting Help](#getting-help) 34 | 35 | 36 | Installation 37 | ------------ 38 | 39 | The Pandoc-xnos filters require [python]. It is easily installed -- see [here](https://realpython.com/installing-python/). [1](#footnote1) Either python 2.7 or 3.x will do. 40 | 41 | The pandoc-xnos filter suite may be installed using the shell command 42 | 43 | pip install pandoc-fignos pandoc-eqnos pandoc-tablenos \ 44 | pandoc-secnos --user 45 | 46 | and upgraded by appending `--upgrade` to the above command. 47 | 48 | Pip is a program that downloads and installs software from the Python Package Index, [PyPI]. 49 | 50 | Instructions for installing from source are given in [DEVELOPERS.md]. 51 | 52 | [python]: https://www.python.org/ 53 | [PyPI]: https://pypi.python.org/pypi 54 | [DEVELOPERS.md]: DEVELOPERS.md 55 | 56 | 57 | Usage 58 | ----- 59 | 60 | The pandoc-xnos filter suite may be applied using the 61 | 62 | --filter pandoc-xnos 63 | 64 | option with pandoc. It is also possible to apply the filters individually. 65 | 66 | Any use of `--filter pandoc-citeproc` or `--bibliography=FILE` should come *after* the `pandoc-xnos` filter call. 67 | 68 | 69 | Getting Help 70 | ------------ 71 | 72 | If you have any difficulties with pandoc-xnos, or would like to see a new feature, then please submit a report to our [Issues tracker]. 73 | 74 | [Issues tracker]: https://github.com/tomduck/pandoc-xnos/issues 75 | 76 | 77 | ---- 78 | 79 | **Footnotes** 80 | 81 | 1: For MacOS, my preferred install method is to use the Installer package [available from python.org](https://www.python.org/downloads/). 82 | -------------------------------------------------------------------------------- /pandocxnos/README.md: -------------------------------------------------------------------------------- 1 | 2 | Pandocxnos 2.5.0 3 | ================ 4 | 5 | The *pandocxnos* package provides library code for the pandoc-xnos filter suite. Below is a short summary of what is available. More details are given in the individual module and function docstrings. 6 | 7 | [fignos]: https://github.com/tomduck/pandoc-fignos 8 | [eqnos]: https://github.com/tomduck/pandoc-eqnos 9 | [tablenos]: https://github.com/tomduck/pandoc-tablenos 10 | [secnos]: https://github.com/tomduck/pandoc-tablenos 11 | [pandoc]: http://pandoc.org/ 12 | 13 | 14 | core.py 15 | ------- 16 | 17 | ### Globals ### 18 | 19 | * `STRTYPES` - a list of string types for this python version 20 | * `STDIN`/`STDOUT`/`STDERR` - streams for use with python 2.x or 3.x 21 | * `NBSP` - nonbreaking space for use with python 2.x or 3.x 22 | 23 | 24 | ### Utility functions ### 25 | 26 | * `init()` - Must be called. Sets/resets global variables; 27 | determines and returns the pandoc version 28 | * `version()` - Converts a version string into a tuple. This is 29 | useful for simple version number comparisons. 30 | * `set_warning_level()` - Sets the global warning level; 31 | 0 for no warnings; 1 for critical warnings; 2 for all warnings 32 | * `check_bool()` - Used to check if a variable is boolean 33 | * `get_meta()` - Retrieves variables from a document's metadata 34 | * `elt()` - Used to create pandoc AST elements 35 | * `add_to_header_includes()` - Adds header-includes to metadata 36 | * `cleveref_required()` - Returns True if cleveref usage was found 37 | 38 | 39 | ### Element list functions ### 40 | 41 | * `quotify()` - Changes Quoted elements to quoted strings 42 | * `dollarfy()` - Changes Math elements to dollared strings 43 | * `extract_attrs()` - Extracts attribute strings 44 | 45 | 46 | ### Actions and their factory functions ### 47 | 48 | * `join_strings()` - Joins adjacent strings in a pandoc document 49 | * `repair_refs()` - Repairs broken Cite elements for pandoc < 1.18 50 | * `process_refs_factory()` - Makes functions that process 51 | references 52 | * `replace_refs_factory()` - Makes functions that replace refs with 53 | format-specific content 54 | * `attach_attrs_factory()` - Makes functions that attach attributes 55 | to elements 56 | * `detach_attrs_factory()` - Makes functions that detach attributes 57 | from elements 58 | * `insert_secnos_factory()` - Makes functions that insert section 59 | numbers into attributes 60 | * `delete_secnos_factory()` - Makes functions that delete section 61 | numbers from attributes 62 | * `insert_rawblocks_factory()` - Makes function to insert 63 | non-duplicate RawBlock elements. 64 | 65 | 66 | pandocattributes.py 67 | ------------------- 68 | 69 | This is a modified version of Aaron O'Leary's project of the same name (https://github.com/aaren/pandoc-attributes). It provides facilities for managing attributes in pandoc. 70 | 71 | 72 | main.py 73 | ------- 74 | 75 | This is a pandoc filter that calls pandoc-fignos/eqnos/tablenos/secnos in sequence (if available). 76 | -------------------------------------------------------------------------------- /pandocxnos/__init__.py: -------------------------------------------------------------------------------- 1 | """Package initialization.""" 2 | 3 | from .core import * 4 | from .main import main 5 | from .pandocattributes import PandocAttributes 6 | -------------------------------------------------------------------------------- /pandocxnos/core.py: -------------------------------------------------------------------------------- 1 | """core.py: library code for the pandoc-xnos filter suite.""" 2 | 3 | # Copyright 2015-2020 Thomas J. Duck. 4 | # All rights reserved. 5 | # 6 | # This program is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | 19 | __version__ = '2.5.0' 20 | 21 | 22 | import os 23 | import sys 24 | import io 25 | import subprocess 26 | import re 27 | import textwrap 28 | import functools 29 | import copy 30 | import collections 31 | import inspect 32 | 33 | import psutil 34 | 35 | from pandocfilters import Str, Space, Math, RawInline, RawBlock, Link, Span 36 | from pandocfilters import walk, stringify 37 | from pandocfilters import elt as _elt 38 | 39 | from .pandocattributes import PandocAttributes 40 | 41 | 42 | # pylint: disable=too-many-lines 43 | 44 | 45 | #============================================================================= 46 | # Globals 47 | 48 | # Python has different string types depending on the python version. We must 49 | # be able to identify them both. 50 | # pylint: disable=undefined-variable 51 | STRTYPES = (str,) if sys.version_info > (3,) else (str, unicode) 52 | 53 | # Pandoc uses UTF-8 for both input and output; so must its filters. This is 54 | # handled differently depending on the python version. 55 | if sys.version_info > (3,): 56 | # Py3 strings are unicode: https://docs.python.org/3.5/howto/unicode.html. 57 | # Character encoding/decoding is performed automatically at stream 58 | # interfaces: https://stackoverflow.com/questions/16549332/. 59 | # Set it to UTF-8 for all streams. 60 | STDIN = io.TextIOWrapper(sys.stdin.buffer, 'utf-8', 'strict') 61 | STDOUT = io.TextIOWrapper(sys.stdout.buffer, 'utf-8', 'strict') 62 | STDERR = io.TextIOWrapper(sys.stderr.buffer, 'utf-8', 'strict') 63 | else: 64 | # Py2 strings are ASCII bytes. Encoding/decoding is handled separately. 65 | # See: https://docs.python.org/2/howto/unicode.html. 66 | STDIN = sys.stdin 67 | STDOUT = sys.stdout 68 | STDERR = sys.stderr 69 | 70 | # Non-breaking space 71 | NBSP = '\u00A0' if sys.version_info > (3,) else '\xc2\xa0' 72 | 73 | # Global state-tracking variables 74 | # pylint: disable=invalid-name 75 | _cleveref_flag = None # Flags that the cleveref package is needed 76 | _sec = None # Used to track section numbers 77 | 78 | 79 | #============================================================================= 80 | # Decorators 81 | 82 | # _repeat() ------------------------------------------------------------------ 83 | 84 | # The _repeat decorator repeats a call until something other than None is 85 | # returned. Functions that must return None should be broken into parts. 86 | # See, for example, join_strings(). 87 | 88 | def _repeat(func): 89 | """Repeats func(...) call until something other than None is returned.""" 90 | @functools.wraps(func) 91 | def wrapper(*args, **kwargs): 92 | """Repeats the call until True is returned.""" 93 | ret = None 94 | while ret is None: 95 | ret = func(*args, **kwargs) 96 | return ret 97 | return wrapper 98 | 99 | # Compatibility layer for deprecated 2.0.1 api. This applies to two functions: 100 | # process_refs_factory() and attach_attrs_factory(). 101 | def _compat(f): 102 | """Compatibility layer.""" 103 | @functools.wraps(f) 104 | def wrapper(*args, **kwargs): 105 | """Wrapper.""" 106 | if 'patt' in kwargs: 107 | kwargs['regex'] = kwargs.pop('patt') 108 | if args and isinstance(args[0], STRTYPES): 109 | try: # This may be a call to the deprecated 2.0.1 api 110 | return f(*args[1:], **kwargs) 111 | except TypeError: # Usually a signature mismatch; use new api 112 | pass 113 | kwargs.pop('name', None) 114 | return f(*args, **kwargs) 115 | return wrapper 116 | 117 | 118 | #============================================================================= 119 | # Utility functions 120 | 121 | # init() --------------------------------------------------------------------- 122 | 123 | _PANDOCVERSION = None # A string giving the pandoc version 124 | _FILTERNAME = None # The name of the calling filter 125 | _WARNINGLEVEL = 2 # 0 for no warnings; 1 for critical warnings; 2 for all 126 | 127 | def _get_pandoc_version(pandocversion, doc): 128 | """Determines, checks, and returns the pandoc version.""" 129 | 130 | # This requires some care because we can't be sure that a call to `pandoc` 131 | # will work. It could be `pandoc-1.17.0.2` or some other name. Try 132 | # checking the PANDOC_VERSION environment variable, and then the parent 133 | # process. Only make a call to `pandoc` as a last resort. 134 | 135 | if pandocversion: # It was provided; only need to check it 136 | pass 137 | 138 | elif 'PANDOC_VERSION' in os.environ: # Available for pandoc >= 1.19.1 139 | pandocversion = str(os.environ['PANDOC_VERSION']) 140 | 141 | elif doc is not None and 'pandoc-api-version' in doc: 142 | # This could be pandoc 1.18 or 1.19; there is no way to distinguish 143 | # them. Fortunately, but there isn't a use case in pandoc-fignos and 144 | # friends where it matters. 145 | pandocversion = '1.18' 146 | 147 | else: 148 | # As a last resort, ask the pandoc executable what its version is 149 | try: # Get the executable of the parent process 150 | if os.name == 'nt': 151 | # psutil appears to work differently for windows 152 | command = psutil.Process(os.getpid()).parent().parent().exe() 153 | else: 154 | command = psutil.Process(os.getpid()).parent().exe() 155 | # Expect the executable to begin with the name `pandoc`; 156 | # e.g., `pandoc-2.7.3`. If it does not, then the parent process 157 | # could be another program altogether (e.g., `panzer`). 158 | if not os.path.basename(command).startswith('pandoc'): 159 | raise RuntimeError('pandoc not found') 160 | except: # pylint: disable=bare-except 161 | # Use whatever pandoc is available and hope for the best 162 | command = 'pandoc' 163 | 164 | # Make the call 165 | try: 166 | # Get the version number and confirm it conforms to expectations 167 | output = subprocess.check_output([command, '-v']) 168 | line = output.decode('utf-8').split('\n')[0] 169 | pandocversion = line.split(' ')[-1].strip() 170 | except: # pylint: disable=bare-except 171 | pass 172 | 173 | # Test `pandocversion` and if it is OK then return it 174 | pattern = re.compile(r'^[1-2]\.[0-9]+(?:\.[0-9]+)?(?:\.[0-9]+)?$') 175 | if pandocversion is not None: 176 | if pattern.match(pandocversion): 177 | return pandocversion 178 | msg = 'Cannot understand pandocversion=%s'%pandocversion 179 | raise RuntimeError(msg) 180 | 181 | # Raise an exception 182 | msg = textwrap.dedent("""\ 183 | Cannot determine pandoc version. Please file an issue at 184 | https://github.com/tomduck/pandocxnos/issues.""") 185 | raise RuntimeError(msg) 186 | 187 | 188 | # pylint: disable=too-many-branches 189 | def init(pandocversion=None, doc=None): 190 | """Initializes library. This must be called before a filter accesses 191 | other functions in this library. 192 | 193 | This function: 194 | 195 | 1) Sets (or resets) global variables. 196 | 2) Sets or determines the pandoc version. 197 | 3) Inspects and saves the calling module's name 198 | 199 | Returns the pandoc version. 200 | 201 | Parameters: 202 | 203 | pandocversion - A string representing the pandoc-version; if this is 204 | None then init() will attempt to determine the version 205 | through other means 206 | doc - the pandoc document AST dict 207 | """ 208 | 209 | # pylint: disable=global-statement 210 | global _PANDOCVERSION 211 | global _FILTERNAME 212 | global _cleveref_flag 213 | global _sec 214 | 215 | # Set (or reset) globals 216 | _cleveref_flag = None # Flags that the cleveref package is needed 217 | _sec = 0 # Used to track section numbers 218 | 219 | # Save the calling module's name; see https://stackoverflow.com/a/1095621 220 | frame = inspect.stack()[1][0] 221 | _FILTERNAME = inspect.getmodule(frame).__name__.replace('_', '-') 222 | 223 | # Get and return the pandoc version 224 | _PANDOCVERSION = _get_pandoc_version(pandocversion, doc) 225 | return _PANDOCVERSION 226 | 227 | 228 | def version(v): 229 | """Converts a version string into a tuple appropriate for comparisons.""" 230 | return tuple(int(x) for x in v.split('.')) 231 | 232 | 233 | # set_warning_level() -------------------------------------------------------- 234 | def set_warning_level(n): 235 | """0 for no warnings; 1 for critical warnings; 2 for all warnings""" 236 | # pylint: disable=global-statement 237 | global _WARNINGLEVEL 238 | _WARNINGLEVEL = n 239 | 240 | 241 | # check_bool() --------------------------------------------------------------- 242 | 243 | def check_bool(v): 244 | """Checks that metadata value `v` is boolean. Returns the value or 245 | raises an exception.""" 246 | if not isinstance(v, bool): 247 | msg = 'Metadata boolean values must be one of the following: ' \ 248 | 'true, True, TRUE, false, False, FALSE. ' \ 249 | 'As of pandoc 2.2.2, the following are not allowed: ' \ 250 | 'On, Off.' 251 | raise ValueError(msg) 252 | return v 253 | 254 | 255 | # get_meta() ----------------------------------------------------------------- 256 | 257 | # Metadata json depends upon whether or not the variables were defined on the 258 | # command line or in a document. The get_meta() function makes no 259 | # distinction. 260 | 261 | # pylint: disable=too-many-return-statements 262 | def get_meta(meta, name): 263 | """Retrieves the metadata variable `name` from the `meta` dict.""" 264 | assert name in meta 265 | data = meta[name] 266 | 267 | if data['t'] in ['MetaString', 'MetaBool']: 268 | return data['c'] 269 | if data['t'] == 'MetaInlines': 270 | # Handle bug in pandoc 2.2.3 and 2.2.3.1: Return boolean value rather 271 | # than strings, as appropriate. 272 | if len(data['c']) == 1 and data['c'][0]['t'] == 'Str': 273 | if data['c'][0]['c'] in ['true', 'True', 'TRUE']: 274 | return True 275 | if data['c'][0]['c'] in ['false', 'False', 'FALSE']: 276 | return False 277 | return stringify(data['c']) 278 | if data['t'] == 'MetaList': 279 | try: # Process MetaList of MetaMaps 280 | ret = [] 281 | for v in data['c']: 282 | assert v['t'] == 'MetaMap' 283 | entry = {} 284 | for key in v['c']: 285 | entry[key] = stringify(v['c'][key]) 286 | ret.append(entry) 287 | return ret 288 | except AssertionError: 289 | pass 290 | return [stringify(v['c']) for v in data['c']] 291 | if data['t'] == 'MetaMap': 292 | ret = {} 293 | for key in data['c']: 294 | ret[key] = stringify(data['c'][key]) 295 | return ret 296 | raise RuntimeError("Could not understand metadata variable '%s'." % name) 297 | 298 | 299 | # elt() ---------------------------------------------------------------------- 300 | 301 | def elt(eltType, numargs): # pylint: disable=invalid-name 302 | """Returns Element(*value) function to create pandoc AST elements. 303 | This should be used in place of pandocfilters.elt(). This version 304 | ensures that the content is stored in a list, not a tuple. 305 | """ 306 | def Element(*value): # pylint: disable=invalid-name 307 | """Creates an element.""" 308 | el = _elt(eltType, numargs)(*value) 309 | if isinstance(el['c'], tuple): 310 | el['c'] = list(el['c']) # The content should be a list, not tuple 311 | return el 312 | return Element 313 | 314 | Cite = elt('Cite', 2) # pylint: disable=invalid-name 315 | 316 | def _getel(key, value): 317 | """Returns an element given a `key` and `value`.""" 318 | if key in ['HorizontalRule', 'Null']: 319 | return elt(key, 0)() 320 | if key in ['Plain', 'Para', 'BlockQuote', 'BulletList', 321 | 'DefinitionList', 'HorizontalRule', 'Null']: 322 | return elt(key, 1)(value) 323 | return elt(key, len(value))(*value) 324 | 325 | 326 | # add_to_header_includes() ------------------------------------------------ 327 | 328 | # WARNING: Pandoc's --include-in-header option overrides the header-includes 329 | # meta variable in post-filter processing. This owing to a design decision 330 | # in pandoc. See https://github.com/jgm/pandoc/issues/3139. 331 | 332 | def add_to_header_includes(meta, fmt, block, warninglevel=None, regex=None): 333 | """Adds `block` to header-includes field of `meta`. The block is 334 | encapsulated in a pandoc RawBlock. 335 | 336 | Parameters: 337 | 338 | meta - the document metadata 339 | fmt - the format of the block (tex, html, ...) 340 | block - the block of text to add to the header-includes 341 | warninglevel - DEPRECATED (set global WARNINGLEVEL instead) 342 | regex - a regular expression used to check existing header-includes 343 | in the document metadata for overlap 344 | """ 345 | 346 | # Set the global warning level (DEPRECATED) 347 | # pylint: disable=global-statement 348 | global _WARNINGLEVEL 349 | assert(warninglevel is None or isinstance(warninglevel, int)) 350 | if warninglevel is not None: 351 | _WARNINGLEVEL = warninglevel 352 | 353 | # If pattern is found in the meta-includes then bail out 354 | if regex and 'header-includes' in meta: 355 | pattern = re.compile(regex) 356 | if pattern.search(str(meta['header-includes'])): 357 | return 358 | # Create the rawblock and install it in the header-includes 359 | block = textwrap.dedent(block) 360 | rawblock = {'t': 'RawBlock', 'c': [fmt, block]} 361 | metablocks = {'t': 'MetaBlocks', 'c': [rawblock]} 362 | if 'header-includes' not in meta: 363 | meta['header-includes'] = metablocks 364 | elif meta['header-includes']['t'] in ['MetaBlocks', 'MetaInlines']: 365 | meta['header-includes'] = \ 366 | {'t': 'MetaList', 'c': [meta['header-includes'], metablocks]} 367 | elif meta['header-includes']['t'] == 'MetaList': 368 | meta['header-includes']['c'].append(metablocks) 369 | else: 370 | msg = textwrap.dedent("""\ 371 | header-includes metadata cannot be parsed: 372 | 373 | %s 374 | """ % str(meta['header-includes'])) 375 | raise RuntimeError(msg) 376 | # Print the block to stderr at warning level 2 377 | if _WARNINGLEVEL == 2: 378 | if hasattr(textwrap, 'indent'): 379 | STDERR.write(textwrap.indent(block, ' ')) 380 | else: 381 | STDERR.write('\n'.join(' ' + line for line in block.split('\n'))) 382 | STDERR.flush() 383 | 384 | 385 | # cleveref_required() -------------------------------------------------------- 386 | 387 | def cleveref_required(): 388 | """Returns True if the cleveref usage was found during xnos processing, 389 | False otherwise.""" 390 | return _cleveref_flag 391 | 392 | 393 | #============================================================================= 394 | # Element list functions 395 | 396 | # quotify() ------------------------------------------------------------------ 397 | 398 | def quotify(x): 399 | """Replaces Quoted elements in element list `x` with quoted strings. 400 | 401 | Pandoc uses the Quoted element when '--smart' is enabled. Note that 402 | output to TeX/pdf automatically triggers '--smart'. 403 | 404 | stringify() ignores Quoted elements. Use quotify() first to replace 405 | Quoted elements in `x` with quoted strings. `x` should be a deep copy so 406 | that the underlying document is left untouched. 407 | 408 | Returns `x`, modified in-place. 409 | """ 410 | 411 | def _quotify(key, value, fmt, meta): # pylint: disable=unused-argument 412 | """Replaced Quoted elements with quoted strings.""" 413 | if key == 'Quoted': 414 | ret = [] 415 | quote = '"' if value[0]['t'] == 'DoubleQuote' else "'" 416 | if value[1][0]['t'] == 'Str': 417 | value[1][0]['c'] = quote + value[1][0]['c'] 418 | else: 419 | ret.append(Str(quote)) 420 | 421 | if value[1][-1]['t'] == 'Str': 422 | value[1][-1]['c'] = value[1][-1]['c'] + quote 423 | ret += value[1] 424 | else: 425 | ret += value[1] + [Str(quote)] 426 | return ret 427 | return None 428 | 429 | return walk(walk(x, _quotify, '', {}), join_strings, '', {}) 430 | 431 | 432 | # dollarfy() ----------------------------------------------------------------- 433 | 434 | def dollarfy(x): 435 | """Replaces Math elements in element list `x` with a $-enclosed string. 436 | 437 | stringify() passes through TeX math. Use dollarfy(x) first to replace 438 | Math elements with math strings set in dollars. `x` should be a deep copy 439 | so that the underlying document is left untouched. 440 | 441 | Returns `x`, modified in-place. 442 | """ 443 | 444 | def _dollarfy(key, value, fmt, meta): # pylint: disable=unused-argument 445 | """Replaces Math elements""" 446 | if key == 'Math': 447 | return Str('$' + value[1] + '$') 448 | return None 449 | 450 | return walk(x, _dollarfy, '', {}) 451 | 452 | 453 | # extract_attrs() ------------------------------------------------------------ 454 | 455 | def extract_attrs(x, n): 456 | """Extracts attributes from element list `x` beginning at index `n`. 457 | 458 | The elements encapsulating the attributes (typically a series of Str and 459 | Space elements) are removed from `x`. Items before index `n` are left 460 | unchanged. 461 | 462 | Returns the attributes. A ValueError is raised if attributes aren't 463 | found. An IndexError is raised if the index `n` is out of range. 464 | """ 465 | 466 | # Check for the start of the attributes string 467 | if not (x[n]['t'] == 'Str' and x[n]['c'].startswith('{')): 468 | raise ValueError('Attributes not found.') 469 | 470 | # It starts with {, so this *may* be an attributes list. Search for where 471 | # the attributes end. Do not consider } in quoted elements. 472 | 473 | seq = [] # A sequence of saved values 474 | quotechar = None # Used to keep track of quotes in strings 475 | flag = False # Flags that an attributes list was found 476 | i = 0 # Initialization 477 | 478 | for i, v in enumerate(x[n:]): # Scan through the list 479 | if v and v['t'] == 'Str': 480 | # Scan for } outside of a quote 481 | for j, c in enumerate(v['c']): 482 | if c == quotechar: # This is an end quote 483 | quotechar = None 484 | elif c in ['"', "'"]: # This is an open quote 485 | quotechar = c 486 | elif c == '}' and quotechar is None: # The attributes end here 487 | # Split the string at the } and save the pieces 488 | head, tail = v['c'][:j+1], v['c'][j+1:] 489 | x[n+i] = copy.deepcopy(v) 490 | x[n+i]['c'] = tail 491 | v['c'] = head 492 | flag = True 493 | break 494 | seq.append(v) 495 | if flag: 496 | break 497 | 498 | if flag: # Attributes string was found, so process it 499 | 500 | # Delete empty and extracted elements 501 | if x[n+i]['t'] == 'Str' and not x[n+i]['c']: 502 | del x[n+i] 503 | del x[n:n+i] 504 | 505 | # Process the attrs 506 | attrstr = stringify(dollarfy(quotify(seq))).strip() 507 | attrs = PandocAttributes(attrstr, 'markdown') 508 | 509 | # Remove extranneous quotes from kvs (this is absolutely necessary 510 | # or else html attributes can get ""double-quoted"") 511 | for k, v in attrs.items(): # pylint: disable=unused-variable 512 | if v[0] == v[-1] == '"' or v[0] == "'" == v[-1] == "'": 513 | attrs[k] = v[1:-1] 514 | 515 | # We're done 516 | return attrs 517 | 518 | # Attributes not found 519 | raise ValueError('Attributes not found.') 520 | 521 | 522 | #============================================================================= 523 | # Actions and their factory functions 524 | 525 | # Actions act on pandoc json elements. The pandocfilters.walk() function 526 | # applies the action to all json elements in a document. A non-None return 527 | # value by an action is used by walk() to replace an element. It is often 528 | # easier to modify or delete elements from element lists in place. 529 | 530 | 531 | # join_strings() ------------------------------------------------------------- 532 | 533 | # Pandoc never produces adjacent Str elements. They may, however, arise from 534 | # processing by actions. This function joins adjacent string elements found 535 | # in Para and Plain blocks. 536 | 537 | # The design pattern used by this function is repeated by other actions, 538 | # below. Processing of an element list 'x' is relegated to a helper. The 539 | # helper processes the list iteratively. Processing is restarted (through use 540 | # of the _repeat() decorator) any time the element list is changed. A value 541 | # of None is returned by the outer function because all modifications are made 542 | # in place. 543 | 544 | @_repeat 545 | def _join_strings(x, start=0): 546 | """Joins adjacent Str elements found in the element list `x`.""" 547 | for i in range(start, len(x)-1): # Process successive pairs of elements 548 | if x[i]['t'] == 'Str' and x[i+1]['t'] == 'Str': 549 | x[i]['c'] += x[i+1]['c'] 550 | del x[i+1] # In-place deletion of element from list 551 | return None # Forces processing to repeat 552 | return True # Terminates processing 553 | 554 | # pylint: disable=unused-argument 555 | def join_strings(key, value, fmt=None, meta=None): 556 | """Joins adjacent Str elements found in `value`.""" 557 | if key in ['Para', 'Plain']: 558 | _join_strings(value) 559 | elif key == 'Span': 560 | _join_strings(value, 1) 561 | elif key == 'Image': 562 | _join_strings(value[-2]) 563 | elif key == 'Table': 564 | if version(_PANDOCVERSION) < version('2.10'): 565 | _join_strings(value[-5]) 566 | else: 567 | _join_strings(value[-5]['c'][1][0]['c']) 568 | 569 | 570 | 571 | # repair_refs() ------------------------------------------------------------- 572 | 573 | # Reference pattern. This splits a reference into three components: the 574 | # prefix, label and suffix. e.g.: 575 | # >>> _REF.match('xxx{+@fig:1}xxx').groups() 576 | # ('xxx{+', 'fig:1', '}xxx'). 577 | _REF = re.compile(r'^((?:.*{)?[\*\+!]?)@([^:]*:[\w/-]+)(.*)') 578 | 579 | def _is_broken_ref(key1, value1, key2, value2): 580 | """True if the keys and values represent a broken reference; 581 | False otherwise. 582 | """ 583 | # A link followed by a string may represent a broken reference 584 | if key1 != 'Link' or key2 != 'Str': 585 | return False 586 | # Assemble the parts 587 | n = 0 if version(_PANDOCVERSION) < version('1.16') else 1 588 | 589 | if isinstance(value1[n][0]['c'], list): 590 | # Occurs when there is quoted text in an actual link. This is not 591 | # a broken link. See Issue #1. 592 | return False 593 | 594 | s = value1[n][0]['c'] + value2 595 | # Return True if this matches the reference pattern 596 | return bool(_REF.match(s)) 597 | 598 | @_repeat 599 | def _repair_refs(x): 600 | """Performs the repair on the element list `x`.""" 601 | 602 | if not bool(_PANDOCVERSION): 603 | raise RuntimeError('Module uninitialized. Please call init().') 604 | 605 | # Scan the element list x 606 | for i in range(len(x)-1): 607 | 608 | # Check for broken references 609 | if _is_broken_ref(x[i]['t'], x[i]['c'] if 'c' in x[i] else [], 610 | x[i+1]['t'], x[i+1]['c'] if 'c' in x[i+1] else []): 611 | 612 | # Get the reference string 613 | n = 0 if version(_PANDOCVERSION) < version('1.16') else 1 614 | s = x[i]['c'][n][0]['c'] + x[i+1]['c'] 615 | 616 | # Chop it into pieces. Note that the prefix and suffix may be 617 | # parts of other broken references. 618 | prefix, label, suffix = _REF.match(s).groups() 619 | 620 | # Insert the suffix, label and prefix back into x. Do it in this 621 | # order so that the indexing works. 622 | if suffix: 623 | x.insert(i+2, Str(suffix)) 624 | x[i+1] = Cite( 625 | [{"citationId":label, 626 | "citationPrefix":[], 627 | "citationSuffix":[], 628 | "citationNoteNum":0, 629 | "citationMode":{"t":"AuthorInText", "c":[]}, 630 | "citationHash":0}], 631 | [Str('@' + label)]) 632 | if prefix: 633 | if i > 0 and x[i-1]['t'] == 'Str': 634 | x[i-1]['c'] = x[i-1]['c'] + prefix 635 | del x[i] 636 | else: 637 | x[i] = Str(prefix) 638 | else: 639 | del x[i] 640 | 641 | return None # Forces processing to repeat 642 | 643 | return True # Terminates processing 644 | 645 | def repair_refs(key, value, fmt, meta): # pylint: disable=unused-argument 646 | """Using '-f markdown+autolink_bare_uris' with pandoc < 1.18 splits a 647 | reference like '{@fig:one}' into email Link and Str elements. This 648 | function replaces the mess with the Cite and Str elements we normally 649 | get. Call this before any reference processing.""" 650 | 651 | if version(_PANDOCVERSION) >= version('1.18'): 652 | return 653 | 654 | # The problem spans multiple elements, and so can only be identified in 655 | # element lists. Element lists are encapsulated in different ways. We 656 | # must process them all. 657 | 658 | if key in ('Para', 'Plain'): 659 | _repair_refs(value) 660 | elif key == 'Image': 661 | _repair_refs(value[-2]) 662 | elif key == 'Table': 663 | _repair_refs(value[-5]) 664 | 665 | 666 | # process_refs_factory() ----------------------------------------------------- 667 | 668 | def _extract_modifier(x, i, attrs): 669 | """Extracts the */+/! modifier in front of the Cite at index `i` of the 670 | element list `x`. The modifier is stored in `attrs`. Returns the 671 | updated index `i`. 672 | """ 673 | 674 | global _cleveref_flag # pylint: disable=global-statement 675 | 676 | assert x[i]['t'] == 'Cite' 677 | 678 | # The modifier can either be found in the Cite prefix or in the Str 679 | # preceeding the Cite. We must handle both cases. 680 | 681 | s = None # The string containing the modifier 682 | modifier = None # The modifier character 683 | has_prefix = False # Flags that the Cite has a prefix 684 | if x[i]['c'][-2][0]['citationPrefix'] and \ 685 | x[i]['c'][-2][0]['citationPrefix'][-1]['t'] == 'Str': 686 | # Modifier is in the last character of the citation prefix 687 | s = x[i]['c'][-2][0]['citationPrefix'][-1]['c'] 688 | modifier = s[-1] 689 | has_prefix = True 690 | elif i > 0 and x[i-1]['t'] == 'Str': 691 | # Modifier is in the last character of the previous string 692 | s = x[i-1]['c'] 693 | modifier = s[-1] 694 | if modifier: 695 | if not _cleveref_flag and modifier in ['*', '+']: 696 | _cleveref_flag = True 697 | if modifier in ['*', '+', '!']: 698 | attrs['modifier'] = modifier 699 | if len(s) > 1: # Lop the modifier off of the string 700 | if has_prefix: 701 | x[i]['c'][-2][0]['citationPrefix'][-1]['c'] = \ 702 | x[i]['c'][-2][0]['citationPrefix'][-1]['c'][:-1] 703 | else: 704 | x[i-1]['c'] = x[i-1]['c'][:-1] 705 | # The element contains only the modifier; delete it 706 | elif has_prefix: 707 | del x[i]['c'][-2][0]['citationPrefix'][-1] 708 | else: 709 | del x[i-1] 710 | i -= 1 711 | return i 712 | 713 | def _remove_brackets(x, i): 714 | """Removes curly brackets surrounding the Cite element at index `i` in 715 | the element list `x`. It is assumed that the modifier has been 716 | extracted. Empty strings are deleted from `x`. Returns the updated 717 | index `i`. 718 | """ 719 | 720 | assert x[i]['t'] == 'Cite' 721 | 722 | # Look at the Cite prefix/suffix if available, otherwise the surrounding 723 | # text. 724 | if x[i]['c'][-2][0]['citationPrefix'] and \ 725 | x[i]['c'][-2][0]['citationSuffix']: 726 | if x[i]['c'][-2][0]['citationPrefix'][-1]['t'] == \ 727 | x[i]['c'][-2][0]['citationSuffix'][0]['t'] == 'Str': 728 | # The surrounding elements are strings; trim off curly brackets 729 | if x[i]['c'][-2][0]['citationPrefix'][-1]['c'].endswith('{') and \ 730 | x[i]['c'][-2][0]['citationSuffix'][0]['c'].startswith('}'): 731 | if len(x[i]['c'][-2][0]['citationSuffix'][0]['c']) > 1: 732 | x[i]['c'][-2][0]['citationSuffix'][0]['c'] = \ 733 | x[i]['c'][-2][0]['citationSuffix'][0]['c'][1:] 734 | else: 735 | del x[i]['c'][-2][0]['citationSuffix'][0] 736 | if len(x[i]['c'][-2][0]['citationPrefix'][-1]['c']) > 1: 737 | x[i]['c'][-2][0]['citationPrefix'][-1]['c'] = \ 738 | x[i]['c'][-2][0]['citationPrefix'][-1]['c'][:-1] 739 | else: 740 | del x[i]['c'][-2][0]['citationPrefix'][-1] 741 | 742 | elif 0 < i < len(x)-1 and x[i-1]['t'] == x[i+1]['t'] == 'Str': 743 | # The surrounding elements are strings; trim off curly brackets 744 | if x[i-1]['c'].endswith('{') and x[i+1]['c'].startswith('}'): 745 | if len(x[i+1]['c']) > 1: 746 | x[i+1]['c'] = x[i+1]['c'][1:] 747 | else: 748 | del x[i+1] 749 | if len(x[i-1]['c']) > 1: 750 | x[i-1]['c'] = x[i-1]['c'][:-1] 751 | else: 752 | del x[i-1] 753 | return i-1 754 | 755 | return i 756 | 757 | # Track bad labels so that we only warn about them once 758 | badlabels = [] 759 | 760 | @_repeat 761 | def _process_refs(x, pattern, labels): 762 | """Searches the element list `x` for the first Cite element with an id 763 | that either matches the compiled regular expression `pattern` or is found in 764 | the `labels` list. Strips surrounding curly braces and adds modifiers to 765 | the attributes of the Cite element. Repeats processing (via decorator) 766 | until all matching Cite elements in `x` are processed.""" 767 | 768 | # Scan the element list x for Cite elements with known labels 769 | for i, v in enumerate(x): 770 | if v['t'] == 'Cite' and len(v['c']) == 2: 771 | 772 | label = v['c'][-2][0]['citationId'] 773 | if not label in labels and ':' in label: 774 | testlabel = label.split(':')[-1] 775 | if testlabel in labels: 776 | label = testlabel 777 | 778 | if (pattern and pattern.match(label)) or label in labels: 779 | 780 | # A new reference was found; create some empty attrs for it 781 | attrs = PandocAttributes() 782 | 783 | # Extract the modifiers. 'attrs' is updated in place. 784 | # Element deletion could change the index of the Cite being 785 | # processed. 786 | i = _extract_modifier(x, i, attrs) 787 | 788 | # Remove surrounding brackets 789 | i = _remove_brackets(x, i) 790 | 791 | # Get the reference attributes. Attributes must immediately 792 | # follow the label. 793 | if not v['c'][0][0]['citationSuffix'] and \ 794 | not stringify(v['c'][-1]).endswith(']'): 795 | try: 796 | a = extract_attrs(x, i+1) 797 | attrs.id = a.id 798 | attrs.classes.extend(a.classes) 799 | attrs.kvs.update(a.kvs) 800 | except (ValueError, IndexError): 801 | pass # None given 802 | 803 | # Attach the attributes 804 | v['c'].insert(0, attrs.list) 805 | 806 | # The element list may be changed 807 | if label in labels: 808 | return None # Forces processing to repeat via @_repeat 809 | 810 | if _WARNINGLEVEL and pattern and \ 811 | pattern.match(label) and label not in badlabels: 812 | badlabels.append(label) 813 | msg = "\n%s: Bad reference: @%s.\n" % (_FILTERNAME, label) 814 | STDERR.write(msg) 815 | STDERR.flush() 816 | 817 | return True # Terminates processing in _repeat decorator 818 | 819 | @_compat 820 | def process_refs_factory(regex, labels, warninglevel=None): 821 | """Returns process_refs(key, value, fmt, meta) action that processes 822 | text around a reference. References are encapsulated in pandoc Cite 823 | elements. 824 | 825 | Consider the markdown '{+@fig:1}', which represents a reference to a 826 | figure. '@' denotes a reference, 'fig:1' is the reference's label, and 827 | '+' is a modifier. Valid modifiers are '+', '*' and '!'. 828 | 829 | Only references with labels that match the regular expression `regex` or 830 | are found in the `labels` list are processed. Curly braces are stripped 831 | and modifiers are stored in the `modifier` field of the Cite element's 832 | attributes. 833 | 834 | Cite attributes must be detached before the document is written to 835 | STDOUT because pandoc doesn't recognize them. Alternatively, an action 836 | from replace_refs_factory() can be used to replace the references 837 | altogether. 838 | 839 | Parameters: 840 | 841 | regex - regular expression (or compiled pattern) that matches references 842 | labels - a list of known target labels 843 | warninglevel - DEPRECATED (set global WARNINGLEVEL instead) 844 | """ 845 | 846 | # Set the global warning level (DEPRECATED) 847 | # pylint: disable=global-statement 848 | global _WARNINGLEVEL 849 | assert(warninglevel is None or isinstance(warninglevel, int)) 850 | if warninglevel is not None: 851 | _WARNINGLEVEL = warninglevel 852 | 853 | # Compile the regex if it is a string; otherwise assume it is either a 854 | # compiled pattern or None. 855 | pattern = re.compile(regex) if isinstance(regex, STRTYPES) else regex 856 | 857 | # pylint: disable=unused-argument 858 | def process_refs(key, value, fmt, meta): 859 | """Processes references.""" 860 | # References may occur in a variety of places; we must process them 861 | # all. 862 | 863 | if key in ['Para', 'Plain', 'Emph', 'Strong']: 864 | _process_refs(value, pattern, labels) 865 | elif key in ['Span', 'Header']: 866 | _process_refs(value[-1], pattern, labels) 867 | elif key == 'Image': 868 | _process_refs(value[-2], pattern, labels) 869 | elif key == 'Table': 870 | if version(_PANDOCVERSION) < version('2.10'): 871 | _process_refs(value[-5], pattern, labels) 872 | elif version(_PANDOCVERSION) < version('2.11'): 873 | if value[-5]['c'][1]: 874 | _process_refs(value[-5]['c'][1][0]['c'], pattern, labels) 875 | else: 876 | if value[-5][1]: 877 | _process_refs(value[-5][1][0]['c'], pattern, labels) 878 | 879 | elif key == 'Cite': 880 | _process_refs(value[-2][0]['citationPrefix'], pattern, labels) 881 | _process_refs(value[-2][0]['citationSuffix'], pattern, labels) 882 | 883 | return process_refs 884 | 885 | 886 | # replace_refs_factory() ------------------------------------------------------ 887 | 888 | # Type for target metadata 889 | Target = collections.namedtuple('Target', ['num', 'secno', 'has_duplicate', 890 | 'name']) 891 | Target.__new__.__defaults__ = (None,) * len(Target._fields) 892 | 893 | # pylint: disable=too-many-arguments,unused-argument 894 | def replace_refs_factory(references, use_cleveref_default, use_eqref, 895 | plusname, starname, allow_implicit_refs=False): 896 | """Returns replace_refs(key, value, fmt, meta) action that replaces 897 | references encapsulated in Cite elements with format-specific content. 898 | The content is determined using the `references` dict, which maps 899 | reference labels to Target objects. 900 | 901 | If `use_cleveref_default` is True, or if `modifier` in the reference's 902 | attributes is '+' or '*, then clever referencing is used; i.e., a name is 903 | placed in front of the number or string tag. The`'plusname` and `starname` 904 | lists give the singular and plural names for '+' and '*' clever references, 905 | respectively. 906 | """ 907 | 908 | global _cleveref_flag # pylint: disable=global-statement 909 | 910 | # Update global if clever referencing is required by default 911 | _cleveref_flag = _cleveref_flag or use_cleveref_default 912 | 913 | # pylint: disable=too-many-locals,unused-argument 914 | def _cite_replacement(key, value, fmt, meta): 915 | """Returns context-dependent content to replace a Cite element.""" 916 | 917 | assert key == 'Cite' 918 | 919 | # Extract the attributes 920 | attrs = PandocAttributes(value[0], 'pandoc') 921 | 922 | # Check if the nolink attribute is set 923 | nolink = attrs['nolink'].capitalize() == 'True' if 'nolink' in attrs \ 924 | else False 925 | 926 | # Extract the label 927 | label = value[-2][0]['citationId'] 928 | if allow_implicit_refs and not label in references and ':' in label: 929 | testlabel = label.split(':')[-1] 930 | if testlabel in references: 931 | label = testlabel 932 | 933 | # Get the target metadata; typecast it as a Target for easier access 934 | target = references[label] if label in references else None 935 | if target and not isinstance(target, Target): 936 | target = Target(*target) 937 | 938 | # Issue a warning for duplicate targets 939 | if _WARNINGLEVEL and target and target.has_duplicate: 940 | msg = textwrap.dedent(""" 941 | %s: Referenced label has duplicate: %s 942 | """ % (_FILTERNAME, label)) 943 | STDERR.write(msg) 944 | STDERR.flush() 945 | 946 | # Get the replacement value 947 | text = str(target.num) if target else '??' 948 | 949 | # Choose between \Cref, \cref and \ref 950 | use_cleveref = attrs['modifier'] in ['*', '+'] \ 951 | if 'modifier' in attrs else use_cleveref_default 952 | is_plus_ref = attrs['modifier'] == '+' if 'modifier' in attrs \ 953 | else use_cleveref_default 954 | refname = plusname[0] if is_plus_ref else starname[0] # Reference name 955 | 956 | # The replacement content depends on the output format 957 | if fmt == 'latex': 958 | if use_cleveref: 959 | macro = r'\cref' if is_plus_ref else r'\Cref' 960 | ret = RawInline('tex', r'%s{%s}'%(macro, label)) 961 | elif use_eqref: 962 | ret = RawInline('tex', r'\eqref{%s}'%label) 963 | else: 964 | ret = RawInline('tex', r'\ref{%s}'%label) 965 | if nolink: # https://tex.stackexchange.com/a/323919 966 | ret['c'][1] = \ 967 | r'{\protect\NoHyper' + ret['c'][1] + r'\protect\endNoHyper}' 968 | else: 969 | if use_eqref: 970 | text = '(' + text + ')' 971 | 972 | elem = Math({"t":"InlineMath", "c":[]}, text[1:-1]) \ 973 | if text.startswith('$') and text.endswith('$') \ 974 | else Str(text) 975 | 976 | if not nolink and target: 977 | prefix = 'ch%03d.xhtml' % target.secno \ 978 | if fmt in ['epub', 'epub2', 'epub3'] and \ 979 | target.secno else '' 980 | 981 | elem = elt('Link', 2)([elem], 982 | ['%s#%s' % (prefix, label), '']) \ 983 | if version(_PANDOCVERSION) < version('1.16') else \ 984 | Link(['', [], []], [elem], ['%s#%s' % (prefix, label), '']) 985 | 986 | ret = ([Str(refname + NBSP)] if use_cleveref else []) + [elem] 987 | 988 | # If the Cite was square-bracketed then wrap everything in a span 989 | s = stringify(value[-1]) 990 | 991 | # pandoc strips off intervening space between the prefix and the Cite; 992 | # we may have to add it back in 993 | prefix = value[-2][0]['citationPrefix'] 994 | spacer = [Space()] \ 995 | if prefix and not stringify(prefix).endswith(('{', '+', '*', '!')) \ 996 | else [] 997 | if s.startswith('[') and s.endswith(']'): 998 | els = value[-2][0]['citationPrefix'] + \ 999 | spacer + ([ret] if fmt == 'latex' else ret) + \ 1000 | value[-2][0]['citationSuffix'] 1001 | # We don't yet know if there will be attributes, so leave them 1002 | # as None. This is fixed later when attributes are processed. 1003 | ret = Span(None, els) 1004 | 1005 | return ret 1006 | 1007 | 1008 | def replace_refs(key, value, fmt, meta): # pylint: disable=unused-argument 1009 | """Replaces references with format-specific content.""" 1010 | if key == 'Cite' and len(value) == 3: # Replace the reference 1011 | return _cite_replacement(key, value, fmt, meta) 1012 | return None 1013 | 1014 | return replace_refs 1015 | 1016 | 1017 | # attach_attrs_factory() ----------------------------------------------------- 1018 | 1019 | # pylint: disable=redefined-outer-name 1020 | @_compat 1021 | def attach_attrs_factory(f, warninglevel=None, 1022 | extract_attrs=extract_attrs, 1023 | allow_space=False, replace=False): 1024 | """Returns attach_attrs(key, value, fmt, meta) action that reads and 1025 | attaches attributes to unattributed elements generated by the 1026 | pandocfilters function `f` (e.g. pandocfilters.Math, etc). 1027 | 1028 | The extract_attrs() function should read and return the attributes and 1029 | raise a ValueError or IndexError if attributes are not found. 1030 | 1031 | Parameters: 1032 | 1033 | f - the pandoc constructor for the elements of interest 1034 | warninglevel - DEPRECATED (set global WARNINGLEVEL instead) 1035 | extract_attrs - a function to extract attributes from an element list; 1036 | defaults to the extract_attrs() function in this module 1037 | allow_space - flags that a space should be allowed between an element and 1038 | its attributes 1039 | replace - flags that existing attributes should be replaced 1040 | """ 1041 | 1042 | # Set the global warning level (DEPRECATED) 1043 | # pylint: disable=global-statement 1044 | global _WARNINGLEVEL 1045 | assert(warninglevel is None or isinstance(warninglevel, int)) 1046 | if warninglevel is not None: 1047 | _WARNINGLEVEL = warninglevel 1048 | 1049 | # Get the name of the element from the function 1050 | elname = f.__closure__[0].cell_contents 1051 | 1052 | @_repeat 1053 | def _attach_attrs(x): 1054 | """Extracts and attaches the attributes.""" 1055 | for i, v in enumerate(x): 1056 | if v and v['t'] == elname: # Find where the attributes start 1057 | n = i+1 1058 | if allow_space and n < len(x) and x[n]['t'] == 'Space': 1059 | n += 1 1060 | try: # Extract the attributes 1061 | attrs = extract_attrs(x, n) 1062 | if _WARNINGLEVEL and attrs.parse_failed: 1063 | msg = textwrap.dedent(""" 1064 | %s: Malformed attributes: 1065 | %s 1066 | """ % (_FILTERNAME, attrs.attrstr)) 1067 | STDERR.write(msg) 1068 | STDERR.flush() 1069 | 1070 | if replace: 1071 | x[i]['c'][0] = attrs.list 1072 | else: 1073 | x[i]['c'].insert(0, attrs.list) 1074 | except (ValueError, IndexError): 1075 | if v['t'] == 'Span' and v['c'][0] is None: 1076 | # We changed this into a span before, but since 1077 | # the attributes are None (it was unattributed), it 1078 | # isn't a valid span. Fix it. 1079 | els = x.pop(i)['c'][1] 1080 | els.insert(0, Str('[')) 1081 | els.append(Str(']')) 1082 | for j, el in enumerate(els): 1083 | x.insert(i+j, el) 1084 | join_strings('Span', x) 1085 | return None 1086 | return True 1087 | 1088 | def attach_attrs(key, value, fmt, meta): # pylint: disable=unused-argument 1089 | """Attaches attributes to an element.""" 1090 | if key in ['Para', 'Plain']: 1091 | _attach_attrs(value) 1092 | 1093 | # Image: Add pandoc's figure marker if warranted 1094 | if len(value) == 1 and value[0]['t'] == 'Image': 1095 | value[0]['c'][-1][1] = 'fig:' 1096 | 1097 | return attach_attrs 1098 | 1099 | 1100 | # detach_attrs_factory() ------------------------------------------------------ 1101 | 1102 | def detach_attrs_factory(f, restore=False): 1103 | """Returns detach_attrs(key, value, fmt, meta) action that detaches 1104 | attributes attached to elements of type `f` (e.g. pandocfilters.Math, etc). 1105 | Attributes provided natively by pandoc are left as is.""" 1106 | 1107 | # Get the name and standard length 1108 | name = f.__closure__[0].cell_contents 1109 | n = f.__closure__[1].cell_contents 1110 | 1111 | def detach_attrs(key, value, fmt, meta): # pylint: disable=unused-argument 1112 | """Detaches the attributes.""" 1113 | if key == name: 1114 | assert len(value) <= n+1 1115 | if len(value) == n+1: 1116 | # Make sure value[0] represents attributes then delete 1117 | assert len(value[0]) == 3 1118 | assert isinstance(value[0][0], STRTYPES) 1119 | assert isinstance(value[0][1], list) 1120 | assert isinstance(value[0][2], list) 1121 | attrs = PandocAttributes(value[0], 'pandoc') 1122 | del value[0] 1123 | if restore: 1124 | return [elt(key, *value), Str(attrs.to_markdown())] 1125 | return None 1126 | 1127 | return detach_attrs 1128 | 1129 | 1130 | # insert_secnos_factory() ---------------------------------------------------- 1131 | 1132 | def insert_secnos_factory(f): 1133 | """Returns insert_secnos(key, value, fmt, meta) action that inserts 1134 | section numbers into the attributes of elements of type `f`. 1135 | """ 1136 | 1137 | # Get the name and standard length 1138 | name = f.__closure__[0].cell_contents 1139 | n = f.__closure__[1].cell_contents 1140 | 1141 | def insert_secnos(key, value, fmt, meta): # pylint: disable=unused-argument 1142 | """Inserts section numbers into elements attributes.""" 1143 | 1144 | global _sec # pylint: disable=global-statement 1145 | 1146 | if key == 'Header': 1147 | if 'unnumbered' in value[1][1]: 1148 | return 1149 | if value[0] == 1: 1150 | _sec += 1 1151 | if key == name: 1152 | 1153 | # Only insert if attributes are attached. Images always have 1154 | # attributes for pandoc >= 1.16. Same for Spans. Tables always 1155 | # have attributes for pandoc >= 10.1. 1156 | assert len(value) <= n+1 1157 | # pylint: disable=too-many-boolean-expressions 1158 | if (name == 'Image' and len(value) == 3) or \ 1159 | name == 'Div' or \ 1160 | name == 'Span' or \ 1161 | (name == 'Table' and len(value) == 6) or \ 1162 | len(value) == n+1: 1163 | 1164 | # Make sure value[0] represents attributes 1165 | assert isinstance(value[0][0], STRTYPES) 1166 | assert isinstance(value[0][1], list) 1167 | assert isinstance(value[0][2], list) 1168 | 1169 | # Insert the section number into the attributes 1170 | value[0][2].insert(0, ['secno', _sec]) 1171 | 1172 | return insert_secnos 1173 | 1174 | 1175 | # delete_secnos_factory() ---------------------------------------------------- 1176 | 1177 | def delete_secnos_factory(f): 1178 | """Returns delete_secnos(key, value, fmt, meta) action that deletes 1179 | section numbers from the attributes of elements of type `f`. 1180 | """ 1181 | 1182 | # Get the name and standard length 1183 | name = f.__closure__[0].cell_contents 1184 | n = f.__closure__[1].cell_contents 1185 | 1186 | def delete_secnos(key, value, fmt, meta): # pylint: disable=unused-argument 1187 | """Deletes section numbers from elements attributes.""" 1188 | 1189 | # Only delete if attributes are attached. Images always have 1190 | # attributes for pandoc >= 1.16. Same for Spans. Tables always 1191 | # have attributes for pandoc >= 10.1. 1192 | if key == name: 1193 | assert len(value) <= n+1 1194 | # pylint: disable=too-many-boolean-expressions 1195 | if (name == 'Image' and len(value) == 3) or \ 1196 | name == 'Div' or \ 1197 | name == 'Span' or \ 1198 | (name == 'Table' and len(value) == 6) or \ 1199 | len(value) == n+1: 1200 | 1201 | # Make sure value[0] represents attributes 1202 | assert isinstance(value[0][0], STRTYPES) 1203 | assert isinstance(value[0][1], list) 1204 | assert isinstance(value[0][2], list) 1205 | 1206 | # Remove the secno attribute 1207 | if value[0][2] and value[0][2][0][0] == 'secno': 1208 | del value[0][2][0] 1209 | 1210 | return delete_secnos 1211 | 1212 | 1213 | # install_rawblock_factory() ------------------------------------------------- 1214 | 1215 | def insert_rawblocks_factory(rawblocks): 1216 | r"""Returns insert_rawblocks(key, value, fmt, meta) action that inserts 1217 | non-duplicate RawBlock elements. 1218 | """ 1219 | 1220 | # pylint: disable=unused-argument 1221 | def insert_rawblocks(key, value, fmt, meta): 1222 | """Inserts non-duplicate RawBlock elements.""" 1223 | 1224 | if not rawblocks: 1225 | return None 1226 | 1227 | # Put the RawBlock elements in front of the first block element that 1228 | # isn't also a RawBlock. 1229 | 1230 | if not key in ['Plain', 'Para', 'CodeBlock', 'RawBlock', 1231 | 'BlockQuote', 'OrderedList', 'BulletList', 1232 | 'DefinitionList', 'Header', 'HorizontalRule', 1233 | 'Table', 'Div', 'Null']: 1234 | return None 1235 | 1236 | if key == 'RawBlock': # Remove duplicates 1237 | rawblock = RawBlock(*value) 1238 | if rawblock in rawblocks: 1239 | rawblocks.remove(rawblock) 1240 | return None 1241 | 1242 | if rawblocks: # Insert blocks 1243 | el = _getel(key, value) 1244 | return [rawblocks.pop(0) for i in range(len(rawblocks))] + [el] 1245 | 1246 | return None 1247 | 1248 | return insert_rawblocks 1249 | -------------------------------------------------------------------------------- /pandocxnos/main.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | 3 | """main: a wrapper for the pandoc-fignos/eqnos/tablenos/secnos filters.""" 4 | 5 | # Copyright 2015-2020 Thomas J. Duck. 6 | # All rights reserved. 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, version 3. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | 20 | import io 21 | 22 | from pandocxnos import STDIN, STDOUT 23 | 24 | FILTERS = ['pandoc_fignos', 'pandoc_eqnos', 'pandoc_tablenos', 'pandoc_secnos'] 25 | 26 | def main(): 27 | """Main program.""" 28 | 29 | stdin = STDIN 30 | for name in FILTERS: 31 | try: 32 | stdout = io.TextIOWrapper(io.BytesIO(), STDOUT.encoding) 33 | m = __import__(name) 34 | if m.__version__ < '2.0.0': 35 | msg = '%s must have version number 2.0.0 or higher to be'\ 36 | 'called by the pandoc-xnos filter.' % name 37 | raise RuntimeError(msg) 38 | m.main(stdin, stdout) 39 | stdin = stdout 40 | stdin.seek(0) 41 | except ImportError: 42 | pass 43 | STDOUT.write(stdin.read()) 44 | STDOUT.flush() 45 | 46 | if __name__ == '__main__': 47 | main() 48 | -------------------------------------------------------------------------------- /pandocxnos/pandocattributes.py: -------------------------------------------------------------------------------- 1 | # Copyright 2014-2015 Aaron O'Leary 2 | # Copyright 2019 Thomas J. Duck 3 | # 4 | # Redistribution and use in source and binary forms, with or without 5 | # modification, are permitted provided that the following conditions are met: 6 | # 7 | # 1. Redistributions of source code must retain the above copyright notice, 8 | # this list of conditions and the following disclaimer. 9 | # 10 | # 2. Redistributions in binary form must reproduce the above copyright notice, 11 | # this list of conditions and the following disclaimer in the documentation 12 | # and/or other materials provided with the distribution. 13 | # 14 | # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 15 | # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 | # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 | # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 18 | # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 19 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 20 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 21 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 22 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 23 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 | # POSSIBILITY OF SUCH DAMAGE. 25 | 26 | """pandocattributes.py - pandoc block attributes management. 27 | 28 | Usage: 29 | 30 | attrs = '#id .class1 .class2 key=value' 31 | attributes = PandocAttributes(attrs, 'markdown') 32 | 33 | attributes.to_pandoc() 34 | >>> ['id', ['class1', 'class2'], [['key', 'value']]] 35 | 36 | attributes.to_markdown() 37 | >>> '{#id .class1 .class2 key=value}' 38 | 39 | attributes.to_html() 40 | >>> id="id" class="class1 class2" key='value' 41 | 42 | attributes.id 43 | >>> 'id' 44 | 45 | attributes.classes 46 | >>> ['class1', 'class2'] 47 | 48 | attributes.kvs 49 | >>> OrderedDict([('key', 'value')]) 50 | """ 51 | 52 | 53 | import re 54 | from collections import OrderedDict 55 | 56 | # pylint: disable=useless-object-inheritance 57 | class PandocAttributes(object): 58 | """Parser / Emitter for pandoc block attributes.""" 59 | 60 | spnl = ' \n' 61 | split_regex = r'''((?:[^{separator}"']|"[^"]*"|'[^']*')+)'''.format 62 | 63 | parse_failed = False # Flags if not all fields were parsed 64 | attrstr = None # The original attribute string 65 | 66 | def __init__(self, attrstr=None, fmt='pandoc'): 67 | """Initializes the attributes.""" 68 | if attrstr is None: 69 | id_ = '' 70 | classes = [] 71 | kvs = OrderedDict() 72 | elif fmt == 'pandoc': 73 | id_, classes, kvs = self._parse_pandoc(attrstr) 74 | elif fmt == 'markdown': 75 | id_, classes, kvs = self._parse_markdown(attrstr) 76 | else: 77 | raise UserWarning('invalid format') 78 | 79 | self.id = id_ 80 | self.classes = classes 81 | self.kvs = kvs 82 | self.attrstr = attrstr 83 | 84 | @staticmethod 85 | def _parse_pandoc(attrs): 86 | """Read pandoc attributes.""" 87 | id_ = attrs[0] 88 | classes = attrs[1] 89 | kvs = OrderedDict(attrs[2]) 90 | return id_, classes, kvs 91 | 92 | def _parse_markdown(self, attrstr): 93 | """Read markdown attributes.""" 94 | attrstr = attrstr.strip('{}') 95 | splitter = re.compile(self.split_regex(separator=self.spnl)) 96 | attrs = splitter.split(attrstr)[1::2] 97 | 98 | # Match single word attributes e.g. python 99 | if len(attrs) == 1 \ 100 | and not attrstr.startswith(('#', '.')) \ 101 | and '=' not in attrstr: 102 | return '', [attrstr], OrderedDict() 103 | 104 | try: 105 | id_ = [a[1:] for a in attrs if a.startswith('#')][0] 106 | except IndexError: 107 | id_ = '' 108 | 109 | classes = [a[1:] for a in attrs if a.startswith('.')] 110 | special = ['unnumbered' for a in attrs if a == '-'] 111 | classes.extend(special) 112 | 113 | nkvs = sum(1 for a in attrs if not a.startswith(('#', '.')) 114 | and a != '-') 115 | kvs = OrderedDict(a.split('=', 1) for a in attrs 116 | if '=' in a and a[0] != '=' and a[-1] != '=') 117 | 118 | if len(kvs) != nkvs: 119 | self.parse_failed = True 120 | 121 | return id_, classes, kvs 122 | 123 | def to_pandoc(self): 124 | """Returns the attributes as a pandoc-compatibile list.""" 125 | kvs = [[k, v] for k, v in self.kvs.items()] 126 | return [self.id, self.classes, kvs] 127 | 128 | def to_markdown(self, fmt='{id} {classes} {kvs}', surround=True): 129 | """Returns attributes formatted as markdown with optional 130 | fmt argument to determine order of attribute contents. 131 | """ 132 | id_ = '#' + self.id if self.id else '' 133 | classes = ' '.join('.' + cls for cls in self.classes) 134 | kvs = ' '.join('{}={}'.format(k, v) for k, v in self.kvs.items()) 135 | 136 | attrs = fmt.format(id=id_, classes=classes, kvs=kvs).strip() 137 | 138 | if surround: 139 | return '{' + attrs + '}' 140 | return attrs 141 | 142 | def to_html(self): 143 | """Returns attributes formatted as html.""" 144 | id_, classes, kvs = self.id, self.classes, self.kvs 145 | id_str = 'id="{}"'.format(id_) if id_ else '' 146 | class_str = 'class="{}"'.format(' '.join(classes)) if classes else '' 147 | key_str = ' '.join('{}={}'.format(k, v) for k, v in kvs.items()) 148 | return ' '.join((id_str, class_str, key_str)).strip() 149 | 150 | @property 151 | def list(self): 152 | """The attributes in list form.""" 153 | return self.to_pandoc() 154 | 155 | @property 156 | def markdown(self): 157 | """The attributes in html form.""" 158 | return self.to_markdown() 159 | 160 | @property 161 | def html(self): 162 | """The attributes in html form.""" 163 | return self.to_html() 164 | 165 | @property 166 | def is_empty(self): 167 | """Returns True if the attributes are empty; False otherwise.""" 168 | return self.id == '' and self.classes == [] and self.kvs == {} 169 | 170 | def __getitem__(self, item): 171 | """Gets 'id', 'classes', or an attribute.""" 172 | if item == 'id': 173 | return self.id 174 | if item == 'classes': 175 | return self.classes 176 | return self.kvs[item] 177 | 178 | def __setitem__(self, key, value): 179 | """Sets 'id', 'classes', or an attribute.""" 180 | if key == 'id': 181 | self.id = value 182 | elif key == 'classes': 183 | self.classes = value 184 | else: 185 | self.kvs[key] = value 186 | 187 | def __contains__(self, key): 188 | """Returns True if key is 'id', 'classes', or an attribute; 189 | False otherwise.""" 190 | return key == 'id' or key == 'classes' or key in self.kvs 191 | 192 | def __iter__(self): 193 | """Returns an interator over the kvs.""" 194 | return iter(self.kvs) 195 | 196 | def items(self): 197 | """Returns the kv items.""" 198 | return self.kvs.items() 199 | 200 | def __repr__(self): 201 | """Returns the string representation of self.""" 202 | return "pandocfilters.Attributes({})".format(self.to_pandoc()) 203 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | """setup.py - install script for pandoc-xnos.""" 2 | 3 | # Copyright 2015-2020 Thomas J. Duck. 4 | # All rights reserved. 5 | # 6 | # This program is free software: you can redistribute it and/or modify 7 | # it under the terms of the GNU General Public License as published by 8 | # the Free Software Foundation, version 3. 9 | # 10 | # This program is distributed in the hope that it will be useful, 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 | # GNU General Public License for more details. 14 | # 15 | # You should have received a copy of the GNU General Public License 16 | # along with this program. If not, see . 17 | 18 | import re 19 | import io 20 | 21 | from setuptools import setup 22 | 23 | # pylint: disable=invalid-name 24 | 25 | DESCRIPTION = 'Library code for the pandoc-xnos filter suite.' 26 | 27 | # From https://stackoverflow.com/a/39671214 28 | __version__ = re.search( 29 | r'__version__\s*=\s*[\'"]([^\'"]*)[\'"]', 30 | io.open('pandocxnos/core.py', encoding='utf_8_sig').read() 31 | ).group(1) 32 | 33 | setup( 34 | name='pandoc-xnos', 35 | version=__version__, 36 | 37 | author='Thomas J. Duck', 38 | author_email='tomduck@tomduck.ca', 39 | description=DESCRIPTION, 40 | long_description=DESCRIPTION, 41 | license='GPL', 42 | keywords='pandoc filters', 43 | url='https://github.com/tomduck/pandoc-xnos', 44 | download_url='https://github.com/tomduck/pandoc-xnos/tarball/'+__version__, 45 | 46 | install_requires=['pandocfilters>=1.4.2,<2', 47 | 'psutil>=4.1.0,<6'], 48 | 49 | packages=['pandocxnos'], 50 | entry_points={'console_scripts':['pandoc-xnos = pandocxnos:main']}, 51 | 52 | classifiers=[ 53 | 'Development Status :: 5 - Production/Stable', 54 | 'Intended Audience :: End Users/Desktop', 55 | 'Environment :: Console', 56 | 'License :: OSI Approved :: GNU General Public License v3 (GPLv3)', 57 | 'Programming Language :: Python' 58 | ], 59 | ) 60 | -------------------------------------------------------------------------------- /test/integration/Makefile: -------------------------------------------------------------------------------- 1 | 2 | PANDOC = pandoc-2.10.1 3 | 4 | FLAGS = --standalone --top-level-division=section --number-sections --filter pandoc-xnos 5 | 6 | all: out/demo.pdf out/demo.html out/demo.docx out/demo.json out/demo.tex 7 | 8 | out/%: demo.md 9 | @if [ ! -d $(dir $@) ]; then mkdir -p $(dir $@); fi 10 | $(PANDOC) $< $(FLAGS) -o $@ 11 | if [ ! -f out/fig.png ]; then cp fig.png out; fi 12 | 13 | .PHONY: clean 14 | 15 | clean: 16 | rm -rf out 17 | -------------------------------------------------------------------------------- /test/integration/demo.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Test Document 3 | xnos-cleveref: True 4 | xnos-number-by-section: True 5 | ... 6 | 7 | Title 1 8 | ======= 9 | 10 | Blah blah @fig:1, @eq:1. 11 | 12 | ![Caption.](fig.png){#fig:1 width=5%} 13 | 14 | $$ y = f(x) $${#eq:1} 15 | 16 | ![Caption.](fig.png){#fig:1.1 width=5%} 17 | 18 | $$ y = f(x) $${#eq:1.1} 19 | 20 | X Y 21 | - - 22 | T F 23 | 24 | Table: Foo. {#tbl:1} 25 | 26 | Reference to @sec:title-2. 27 | 28 | An uncaptioned table: 29 | 30 | X Y 31 | - - 32 | T F 33 | 34 | 35 | Subtitle 1 {-} 36 | ---------- 37 | 38 | Blah @fig:2, @eq:2. 39 | 40 | ![Caption.](fig.png){#fig:2 width=5%} 41 | 42 | ![Caption.](fig.png){#fig:2.1 width=5%} 43 | 44 | $$ y = f(x) $${#eq:2} 45 | 46 | $$ y = f(x) $${#eq:2.1} 47 | 48 | 49 | Title 2 50 | ======= 51 | 52 | Blah blah blah @fig:2, @eq:3. 53 | 54 | $$ y = f(x) $${#eq:3} 55 | 56 | ![Caption.](fig.png){#fig:3 width=5%} 57 | 58 | 59 | Subtitle 2 60 | ---------- 61 | 62 | Blah @fig:4, @eq:4. 63 | 64 | 65 | Subtitle 3 66 | ---------- 67 | 68 | $$ y = f(x) $${#eq:4} 69 | 70 | ![Caption.](fig.png){#fig:4 width=5%} 71 | 72 | ![Caption.](fig.png){#fig:4.1 tag="B.3" width=5%} 73 | 74 | ![Caption.](fig.png){#fig:4.2 width=5%} 75 | 76 | $$ y = f(x) $${#eq:4.1} 77 | 78 | $$ y = f(x) $${#eq:4.2} 79 | 80 | 81 | 82 | Title 3 83 | ======= 84 | 85 | ![Caption.](fig.png){#fig:5 width=5%} 86 | 87 | $$ y = f(x) $${#eq:5} 88 | -------------------------------------------------------------------------------- /test/integration/fig.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tomduck/pandoc-xnos/dff6978fe1283cda1950ac6d494257e35b8d30de/test/integration/fig.png -------------------------------------------------------------------------------- /test/test.py: -------------------------------------------------------------------------------- 1 | #! /usr/bin/env python3 2 | 3 | """Unit tests for pandoc-xnos.""" 4 | 5 | # Copyright 2016-2020 Thomas J. Duck. 6 | # All rights reserved. 7 | # 8 | # This program is free software: you can redistribute it and/or modify 9 | # it under the terms of the GNU General Public License as published by 10 | # the Free Software Foundation, version 3. 11 | # 12 | # This program is distributed in the hope that it will be useful, 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 | # GNU General Public License for more details. 16 | # 17 | # You should have received a copy of the GNU General Public License 18 | # along with this program. If not, see . 19 | 20 | # pylint: disable=eval-used, line-too-long 21 | 22 | import sys 23 | import unittest 24 | import subprocess 25 | 26 | from pandocfilters import walk, Math 27 | 28 | import pandocxnos 29 | from pandocxnos import PandocAttributes 30 | from pandocxnos import get_meta, elt 31 | from pandocxnos import join_strings 32 | from pandocxnos import quotify, dollarfy 33 | from pandocxnos import extract_attrs 34 | from pandocxnos import attach_attrs_factory, detach_attrs_factory 35 | from pandocxnos import insert_secnos_factory 36 | from pandocxnos import repair_refs, process_refs_factory, replace_refs_factory 37 | 38 | PANDOCVERSION = '2.8.1' 39 | PANDOC = 'pandoc-2.11.1.1' 40 | PANDOC1p15 = 'pandoc-1.15.2' # pylint: disable=invalid-name 41 | PANDOC_API_VERSION = '1,22' 42 | 43 | 44 | #----------------------------------------------------------------------------- 45 | # Test class 46 | 47 | # pylint: disable=too-many-public-methods 48 | class TestXnos(unittest.TestCase): 49 | """Test the pandocxnos package.""" 50 | 51 | maxDiff = None 52 | 53 | def setUp(self): 54 | """Sets up the test.""" 55 | pandocxnos.init(PANDOCVERSION) 56 | 57 | def test_get_meta_1(self): 58 | """Tests get_meta() #1.""" 59 | 60 | ## test.md empty 61 | 62 | # Command: pandoc test.md -t json -M foo=bar 63 | src = eval(r'''{"meta":{"foo":{"t":"MetaString","c":"bar"}},"blocks":[],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 64 | 65 | # Check src against current pandoc 66 | md = subprocess.Popen(('echo', ''), stdout=subprocess.PIPE) 67 | output = eval(subprocess.check_output( 68 | (PANDOC+' -t json -M foo=bar').split(), stdin=md.stdout).strip()) 69 | self.assertEqual(src, output) 70 | 71 | expected = 'bar' 72 | 73 | # Make the comparison 74 | self.assertEqual(get_meta(src['meta'], 'foo'), expected) 75 | 76 | 77 | def test_get_meta_2(self): 78 | """Tests get_meta() #2.""" 79 | 80 | ## test.md: ---\nfoo: bar\n... ## 81 | 82 | # Command: pandoc test.md -t json 83 | src = eval(r'''{"meta":{"foo":{"t":"MetaInlines","c":[{"t":"Str","c":"bar"}]}},"blocks":[],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 84 | 85 | # Check src against current pandoc 86 | md = subprocess.Popen(('echo', '---\nfoo: bar\n...'), 87 | stdout=subprocess.PIPE) 88 | output = eval(subprocess.check_output( 89 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 90 | self.assertEqual(src, output) 91 | 92 | expected = 'bar' 93 | 94 | # Make the comparison 95 | self.assertEqual(get_meta(src['meta'], 'foo'), expected) 96 | 97 | 98 | def test_get_meta_3(self): 99 | """Tests get_meta() #3.""" 100 | 101 | ## test.md: ---\nfoo:\n - bar\n - baz\n... ## 102 | 103 | # Command: pandoc test.md -t json 104 | src = eval(r'''{"meta":{"foo":{"t":"MetaList","c":[{"t":"MetaInlines","c":[{"t":"Str","c":"bar"}]},{"t":"MetaInlines","c":[{"t":"Str","c":"baz"}]}]}},"blocks":[],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 105 | 106 | # Check src against current pandoc 107 | md = subprocess.Popen(('echo', '---\nfoo:\n - bar\n - baz\n...'), 108 | stdout=subprocess.PIPE) 109 | output = eval(subprocess.check_output( 110 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 111 | self.assertEqual(src, output) 112 | 113 | expected = ['bar', 'baz'] 114 | 115 | # Make the comparison 116 | self.assertEqual(get_meta(src['meta'], 'foo'), expected) 117 | 118 | 119 | def test_get_meta_4(self): 120 | """Tests get_meta() #4.""" 121 | 122 | ## test.md: ---\nfoo: True\n... ## 123 | 124 | # Command: pandoc test.md -t json 125 | src = eval(r'''{"blocks": [], "pandoc-api-version": [%s], "meta": {"foo": {"t": "MetaBool", "c": True}}}'''%PANDOC_API_VERSION) 126 | 127 | # Check src against current pandoc 128 | md = subprocess.Popen(('echo', '---\nfoo: True\n...'), 129 | stdout=subprocess.PIPE) 130 | output = eval(subprocess.check_output( 131 | (PANDOC+' -t json').split(), 132 | stdin=md.stdout).strip().decode("utf-8").replace('true', 'True')) 133 | self.assertEqual(src, output) 134 | 135 | expected = True 136 | 137 | # Make the comparison 138 | self.assertEqual(get_meta(src['meta'], 'foo'), expected) 139 | 140 | 141 | def test_insert_secnos_factory_1(self): 142 | """Tests insert_secnos_factory() #1.""" 143 | 144 | ## test.md: ---\nxnos-number-sections: True\n...\n\n# Title\n\n$$ x $$ {#eq:1}\n ## 145 | 146 | # Command: pandoc test.md -t json 147 | src = eval(r'''{"blocks": [{"t": "Header", "c": [1, ["title", [], []], [{"t": "Str", "c": "Title"}]]}, {"t": "Para", "c": [{"t": "Math", "c": [{"t": "DisplayMath"}, " x "]}, {"t": "Space"}, {"t": "Str", "c": "{#eq:1}"}]}], "pandoc-api-version": [%s], "meta": {"xnos-number-sections": {"t": "MetaBool", "c": True}}}'''%PANDOC_API_VERSION) 148 | 149 | # Check src against current pandoc 150 | md = subprocess.Popen(('echo', '---\nxnos-number-sections: True\n...\n\n# Title\n\n$$ x $$ {#eq:1}\n'), stdout=subprocess.PIPE) 151 | 152 | output = eval(subprocess.check_output( 153 | (PANDOC+' -t json').split(), 154 | stdin=md.stdout).strip().decode("utf-8").replace('true', 'True')) 155 | self.assertEqual(src, output) 156 | 157 | expected = eval(r'''{"blocks": [{"t": "Header", "c": [1, ["title", [], []], [{"t": "Str", "c": "Title"}]]}, {"t": "Para", "c": [{"t": "Math", "c": [["eq:1", [], [["secno", 1]]], {"t": "DisplayMath"}, " x "]}, {"t": "Space"}]}], "pandoc-api-version": [%s], "meta": {"xnos-number-sections": {"t": "MetaBool", "c": True}}}'''%PANDOC_API_VERSION) 158 | 159 | # Make the comparison 160 | meta = src['meta'] 161 | fmt = 'html' 162 | attach_attrs_math = attach_attrs_factory(Math, 0, allow_space=True) 163 | insert_secnos = insert_secnos_factory(Math) 164 | tmp = walk(src, attach_attrs_math, fmt, meta) 165 | self.assertEqual(walk(tmp, insert_secnos, fmt, meta), expected) 166 | 167 | 168 | def test_insert_secnos_factory_2(self): 169 | """Tests insert_secnos_factory() #2.""" 170 | 171 | ## test.md: ---\nxnos-number-sections: True\n...\n\n# Title\n\n$$ x $$\n ## 172 | 173 | # Command: pandoc test.md -t json 174 | src = eval(r'''{"blocks": [{"t": "Header", "c": [1, ["title", [], []], [{"t": "Str", "c": "Title"}]]}, {"t": "Para", "c": [{"t": "Math", "c": [{"t": "DisplayMath"}, " x "]}]}], "pandoc-api-version": [%s], "meta": {"xnos-number-sections": {"t": "MetaBool", "c": True}}}'''%PANDOC_API_VERSION) 175 | 176 | # Check src against current pandoc 177 | md = subprocess.Popen(('echo', '---\nxnos-number-sections: True\n...\n\n# Title\n\n$$ x $$\n'), stdout=subprocess.PIPE) 178 | 179 | output = eval(subprocess.check_output( 180 | (PANDOC+' -t json').split(), 181 | stdin=md.stdout).strip().decode("utf-8").replace('true', 'True')) 182 | self.assertEqual(src, output) 183 | 184 | expected = eval(r'''{"blocks": [{"t": "Header", "c": [1, ["title", [], []], [{"t": "Str", "c": "Title"}]]}, {"t": "Para", "c": [{"t": "Math", "c": [{"t": "DisplayMath"}, " x "]}]}], "pandoc-api-version": [%s], "meta": {"xnos-number-sections": {"t": "MetaBool", "c": True}}}'''%PANDOC_API_VERSION) 185 | 186 | # Make the comparison 187 | meta = src['meta'] 188 | fmt = 'html' 189 | attach_attrs_math = attach_attrs_factory(Math, 0, allow_space=True) 190 | insert_secnos = insert_secnos_factory(Math) 191 | tmp = walk(src, attach_attrs_math, fmt, meta) 192 | self.assertEqual(walk(tmp, insert_secnos, fmt, meta), expected) 193 | 194 | 195 | def test_elt(self): 196 | """Tests elt().""" 197 | 198 | # pylint: disable=no-member 199 | el = elt('RawBlock', 2) 200 | self.assertEqual(len(el.__closure__), 2) 201 | self.assertEqual(el.__closure__[0].cell_contents, 'RawBlock') 202 | self.assertEqual(el.__closure__[1].cell_contents, 2) 203 | 204 | 205 | def test_quotify_1(self): 206 | """Tests quotify() #1.""" 207 | 208 | ## test.md: "test" ## 209 | 210 | # Command: pandoc test.md -f markdown+smart -t json 211 | src = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"test"}]]}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 212 | 213 | # Check src against current pandoc 214 | md = subprocess.Popen(('echo', '"test"'), stdout=subprocess.PIPE) 215 | output = eval(subprocess.check_output( 216 | (PANDOC+' -f markdown+smart -t json').split(), 217 | stdin=md.stdout).strip()) 218 | self.assertEqual(src, output) 219 | 220 | # Command: pandoc -f markdown-smart test.md -t json 221 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"\"test\""}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 222 | 223 | # Check expected against current pandoc 224 | md = subprocess.Popen(('echo', '"test"'), stdout=subprocess.PIPE) 225 | output = eval(subprocess.check_output( 226 | (PANDOC+' -f markdown-smart -t json').split(), 227 | stdin=md.stdout).strip()) 228 | self.assertEqual(expected, output) 229 | 230 | # Make the comparison 231 | self.assertEqual(quotify(src['blocks']), expected['blocks']) 232 | 233 | 234 | def test_quotify_2(self): 235 | """Tests quotify() #2.""" 236 | 237 | ## test.md: This is 'test 2'. ## 238 | 239 | # Command: pandoc test.md -f markdown+smart -t json 240 | src = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"This"},{"t":"Space"},{"t":"Str","c":"is"},{"t":"Space"},{"t":"Quoted","c":[{"t":"SingleQuote"},[{"t":"Str","c":"test"},{"t":"Space"},{"t":"Str","c":"2"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 241 | 242 | # Check src against current pandoc 243 | md = subprocess.Popen(('echo', "This is 'test 2'."), 244 | stdout=subprocess.PIPE) 245 | output = eval(subprocess.check_output( 246 | (PANDOC+' -f markdown+smart -t json').split(), 247 | stdin=md.stdout).strip()) 248 | self.assertEqual(src, output) 249 | 250 | # Command: pandoc -f markdown-smart test.md -t json 251 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"This"},{"t":"Space"},{"t":"Str","c":"is"},{"t":"Space"},{"t":"Str","c":"'test"},{"t":"Space"},{"t":"Str","c":"2'."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 252 | 253 | # Check expected against current pandoc 254 | md = subprocess.Popen(('echo', "This is 'test 2'."), 255 | stdout=subprocess.PIPE) 256 | output = eval(subprocess.check_output( 257 | (PANDOC+' -f markdown-smart -t json').split(), 258 | stdin=md.stdout).strip()) 259 | self.assertEqual(expected, output) 260 | 261 | # Make the comparison 262 | self.assertEqual(quotify(src['blocks']), expected['blocks']) 263 | 264 | 265 | def test_dollarfy(self): 266 | """Tests dollarfy().""" 267 | 268 | ## test.md: $\frac{1}{2}$ ## 269 | 270 | # Command: pandoc test.md -t json 271 | src = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Math","c":[{"t":"InlineMath"},"\\frac{1}{2}"]}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 272 | 273 | # Check src against current pandoc 274 | md = subprocess.Popen(('echo', r'$\frac{1}{2}$'), 275 | stdout=subprocess.PIPE) 276 | output = eval(subprocess.check_output( 277 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 278 | self.assertEqual(src, output) 279 | 280 | # Hand-coded 281 | expected = eval(r'''[{"t": "Str", "c": "$\\frac{1}{2}$"}]''') 282 | 283 | # Make the comparison 284 | self.assertEqual(dollarfy(src['blocks'][0]['c']), expected) 285 | 286 | 287 | def test_extract_attrs_1(self): 288 | """Tests extract_attrs() #1.""" 289 | 290 | ## test.md: Test {#eq:id .class tag="foo"}. ## 291 | 292 | # Command: pandoc test.md -t json 293 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"Test"},{"t":"Space"},{"t":"Str","c":"{#eq:id"},{"t":"Space"},{"t":"Str","c":".class"},{"t":"Space"},{"t":"Str","c":"tag="},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"foo"}]]},{"t":"Str","c":"}."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 294 | 295 | # Check src against current pandoc 296 | md = subprocess.Popen(('echo', 'Test {#eq:id .class tag="foo"}.'), 297 | stdout=subprocess.PIPE) 298 | output = eval(subprocess.check_output( 299 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 300 | self.assertEqual(src, output) 301 | 302 | # Hand-coded 303 | expected = ['eq:id', ['class'], [['tag', 'foo']]] 304 | 305 | # Make the comparison 306 | self.assertEqual(extract_attrs(src['blocks'][0]['c'], 2).list, expected) 307 | 308 | 309 | def test_extract_attrs_2(self): 310 | """Tests extract_attrs() #2.""" 311 | 312 | ## test.md: Test {#eq:id .class tag="foo"}. ## 313 | 314 | # Command: pandoc test.md -f markdown+smart -t json 315 | src = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"Test"},{"t":"Space"},{"t":"Str","c":"{#eq:id"},{"t":"Space"},{"t":"Str","c":".class"},{"t":"Space"},{"t":"Str","c":"tag="},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"foo"}]]},{"t":"Str","c":"}."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 316 | 317 | # Check src against pandoc-1.17.2 318 | md = subprocess.Popen(('echo', 'Test {#eq:id .class tag="foo"}.'), 319 | stdout=subprocess.PIPE) 320 | output = eval(subprocess.check_output( 321 | (PANDOC+' -f markdown+smart -t json').split(),\ 322 | stdin=md.stdout).strip()) 323 | self.assertEqual(src, output) 324 | 325 | # Hand-coded 326 | expected = ['eq:id', ['class'], [['tag', 'foo']]] 327 | 328 | # Make the comparison 329 | self.assertEqual(extract_attrs(src['blocks'][0]['c'], 2).list, expected) 330 | 331 | 332 | # NOTE: Broken refs are fixed with pandoc 1.18 333 | def test_repair_refs_1(self): 334 | """Tests repair_refs() #1.""" 335 | 336 | ## test.md: {@doe:1999} ## 337 | 338 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 339 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{@doe"}],["mailto:%7B@doe",""]]},{"t":"Str","c":":1999}"}]}]]''') 340 | 341 | # Command: pandoc-1.17.2 test.md -t json 342 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"doe:1999","citationHash":0}],[{"t":"Str","c":"@doe:1999"}]]},{"t":"Str","c":"}"}]}]]''') 343 | 344 | # Make the comparison 345 | pandocxnos.init('1.17.2') 346 | self.assertEqual(walk(src, repair_refs, '', {}), expected) 347 | 348 | # NOTE: Broken refs are fixed with pandoc 1.18 349 | def test_repair_refs_2(self): 350 | """Tests repair_refs() #2.""" 351 | 352 | ## test.md: Eqs. {@eq:1}a and {@eq:1}b. ## 353 | 354 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 355 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"Eqs."},{"t":"Space","c":[]},{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{@eq"}],["mailto:%7B@eq",""]]},{"t":"Str","c":":1}a"},{"t":"Space","c":[]},{"t":"Str","c":"and"},{"t":"Space","c":[]},{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{@eq"}],["mailto:%7B@eq",""]]},{"t":"Str","c":":1}b."}]}]]''') 356 | 357 | # Command: pandoc-1.17.2 test.md -t json 358 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"Eqs."},{"t":"Space","c":[]},{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"@eq:1"}]]},{"t":"Str","c":"}a"},{"t":"Space","c":[]},{"t":"Str","c":"and"},{"t":"Space","c":[]},{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"@eq:1"}]]},{"t":"Str","c":"}b."}]}]]''') 359 | 360 | # Make the comparison 361 | pandocxnos.init('1.17.2') 362 | self.assertEqual(walk(src, repair_refs, {}, ''), expected) 363 | 364 | 365 | # NOTE: Broken refs are fixed with pandoc 1.18 366 | def test_repair_refs_3(self): 367 | """Tests repair_refs() #3.""" 368 | 369 | ## test.md: See {+@eq:1}. ## 370 | 371 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 372 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space","c":[]},{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{+@eq"}],["mailto:%7B+@eq",""]]},{"t":"Str","c":":1}."}]}]]''') 373 | 374 | # Command: pandoc test.md -t json 375 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space","c":[]},{"t":"Str","c":"{+"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"@eq:1"}]]},{"t":"Str","c":"}."}]}]]''') 376 | 377 | # Make the comparison 378 | pandocxnos.init('1.17.2') 379 | self.assertEqual(walk(src, repair_refs, {}, ''), expected) 380 | 381 | 382 | # NOTE: Broken refs are fixed with pandoc 1.18 383 | def test_repair_refs_4(self): 384 | """Tests repair_refs() #4.""" 385 | 386 | ## test.md: *@fig:plot1 and {+@fig:plot3}a. ## 387 | 388 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 389 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"*@fig"}],["mailto:*@fig",""]]},{"t":"Str","c":":plot1"},{"t":"Space","c":[]},{"t":"Str","c":"and"},{"t":"Space","c":[]},{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{+@fig"}],["mailto:%7B+@fig",""]]},{"t":"Str","c":":plot3}a."}]}]]''') 390 | 391 | # Command: pandoc-1.17.2 test.md -t json 392 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"*"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:plot1","citationHash":0}],[{"t":"Str","c":"@fig:plot1"}]]},{"t":"Space","c":[]},{"t":"Str","c":"and"},{"t":"Space","c":[]},{"t":"Str","c":"{+"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:plot3","citationHash":0}],[{"t":"Str","c":"@fig:plot3"}]]},{"t":"Str","c":"}a."}]}]]''') 393 | 394 | # Make the comparison 395 | pandocxnos.init('1.17.2') 396 | self.assertEqual(walk(src, repair_refs, {}, ''), expected) 397 | 398 | 399 | # NOTE: Broken refs are fixed with pandoc 1.18 400 | def test_repair_refs_5(self): 401 | """Tests repair_refs() #5.""" 402 | 403 | ## test.md: +@eq:1, ## 404 | 405 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 406 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"+@eq"}],["mailto:+@eq",""]]},{"t":"Str","c":":1,"}]}]]''') 407 | 408 | # Command: pandoc-1.17.2 test.md -t json 409 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"+"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"@eq:1"}]]},{"t":"Str","c":","}]}]]''') 410 | 411 | # Make the comparison 412 | pandocxnos.init('1.17.2') 413 | self.assertEqual(walk(src, repair_refs, {}, ''), expected) 414 | 415 | 416 | # NOTE: Broken refs are fixed with pandoc 1.18 417 | def test_repair_refs_6(self): 418 | """Tests repair_refs() #6.""" 419 | 420 | ## test.md: {@fig:1{baz=bat}}a ## 421 | 422 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 423 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{@fig"}],["mailto:%7B@fig",""]]},{"t":"Str","c":":1{baz=bat}}a"}]}]]''') 424 | 425 | # Command: pandoc-1.17.2 test.md -t json 426 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:1","citationHash":0}],[{"t":"Str","c":"@fig:1"}]]},{"t":"Str","c":"{baz=bat}}a"}]}]]''') 427 | 428 | # Make the comparison 429 | pandocxnos.init('1.17.2') 430 | self.assertEqual(walk(src, repair_refs, {}, ''), expected) 431 | 432 | 433 | # NOTE: Broken refs are fixed with pandoc 1.18 434 | def test_repair_refs_7(self): 435 | """Tests repair_refs() #7.""" 436 | 437 | ## test.md: {@fig:1{baz=bat foo=bar}}a ## 438 | 439 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 440 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{@fig"}],["mailto:%7B@fig",""]]},{"t":"Str","c":":1{baz=bat"},{"t":"Space","c":[]},{"t":"Str","c":"foo=bar}}a"}]}]]''') 441 | 442 | # Command: pandoc-1.17.2 test.md -t json 443 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:1","citationHash":0}],[{"t":"Str","c":"@fig:1"}]]},{"t":"Str","c":"{baz=bat"},{"t":"Space","c":[]},{"t":"Str","c":"foo=bar}}a"}]}]]''') 444 | 445 | # Make the comparison 446 | pandocxnos.init('1.17.2') 447 | self.assertEqual(walk(src, repair_refs, {}, ''), expected) 448 | 449 | 450 | # NOTE: Broken refs are fixed with pandoc 1.18 451 | def test_repair_refs_8(self): 452 | """Tests repair_refs() #8.""" 453 | 454 | ## test.md: {@fig:1}-{@fig:3} ## 455 | 456 | # Command: pandoc-1.17.2 test.md -f markdown+autolink_bare_uris -t json 457 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"{@fig"}],["mailto:%7B@fig",""]]},{"t":"Str","c":":"},{"t":"Link","c":[["",[],[]],[{"t":"Str","c":"1}-{@fig"}],["mailto:1%7D-%7B@fig",""]]},{"t":"Str","c":":3}"}]}]]''') 458 | 459 | # Command: pandoc test.md -t json 460 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:1","citationHash":0}],[{"t":"Str","c":"@fig:1"}]]},{"t":"Str","c":"}-{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:3","citationHash":0}],[{"t":"Str","c":"@fig:3"}]]},{"t":"Str","c":"}"}]}]]''') 461 | 462 | # Make the comparison 463 | pandocxnos.init('1.17.2') 464 | self.assertEqual(walk(src, repair_refs, {}, ''), expected) 465 | 466 | 467 | def test_process_refs_factory_1(self): 468 | """Tests process_refs_factory() #1.""" 469 | 470 | ## test.md: As shown in @fig:one. ## 471 | 472 | # Command: pandoc test.md -t json 473 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"As"},{"t":"Space"},{"t":"Str","c":"shown"},{"t":"Space"},{"t":"Str","c":"in"},{"t":"Space"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"fig:one","citationHash":0}],[{"t":"Str","c":"@fig:one"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 474 | 475 | # Check src against current pandoc 476 | md = subprocess.Popen(('echo', 'As shown in @fig:one.'), 477 | stdout=subprocess.PIPE) 478 | output = eval(subprocess.check_output( 479 | (PANDOC+' -t json').split(), 480 | stdin=md.stdout).strip()) 481 | self.assertEqual(src, output) 482 | 483 | # Hand-coded (added empty attributes) 484 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"As"},{"t":"Space"},{"t":"Str","c":"shown"},{"t":"Space"},{"t":"Str","c":"in"},{"t":"Space"},{"t":"Cite","c":[["",[],[]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"fig:one","citationHash":0}],[{"t":"Str","c":"@fig:one"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 485 | 486 | # Make the comparison 487 | process_refs = process_refs_factory(None, ['fig:one'], 0) 488 | self.assertEqual(walk(src, process_refs, '', {}), expected) 489 | 490 | 491 | def test_process_refs_factory_2(self): 492 | """Tests process_refs_factory() #2.""" 493 | 494 | ## test.md: (@eq:one) ## 495 | 496 | # Command: pandoc test.md -t json 497 | src = eval( 498 | r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"("},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"eq:one","citationHash":0}],[{"t":"Str","c":"@eq:one"}]]},{"t":"Str","c":")"}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 499 | 500 | # Check src against current pandoc 501 | md = subprocess.Popen(('echo', '(@eq:one)'), stdout=subprocess.PIPE) 502 | output = eval(subprocess.check_output( 503 | (PANDOC+' -t json').split(), 504 | stdin=md.stdout).strip()) 505 | self.assertEqual(src, output) 506 | 507 | # Hand-coded (attributes added) 508 | expected = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"("},{"t":"Cite","c":[["",[],[]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"eq:one","citationHash":0}],[{"t":"Str","c":"@eq:one"}]]},{"t":"Str","c":")"}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 509 | 510 | # Make the comparison 511 | process_refs = process_refs_factory(None, ['eq:one'], 0) 512 | self.assertEqual(walk(src, process_refs, '', {}), expected) 513 | 514 | 515 | def test_process_refs_factory_3(self): 516 | """Tests process_refs_factory() #3.""" 517 | 518 | ## test.md: See {@tbl:1}. ## 519 | 520 | # Command: pandoc test.md -t json 521 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:1","citationHash":0}],[{"t":"Str","c":"@tbl:1"}]]},{"t":"Str","c":"}."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 522 | 523 | # Check src against current pandoc 524 | md = subprocess.Popen(('echo', 'See {@tbl:1}.'), stdout=subprocess.PIPE) 525 | output = eval(subprocess.check_output( 526 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 527 | self.assertEqual(src, output) 528 | 529 | # Hand-coded (braces stripped, attributes added) 530 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Cite","c":[["",[],[]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:1","citationHash":0}],[{"t":"Str","c":"@tbl:1"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 531 | 532 | # Make the comparison 533 | process_refs = process_refs_factory(None, ['tbl:1'], 0) 534 | self.assertEqual(walk(src, process_refs, '', {}), expected) 535 | 536 | 537 | def test_process_refs_factory_4(self): 538 | """Tests process_refs_factory() #4.""" 539 | 540 | ## test.md: See +@eq:1. ## 541 | 542 | # Command: pandoc test.md -t json 543 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Str","c":"+"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"@eq:1"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 544 | 545 | # Check src against current pandoc 546 | md = subprocess.Popen(('echo', 'See +@eq:1.'), stdout=subprocess.PIPE) 547 | output = eval(subprocess.check_output( 548 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 549 | self.assertEqual(src, output) 550 | 551 | # Hand-coded (modifier extracted, attributes added) 552 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Cite","c":[["",[],[["modifier","+"]]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"@eq:1"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 553 | 554 | # Make the comparison 555 | process_refs = process_refs_factory(None, ['eq:1'], 0) 556 | self.assertEqual(walk(src, process_refs, '', {}), expected) 557 | 558 | 559 | def test_process_refs_factory_5(self): 560 | """Tests process_refs_factory() #5.""" 561 | 562 | ## test.md: See {+@tbl:1}. ## 563 | 564 | # Command: pandoc test.md -t json 565 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Str","c":"{+"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:1","citationHash":0}],[{"t":"Str","c":"@tbl:1"}]]},{"t":"Str","c":"}."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 566 | 567 | # Check src against current pandoc 568 | md = subprocess.Popen(('echo', 'See {+@tbl:1}.'), stdout=subprocess.PIPE) 569 | output = eval(subprocess.check_output( 570 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 571 | self.assertEqual(src, output) 572 | 573 | # Hand-coded (braces stripped, modifier extracted, attributes added) 574 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Cite","c":[["",[],[["modifier","+"]]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:1","citationHash":0}],[{"t":"Str","c":"@tbl:1"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 575 | 576 | # Make the comparison 577 | process_refs = process_refs_factory(None, ['tbl:1'], 0) 578 | self.assertEqual(walk(src, process_refs, '', {}), expected) 579 | 580 | 581 | def test_process_refs_factory_6(self): 582 | """Tests process_refs_factory() #6.""" 583 | 584 | ## test.md: See xxx{+@tbl:1}xxx. ## 585 | 586 | # Command: pandoc test.md -t json 587 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Str","c":"xxx{+"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:1","citationHash":0}],[{"t":"Str","c":"@tbl:1"}]]},{"t":"Str","c":"}xxx."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 588 | 589 | # Check src against current pandoc 590 | md = subprocess.Popen(('echo', 'See xxx{+@tbl:1}xxx.'), stdout=subprocess.PIPE) 591 | output = eval(subprocess.check_output( 592 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 593 | self.assertEqual(src, output) 594 | 595 | # Hand-coded (braces stripped, modifier extracted, attributes added) 596 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Str","c":"xxx"},{"t":"Cite","c":[["",[],[["modifier","+"]]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:1","citationHash":0}],[{"t":"Str","c":"@tbl:1"}]]},{"t":"Str","c":"xxx."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 597 | 598 | # Make the comparison 599 | process_refs = process_refs_factory(None, ['tbl:1'], 0) 600 | self.assertEqual(walk(src, process_refs, '', {}), expected) 601 | 602 | 603 | def test_process_refs_factory_7(self): 604 | """Tests process_refs_factory() #7.""" 605 | 606 | ## test.md: See [+@eq:1]. ## 607 | 608 | # Command: pandoc test.md -t json 609 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"NormalCitation"},"citationPrefix":[{"t":"Str","c":"+"}],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"[+@eq:1]"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 610 | 611 | # Check src against current pandoc 612 | md = subprocess.Popen(('echo', 'See [+@eq:1].'), stdout=subprocess.PIPE) 613 | output = eval(subprocess.check_output( 614 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 615 | self.assertEqual(src, output) 616 | 617 | # Hand-coded (modifier extracted, attributes added) 618 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Str","c":"See"},{"t":"Space"},{"t":"Cite","c":[["",[],[["modifier","+"]]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"NormalCitation"},"citationPrefix":[],"citationId":"eq:1","citationHash":0}],[{"t":"Str","c":"[+@eq:1]"}]]},{"t":"Str","c":"."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 619 | 620 | # Make the comparison 621 | process_refs = process_refs_factory(None, ['eq:1'], 0) 622 | self.assertEqual(walk(src, process_refs, '', {}), expected) 623 | 624 | 625 | def test_use_refs_factory_7(self): 626 | """Tests use_refs_factory() #7.""" 627 | 628 | ## test.md: {+@tbl:one}-{@tbl:four} provide the data. ## 629 | 630 | # Command: pandoc test.md -t json 631 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Str","c":"{+"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:one","citationHash":0}],[{"t":"Str","c":"@tbl:one"}]]},{"t":"Str","c":"}-{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":2,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:four","citationHash":0}],[{"t":"Str","c":"@tbl:four"}]]},{"t":"Str","c":"}"},{"t":"Space"},{"t":"Str","c":"provide"},{"t":"Space"},{"t":"Str","c":"the"},{"t":"Space"},{"t":"Str","c":"data."}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 632 | 633 | # Check src against current pandoc 634 | md = subprocess.Popen( 635 | ('echo', '{+@tbl:one}-{@tbl:four} provide the data.'), 636 | stdout=subprocess.PIPE) 637 | output = eval(subprocess.check_output( 638 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 639 | self.assertEqual(src, output) 640 | 641 | # Hand-coded 642 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Cite","c":[["",[],[["modifier","+"]]],[{"citationSuffix":[],"citationNoteNum":1,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:one","citationHash":0}],[{"t":"Str","c":"@tbl:one"}]]},{"t":"Str","c":"-"},{"t":"Cite","c":[["",[],[]],[{"citationSuffix":[],"citationNoteNum":2,"citationMode":{"t":"AuthorInText"},"citationPrefix":[],"citationId":"tbl:four","citationHash":0}],[{"t":"Str","c":"@tbl:four"}]]},{"t":"Space"},{"t":"Str","c":"provide"},{"t":"Space"},{"t":"Str","c":"the"},{"t":"Space"},{"t":"Str","c":"data."}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 643 | 644 | # Make the comparison 645 | process_refs = process_refs_factory(None, ['tbl:one', 'tbl:four'], 0) 646 | self.assertEqual(walk(src, process_refs, '', {}), expected) 647 | 648 | @unittest.skip('Known issue for pandoc-1.15.2') 649 | def test_use_refs_factory_8(self): 650 | """Tests use_refs_factory() #8.""" 651 | 652 | ## test.md: @fig:1: 653 | 654 | # pandoc-1.15.2 doesn't detect references that end in a colon. This 655 | # was fixed in subsequent versions of pandoc. There is a trivial 656 | # workaround; use "{@fig:1}:" instead. This is demonstrated in the 657 | # next unit test. Given that there is a trivial work-around, this is 658 | # probably not worth fixing. 659 | 660 | # Command: pandoc-1.15.2 test.md -t json 661 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"@fig:1:"}]}]]''') 662 | 663 | # Check against pandoc-1.15.2 664 | md = subprocess.Popen(('echo', '@fig:1:'), stdout=subprocess.PIPE) 665 | output = eval(subprocess.check_output( 666 | (PANDOC1p15 + ' -t json').split(), stdin=md.stdout).strip()) 667 | self.assertEqual(src, output) 668 | 669 | # Generate expected using current pandoc 670 | md = subprocess.Popen(('echo', '@fig:1:'), stdout=subprocess.PIPE) 671 | expected = eval(subprocess.check_output( 672 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 673 | 674 | # Make the comparison 675 | process_refs = process_refs_factory(None, ['fig:1'], 0) 676 | self.assertEqual(walk(src, process_refs, {}, ''), expected) 677 | 678 | 679 | def test_process_refs_factory_9(self): 680 | """Tests process_refs_factory() #9.""" 681 | 682 | ## test.md: {@fig:1}: 683 | 684 | # See previous unit test 685 | 686 | # Command: pandoc-1.15.2 test.md -t json 687 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"{"},{"t":"Cite","c":[[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:1","citationHash":0}],[{"t":"Str","c":"@fig:1"}]]},{"t":"Str","c":"}:"}]}]]''') 688 | 689 | # Check against pandoc-1.15.2 690 | md = subprocess.Popen(('echo', '{@fig:1}:'), stdout=subprocess.PIPE) 691 | output = eval(subprocess.check_output( 692 | (PANDOC1p15 + ' -t json').split(), stdin=md.stdout).strip()) 693 | self.assertEqual(src, output) 694 | 695 | # Hand-coded 696 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Cite","c":[["",[],[]],[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:1","citationHash":0}],[{"t":"Str","c":"@fig:1"}]]},{"t":"Str","c":":"}]}]]''') 697 | 698 | # Make the comparison 699 | process_refs = process_refs_factory(None, ['fig:1'], 0) 700 | self.assertEqual(walk(src, process_refs, {}, ''), expected) 701 | 702 | 703 | def test_replace_refs_factory(self): 704 | """Tests replace_refs_factory.""" 705 | 706 | ## test.md: As shown in @fig:1. ## 707 | 708 | # Command: pandoc-1.15.2 test.md -t json 709 | src = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"As"},{"t":"Space","c":[]},{"t":"Str","c":"shown"},{"t":"Space","c":[]},{"t":"Str","c":"in"},{"t":"Space","c":[]},{"t":"Cite","c":[["",[],[]],[{"citationSuffix":[],"citationNoteNum":0,"citationMode":{"t":"AuthorInText","c":[]},"citationPrefix":[],"citationId":"fig:one","citationHash":0}],[{"t":"Str","c":"@fig:one"}]]},{"t":"Str","c":"."}]}]]''') 710 | 711 | # Hand-coded 712 | expected = eval(r'''[{"unMeta":{}},[{"t":"Para","c":[{"t":"Str","c":"As"},{"t":"Space","c":[]},{"t":"Str","c":"shown"},{"t":"Space","c":[]},{"t":"Str","c":"in"},{"t":"Space","c":[]},{"t":"Str","c":"fig.\u00A0"},{'t':'Link','c':[['',[],[]],[{'t':'Str','c':'1'}],['#fig:one','']]},{"t":"Str","c":"."}]}]]''') 713 | 714 | # Make the comparison 715 | replace_refs = replace_refs_factory({'fig:one':pandocxnos.Target(1, 1)}, 716 | True, False, 717 | ['fig.', 'figs.'], 718 | ['Figure', 'Figures']) 719 | self.assertEqual(walk(walk(src, replace_refs, {}, ''), 720 | join_strings, {}, ''), expected) 721 | 722 | 723 | def test_attach_attrs_factory(self): 724 | """Tests attach_attrs_math().""" 725 | 726 | attach_attrs_math = attach_attrs_factory(Math, 0, allow_space=True) 727 | 728 | ## test.md: $$ y = f(x) $${#eq:1 tag="B.1"} ## 729 | 730 | # Command: pandoc test.md -t json 731 | src = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Math","c":[{"t":"DisplayMath"}," y = f(x) "]},{"t":"Str","c":"{#eq:1"},{"t":"Space"},{"t":"Str","c":"tag="},{"t":"Quoted","c":[{"t":"DoubleQuote"},[{"t":"Str","c":"B.1"}]]},{"t":"Str","c":"}"}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 732 | 733 | # Check src against current pandoc 734 | md = subprocess.Popen(('echo', '$$ y = f(x) $${#eq:1 tag="B.1"}'), 735 | stdout=subprocess.PIPE) 736 | output = eval(subprocess.check_output( 737 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 738 | self.assertEqual(src, output) 739 | 740 | # Hand-coded (attributes deleted) 741 | expected = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Math","c":[["eq:1",[],[["tag","B.1"]]],{"t":"DisplayMath"}," y = f(x) "]}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 742 | 743 | # Make the comparison 744 | self.assertEqual(walk(src, attach_attrs_math, '', {}), expected) 745 | 746 | 747 | def test_detach_attrs_factory(self): 748 | """Tests filter_attrs_factory().""" 749 | 750 | detach_attrs_math = detach_attrs_factory(Math) 751 | 752 | ## Use 'expected' from test_attach_attrs_factory ## 753 | 754 | src = eval(r'''{"meta":{},"blocks":[{"t":"Para","c":[{"t":"Math","c":[["eq:1",[],[["tag","B.1"]]],{"t":"DisplayMath"}," y = f(x) "]}]}],"pandoc-api-version":[%s]}'''%PANDOC_API_VERSION) 755 | 756 | # test.md: $$ y = f(x) $$ 757 | # Command: pandoc test.md -t json 758 | expected = eval(r'''{"blocks":[{"t":"Para","c":[{"t":"Math","c":[{"t":"DisplayMath"}," y = f(x) "]}]}],"pandoc-api-version":[%s],"meta":{}}'''%PANDOC_API_VERSION) 759 | 760 | # Check expected against current pandoc 761 | md = subprocess.Popen(('echo', '$$ y = f(x) $$'), 762 | stdout=subprocess.PIPE) 763 | output = eval(subprocess.check_output( 764 | (PANDOC+' -t json').split(), stdin=md.stdout).strip()) 765 | self.assertEqual(expected, output) 766 | 767 | # Make the comparison 768 | self.assertEqual(walk(src, detach_attrs_math, '', {}), expected) 769 | 770 | 771 | # pylint: disable=too-few-public-methods 772 | class TestPandocAttributes(unittest.TestCase): 773 | """Test the pandocattributes package.""" 774 | 775 | def test_kvs(self): 776 | """Tests PandocAttributes.kvs.""" 777 | attrs = PandocAttributes(['', [], [['tag', 'B.1']]], 'pandoc') 778 | kvs = attrs.kvs 779 | 780 | # Ensure that changing the kvs changes attrs too 781 | kvs['tag'] = 'B.3' 782 | self.assertEqual(attrs['tag'], 'B.3') 783 | 784 | 785 | #----------------------------------------------------------------------------- 786 | # main() 787 | 788 | def main(): 789 | """Runs the suite of unit tests""" 790 | 791 | # Do the tests 792 | suite = unittest.TestSuite() 793 | suite.addTests(unittest.makeSuite(TestXnos)) 794 | suite.addTests(unittest.makeSuite(TestPandocAttributes)) 795 | result = unittest.TextTestRunner(verbosity=1).run(suite) 796 | n_errors = len(result.errors) 797 | n_failures = len(result.failures) 798 | 799 | if n_errors or n_failures: 800 | print('\n\nSummary: %d errors and %d failures reported\n'%\ 801 | (n_errors, n_failures)) 802 | 803 | print() 804 | 805 | sys.exit(n_errors+n_failures) 806 | 807 | 808 | if __name__ == '__main__': 809 | main()() 810 | --------------------------------------------------------------------------------