Browse Source

Automatically convert spaces to tabs in codeblocks

• Adds codeblock css rule for tab-size
• Only applies to GDScript/C++
• Fix some inconsistent cpp codeblock filters
Thaddeus Crews 1 year ago
parent
commit
c4bb6dfdfa

+ 1 - 0
_static/css/custom.css

@@ -847,6 +847,7 @@ code,
 
 .highlight {
     background-color: var(--highlight-background-color);
+    tab-size: 4;
 }
 
 /* Emphasized lines */

+ 13 - 0
_static/js/custom.js

@@ -291,6 +291,19 @@ $(document).ready(() => {
     }
   }
 
+  // Change indentation from spaces to tabs for codeblocks.
+  const codeBlocks = document.querySelectorAll('.rst-content div[class^="highlight"] pre');
+  for (const codeBlock of codeBlocks) {
+    const classList = codeBlock.parentNode.parentNode.classList;
+    if (!classList.contains('highlight-gdscript') && !classList.contains('highlight-cpp')) {
+      // Only change indentation for GDScript and C++.
+      continue;
+    }
+    let html = codeBlock.innerHTML;
+    html = html.replace(/(?<=^(<span class="w">)?( {4})*)( {4})/gm, '\t');
+    codeBlock.innerHTML = html;
+  }
+
   // See `godot_is_latest` in conf.py
   const isLatest = document.querySelector('meta[name=doc_is_latest]').content.toLowerCase() === 'true';
   if (isLatest) {

+ 2 - 2
tutorials/migrating/upgrading_to_godot_4.1.rst

@@ -253,7 +253,7 @@ This means that GDExtensions made for Godot 4.0 will need to be recompiled for G
 
 In Godot 4.0, your "entry_symbol" function looks something like this:
 
-.. code-block:: C++
+.. code-block:: cpp
 
   GDExtensionBool GDE_EXPORT example_library_init(const GDExtensionInterface *p_interface, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
       godot::GDExtensionBinding::InitObject init_obj(p_interface, p_library, r_initialization);
@@ -267,7 +267,7 @@ In Godot 4.0, your "entry_symbol" function looks something like this:
 
 However, for Godot 4.1, it should look like:
 
-.. code-block:: C++
+.. code-block:: cpp
 
   GDExtensionBool GDE_EXPORT example_library_init(GDExtensionInterfaceGetProcAddress p_get_proc_address, const GDExtensionClassLibraryPtr p_library, GDExtensionInitialization *r_initialization) {
       godot::GDExtensionBinding::InitObject init_obj(p_get_proc_address, p_library, r_initialization);

+ 12 - 12
tutorials/scripting/gdextension/gdextension_cpp_example.rst

@@ -158,7 +158,7 @@ Your folder structure should now look like this:
 In the ``src`` folder, we'll start with creating our header file for the
 GDExtension node we'll be creating. We will name it ``gdexample.h``:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     #ifndef GDEXAMPLE_H
     #define GDEXAMPLE_H
@@ -211,7 +211,7 @@ as the ``_process`` function you're used to in GDScript.
 
 Let's implement our functions by creating our ``gdexample.cpp`` file:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     #include "gdexample.h"
     #include <godot_cpp/core/class_db.hpp>
@@ -250,7 +250,7 @@ and source file like we've implemented ``GDExample`` up above. What we need now
 is a small bit of code that tells Godot about all the classes in our
 GDExtension plugin.
 
-.. code-block:: C++
+.. code-block:: cpp
 
     #include "register_types.h"
 
@@ -304,7 +304,7 @@ Furthermore, it sets the level of initialization (core, servers, scene, editor,
 At last, we need the header file for the ``register_types.cpp`` named
 ``register_types.h``.
 
-.. code-block:: C++
+.. code-block:: cpp
 
     #ifndef GDEXAMPLE_REGISTER_TYPES_H
     #define GDEXAMPLE_REGISTER_TYPES_H
@@ -463,7 +463,7 @@ Lets add a property that allows us to control the amplitude of our wave.
 In our ``gdexample.h`` file we need to add a member variable and getter and setter
 functions:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     ...
     private:
@@ -478,7 +478,7 @@ functions:
 In our ``gdexample.cpp`` file we need to make a number of changes, we will only
 show the methods we end up changing, don't remove the lines we're omitting:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     void GDExample::_bind_methods() {
         ClassDB::bind_method(D_METHOD("get_amplitude"), &GDExample::get_amplitude);
@@ -520,7 +520,7 @@ Let's do the same but for the speed of our animation and use a setter and getter
 function. Our ``gdexample.h`` header file again only needs a few more lines of
 code:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     ...
         double amplitude;
@@ -534,7 +534,7 @@ code:
 This requires a few more changes to our ``gdexample.cpp`` file, again we're only
 showing the methods that have changed so don't remove anything we're omitting:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     void GDExample::_bind_methods() {
         ...
@@ -591,7 +591,7 @@ would need to showcase a far more complete example.
 
 This is the required syntax:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     some_other_node->connect("the_signal", this, "my_method");
 
@@ -604,7 +604,7 @@ emit a signal every time a second has passed and pass the new location along.
 
 In our ``gdexample.h`` header file, we need to define a new member ``time_emit``:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     ...
         double time_passed;
@@ -619,7 +619,7 @@ constructor. We'll look at the other 2 needed changes one by one.
 In our ``_bind_methods`` method, we need to declare our signal. This is done
 as follows:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     void GDExample::_bind_methods() {
         ...
@@ -640,7 +640,7 @@ of type ``Vector2``, respectively named "node" and "new_pos".
 
 Next, we'll need to change our ``_process`` method:
 
-.. code-block:: C++
+.. code-block:: cpp
 
     void GDExample::_process(double delta) {
         time_passed += speed * delta;