Generate_CSS_PropertyID_h.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/ByteBuffer.h>
  7. #include <AK/JsonObject.h>
  8. #include <AK/SourceGenerator.h>
  9. #include <AK/StringBuilder.h>
  10. #include <LibCore/File.h>
  11. #include <ctype.h>
  12. static String title_casify(const String& dashy_name)
  13. {
  14. auto parts = dashy_name.split('-');
  15. StringBuilder builder;
  16. for (auto& part : parts) {
  17. if (part.is_empty())
  18. continue;
  19. builder.append(toupper(part[0]));
  20. if (part.length() == 1)
  21. continue;
  22. builder.append(part.substring_view(1, part.length() - 1));
  23. }
  24. return builder.to_string();
  25. }
  26. int main(int argc, char** argv)
  27. {
  28. if (argc != 2) {
  29. warnln("usage: {} <path/to/CSS/Properties.json>", argv[0]);
  30. return 1;
  31. }
  32. auto file = Core::File::construct(argv[1]);
  33. if (!file->open(Core::OpenMode::ReadOnly))
  34. return 1;
  35. auto json = JsonValue::from_string(file->read_all());
  36. VERIFY(json.has_value());
  37. VERIFY(json.value().is_object());
  38. StringBuilder builder;
  39. SourceGenerator generator { builder };
  40. generator.append(R"~~~(
  41. #pragma once
  42. #include <AK/StringView.h>
  43. #include <AK/Traits.h>
  44. #include <LibWeb/Forward.h>
  45. namespace Web::CSS {
  46. enum class PropertyID {
  47. Invalid,
  48. Custom,
  49. )~~~");
  50. json.value().as_object().for_each_member([&](auto& name, auto& value) {
  51. VERIFY(value.is_object());
  52. auto member_generator = generator.fork();
  53. member_generator.set("name:titlecase", title_casify(name));
  54. member_generator.append(R"~~~(
  55. @name:titlecase@,
  56. )~~~");
  57. });
  58. generator.append(R"~~~(
  59. };
  60. PropertyID property_id_from_string(const StringView&);
  61. const char* string_from_property_id(PropertyID);
  62. bool is_inherited_property(PropertyID);
  63. bool is_pseudo_property(PropertyID);
  64. RefPtr<StyleValue> property_initial_value(PropertyID);
  65. } // namespace Web::CSS
  66. namespace AK {
  67. template<>
  68. struct Traits<Web::CSS::PropertyID> : public GenericTraits<Web::CSS::PropertyID> {
  69. static unsigned hash(Web::CSS::PropertyID property_id) { return int_hash((unsigned)property_id); }
  70. };
  71. } // namespace AK
  72. )~~~");
  73. outln("{}", generator.as_string_view());
  74. }